📖 Scenario: You have a small DC motor connected to an Arduino. You want to control its speed smoothly using PWM (Pulse Width Modulation). This is like adjusting the brightness of a lamp by changing how long it stays on and off quickly.
🎯 Goal: Build a simple Arduino program that sets up the motor pin, defines a speed value, uses PWM to control the motor speed, and prints the speed to the Serial Monitor.
📋 What You'll Learn
Use pin 9 for the motor control output
Create a variable called motorPin and set it to 9
Create a variable called speed and set it to 128 (half speed)
Use analogWrite() to send the speed value to the motor pin
Print the speed value to the Serial Monitor using Serial.println()
💡 Why This Matters
🌍 Real World
Controlling motor speed with PWM is common in robotics, fans, and toys where smooth speed control is needed.
💼 Career
Understanding PWM and motor control is important for embedded systems engineers and anyone working with hardware and electronics.
Progress0 / 4 steps
1
Set up the motor pin
Create an integer variable called motorPin and set it to 9. Then, in the setup() function, set motorPin as an output using pinMode(). Also, start the serial communication at 9600 baud with Serial.begin(9600).
Arduino
Hint
Use pinMode(motorPin, OUTPUT); inside setup() to prepare the pin for output. Use Serial.begin(9600); to start serial communication.
2
Define the motor speed
Create an integer variable called speed and set it to 128 (which is about half speed). Add this variable above the setup() function.
Arduino
Hint
Declare int speed = 128; before setup() so it can be used anywhere in the program.
3
Control the motor speed with PWM
Inside the loop() function, use analogWrite() to send the speed value to motorPin. This will control the motor speed using PWM.
Arduino
Hint
Use analogWrite(motorPin, speed); inside loop() to control motor speed.
4
Print the motor speed to Serial Monitor
Still inside the loop() function, add a line to print the speed value to the Serial Monitor using Serial.println(speed);. This helps you see the speed value on your computer.
Arduino
Hint
Use Serial.println(speed); to print the speed value so you can see it on your computer screen.
Practice
(1/5)
1. What does PWM do in motor speed control on an Arduino?
easy
A. It changes the motor's voltage permanently.
B. It switches power on and off quickly to control speed.
C. It changes the motor's direction.
D. It measures the motor's speed.
Solution
Step 1: Understand PWM function
PWM (Pulse Width Modulation) controls speed by rapidly switching power on and off.
Step 2: Compare options
Only It switches power on and off quickly to control speed. correctly describes PWM's role in speed control, others describe different functions.
Final Answer:
It switches power on and off quickly to control speed. -> Option B
Quick Check:
PWM controls speed by switching power on/off [OK]
Hint: PWM controls speed by fast power switching [OK]
Common Mistakes:
Thinking PWM changes voltage permanently
Confusing speed control with direction control
Believing PWM measures speed
2. Which Arduino function is used to set motor speed with PWM?
easy
A. pinMode(pin, OUTPUT)
B. digitalWrite(pin, value)
C. analogWrite(pin, value)
D. delay(value)
Solution
Step 1: Identify PWM function
analogWrite(pin, value) sets PWM signal on a pin with value 0-255 controlling speed.
analogWrite value ranges 0 (off) to 255 (full speed). 128 is about half of 255.
Step 2: Interpret speed from value
Value 128 means roughly half the power, so motor runs at about half speed.
Final Answer:
Motor runs at about half speed -> Option D
Quick Check:
128/255 ≈ half speed [OK]
Hint: PWM 128 means about half speed [OK]
Common Mistakes:
Thinking 128 means full speed
Assuming motor is off at 128
Believing speed can be doubled
4. Identify the error in this code snippet for motor speed control:
int speed = 300;
analogWrite(6, speed);
medium
A. Speed value is out of PWM range (0-255).
B. Pin 6 cannot be used for PWM.
C. analogWrite requires three arguments.
D. Variable speed must be declared as float.
Solution
Step 1: Check PWM value limits
analogWrite accepts values from 0 to 255. 300 is outside this range.
Step 2: Verify other options
Pin 6 supports PWM, analogWrite takes two arguments, and int is fine for speed.
Final Answer:
Speed value is out of PWM range (0-255). -> Option A
Quick Check:
PWM value must be 0-255 [OK]
Hint: PWM values must be 0 to 255 only [OK]
Common Mistakes:
Using values above 255 for PWM
Thinking analogWrite needs 3 arguments
Believing PWM pins are limited incorrectly
5. You want to smoothly increase motor speed from 0 to full over 5 seconds using PWM on pin 10. Which code snippet achieves this?
hard
A. for(int speed=0; speed<=255; speed++) { analogWrite(10, speed); delay(20); }
B. analogWrite(10, 255); delay(5000); analogWrite(10, 0);
C. for(int speed=255; speed>=0; speed--) { analogWrite(10, speed); delay(20); }
D. digitalWrite(10, HIGH); delay(5000); digitalWrite(10, LOW);
Solution
Step 1: Understand smooth speed increase
Gradually increasing PWM value from 0 to 255 in small steps with delay creates smooth speed change.
Step 2: Analyze code snippets
for(int speed=0; speed<=255; speed++) { analogWrite(10, speed); delay(20); } uses a for loop from 0 to 255 with delay(20)ms, totaling about 5 seconds. Others either jump speed or use digitalWrite which is not PWM.