Sub topic 3
PWM: Faking Analog Output with `analogWrite()`
Assume you can upload an Arduino sketch and already know setup(), loop(), pinMode(), and digitalWrite(). You should also know that an Arduino pin is a signal source, not a power supply for a motor.
A digital pin has two normal states: LOW and HIGH. Pulse-width modulation (PWM) creates a useful middle effect by switching the pin between those states very quickly. The signal is still digital, but a load can respond to its average power.
The PWM working model
A PWM signal repeats a fixed pattern called a cycle. During each cycle, the signal stays HIGH for part of the time and LOW for the rest.
The percentage of each cycle spent HIGH is the duty cycle. A 25% duty cycle means the signal is HIGH for one quarter of every cycle. A 75% duty cycle means it is HIGH for three quarters.
On many Arduino boards, analogWrite() accepts a value from 0 to 255:
| Value | Approximate duty cycle | Typical result |
|---|---|---|
| 0 | 0% | Always LOW |
| 128 | 50% | Half the cycle HIGH |
| 255 | 100% | Always HIGH |
The output is not a steady analog voltage. For example, analogWrite(pin, 128) does not make a 5 V Arduino pin become a stable 2.5 V source. A multimeter may display a value near 2.5 V because it averages the rapid switching. An LED, motor, or other load may respond to that average.
Using `analogWrite()`
PWM works only on pins marked with a ~ on many Arduino boards. On an Uno, common PWM pins include 3, 5, 6, 9, 10, and 11. Check the board documentation before choosing a pin.
This sketch fades an LED connected through a suitable resistor:
const int LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
for (int level = 0; level <= 255; level++) {
analogWrite(LED_PIN, level);
delay(10);
}
for (int level = 255; level >= 0; level--) {
analogWrite(LED_PIN, level);
delay(10);
}
}The level value controls duty cycle. Increasing it usually makes the LED brighter, although human vision does not perceive brightness linearly.
PWM and motors
For a small DC motor, PWM changes the power delivered over time. A higher duty cycle usually produces more speed, but the relationship is not exact. Friction, mechanical load, motor voltage, and the motor’s starting behavior all matter.
Do not connect a motor directly to an Arduino pin. Motors can draw far more current than a pin should provide, especially when starting or stalled. Use PWM to control a transistor or motor-driver input. The driver handles motor current while the Arduino supplies only a control signal.
The PWM frequency also matters. Arduino defaults are suitable for many basic motor experiments, but some motors, drivers, and audible-noise requirements need a different frequency. Frequency changes are board-specific, so change timer settings only after checking how they affect other functions such as millis() or servo timing.
Common errors
- Calling
analogWrite()on a non-PWM pin usually gives only an on-or-off result. - Using a value outside 0–255 can produce confusing behavior. Keep values in range or use
constrain(). - Assuming PWM limits current by itself is unsafe. The switched load still needs a suitable transistor or driver.
- Forgetting a shared ground can leave the driver without a reliable control reference.
- Expecting the motor to start at a very low duty cycle is unrealistic. A motor may need a short higher-power start, then a lower running level.
Exercise
What duty cycle does analogWrite(6, 192) request, approximately, and why does this not create a steady 3.76 V output on a 5 V Arduino?
Show answer
It requests about 75% duty cycle because 192 is three quarters of 255. The pin switches between 0 V and 5 V; the apparent average is produced by the load or a measuring instrument, not by a steady 3.76 V level.