Bird
0
0
Arduinoprogramming~10 mins

Motor speed control with PWM in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Motor speed control with PWM
Start
Setup PWM Pin
Set PWM Value (0-255)
Write PWM to Motor Pin
Motor Speed Changes
Loop or Update PWM
Back to Set PWM Value
The program sets up a PWM pin, writes a value between 0 and 255 to control motor speed, then loops to update speed as needed.
Execution Sample
Arduino
int motorPin = 9;
void setup() {
  pinMode(motorPin, OUTPUT);
}
void loop() {
  analogWrite(motorPin, 128); // half speed
}
This code sets motorPin as output and writes a PWM value of 128 to run the motor at half speed.
Execution Table
StepActionPWM ValueMotor Speed Effect
1Setup motorPin as OUTPUTN/AMotor ready to receive PWM
2Write PWM value 128 to motorPin128Motor runs at about 50% speed
3Loop repeats, PWM stays 128128Motor speed remains steady
4If PWM changes (e.g., 255)255Motor runs at full speed
5If PWM changes (e.g., 0)0Motor stops
6End of loop iterationN/AReady for next PWM update
💡 Loop runs continuously; motor speed updates whenever PWM value changes
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
motorPin99999
PWM ValueN/A12825500
Key Moments - 3 Insights
Why does the motor speed change when PWM value changes?
Because PWM value controls the power sent to the motor; higher PWM means more power and faster speed (see execution_table steps 2, 4, 5).
What happens if PWM value is 0?
The motor stops because no power is sent (execution_table step 5 shows PWM 0 means motor speed stops).
Why do we use analogWrite instead of digitalWrite?
analogWrite sends a PWM signal to control speed smoothly, while digitalWrite only turns motor fully ON or OFF.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what PWM value makes the motor run at half speed?
A128
B255
C0
D64
💡 Hint
Check step 2 in the execution_table where PWM value 128 corresponds to half speed
At which step does the motor stop running?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look at step 5 in execution_table where PWM is 0 and motor speed stops
If PWM value changes from 128 to 255, what happens to motor speed?
AMotor speed stays the same
BMotor speed increases to full speed
CMotor speed decreases
DMotor stops
💡 Hint
See step 4 in execution_table where PWM 255 means full speed
Concept Snapshot
Motor speed control with PWM:
- Use analogWrite(pin, value) where value is 0-255
- 0 means motor off, 255 means full speed
- PWM controls power by switching ON/OFF rapidly
- Setup pin as OUTPUT before writing PWM
- Loop to update speed dynamically
Full Transcript
This example shows how to control motor speed using PWM on an Arduino. First, the motor pin is set as an output. Then, analogWrite sends a PWM signal with a value between 0 and 255. A value of 128 runs the motor at half speed, 255 at full speed, and 0 stops the motor. The loop repeats to keep the motor running or update speed. PWM controls motor power by turning the pin on and off quickly, changing the average voltage and thus speed.