Sub topic 7
Controlling Speed with PWM
This chapter assumes you know pinMode, digitalWrite, and analogWrite, and that a motor driver already handles the motor’s current. Do not connect a motor directly to an Arduino pin.
How PWM controls speed
Pulse-width modulation, or PWM, controls motor power by switching the driver rapidly between on and off. The duty cycle is the percentage of each cycle that the signal stays on.
For example, a 25% duty cycle applies power for one quarter of each cycle. A 75% duty cycle applies power for three quarters. On many Arduino boards, analogWrite(pin, value) uses values from 0 to 255:
| `analogWrite` value | Approximate duty cycle | Typical effect |
|---|---|---|
| 0 | 0% | Motor off |
| 128 | 50% | Medium power |
| 255 | 100% | Full power |
The motor’s winding has inductance, so its current does not instantly become zero during each off period. Mechanical inertia also smooths the speed. The motor therefore responds roughly to the average applied voltage, although speed is not perfectly proportional to the PWM value. Friction, load, supply voltage, and back electromotive force change the result.
The PWM signal must go to the driver’s enable or PWM input. Direction inputs select which way the motor turns. The driver uses the PWM signal to regulate the selected direction.
A basic speed control function
This example assumes an H-bridge module with two direction inputs and one PWM-capable enable input. The motor and its supply connect to the driver, while the Arduino provides logic signals and shares ground with the driver.
const uint8_t IN1 = 7;
const uint8_t IN2 = 8;
const uint8_t ENA = 9; // PWM-capable pin
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);
// Select forward direction.
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
void loop() {
analogWrite(ENA, 80); // about 31% duty cycle
delay(3000);
analogWrite(ENA, 160); // about 63% duty cycle
delay(3000);
analogWrite(ENA, 255); // 100% duty cycle
delay(3000);
analogWrite(ENA, 0); // disable the driver output
delay(1000);
}On boards that use an 8-bit PWM value, the duty cycle is approximately:
duty cycle = value / 255 × 100%
A motor may not start at a low duty cycle because static friction requires more torque than the low average current provides. A common solution is to apply a short starting boost, then reduce the duty cycle to the desired running value.
Choosing the PWM frequency
The PWM frequency is the number of switching cycles per second. Low frequencies can produce an audible whine and visible speed variation. Higher frequencies usually reduce audible noise, but they increase switching losses and may reduce low-speed torque on some drivers.
Use the driver’s recommended frequency first. Changing timer registers can affect other Arduino functions, including analogWrite on related pins and timing functions such as millis().
Exercise
Why might a motor fail to start at a PWM value of 40 but start at 100?
Show answer
The lower duty cycle may not provide enough average current and torque to overcome static friction and the attached load.