What PWM is
A digital pin is only ever 0 V or 5 V. PWM switches it between the two hundreds of times a second and varies how long it stays up, and a lamp, a motor or an eye responds to the average. analogWrite does it on six pins; on the pin-13 lamp the sketch has to do it itself.
Only two voltages
A pin set to OUTPUT is either HIGH, near 5 V, or LOW, near 0 V. There is no setting in between. What it can do is switch, very fast, and the fraction of each cycle it spends HIGH is the duty cycle. Switching like that is pulse-width modulation, PWM: the pulses stay the same distance apart and only their width changes.
The green line is all the pin ever does. The dashed line is the average,
duty × 5 V, and it is what everything slower than the pin responds to: an
LED's brightness as your eye judges it, a motor's speed, a meter's needle.
The pin itself is never at the average, which is why analogWrite is a
slightly misleading name.
analogWrite, 0 to 255
analogWrite(pin, value) sets the duty on one of the six pins marked with a
~, 490 or 980 cycles a second, as the
next article measures:
valueruns from 0 to 255, and the duty is value ÷ 255. 64 is about 25%, 128 about 50%.- 0 and 255 are not PWM. The Arduino core turns them into
digitalWrite(pin, LOW)anddigitalWrite(pin, HIGH), so 0 is fully off and 255 fully on. analogWritesets the pin to OUTPUT itself. It needs nopinMode.
On a pin with no ~, the same call cannot pulse. The core's source makes any
value under 128 a plain LOW and anything from 128 a plain HIGH. That includes
13, the pin the L lamp follows, and the sketch above starts by showing it:
analogWrite(13, 100) is off, analogWrite(13, 200) is fully on.
Doing it by hand, slowly
With no timer on 13, the sketch makes PWM itself. Every pass of loop()
asks micros() how far into the current period it is, and lights the lamp
for the first quarter. That is PWM at 25% duty, and the sketch starts it at a period
of a whole second so you can watch each pulse.
Then it gets faster. At 4 Hz it still blinks. At 20 Hz it flickers. Somewhere around 50 to 100 Hz, for most people, the flicker disappears and the lamp glows steadily at part brightness. At 500 Hz nothing changes except that the glow is easier on the eye. The duty was 25% the whole time: speed turned a blink into a brightness.
That is the whole idea, and it is why the timer-driven pins run at hundreds of hertz. Faster still would not look different, and slower would flicker.
The code
No wiring. It first shows what analogWrite does on pin 13, which has no PWM, then switches the pin-13 lamp (L on the UNO, D13 on the Nano) itself at a 25% duty cycle, starting at one period a second and getting faster every four seconds.
// Slow_Pwm_On_D13.ino — one duty cycle on the pin-13 lamp,
// faster and faster
// Tools > Board: Arduino Uno, or Arduino Nano with
// Processor ATmega328P (Old Bootloader if the upload says
// "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: none. The pin-13 lamp (L on the UNO, D13 on the Nano)
// follows D13, and D13 has no PWM timer, so the sketch
// switches it itself, timed with micros().
const byte LAMP = 13;
const byte DUTY_PCT = 25; // on for a quarter of every period
const unsigned long PERIOD_US[] = {
1000000, 250000, 50000, 20000, 10000, 2000
};
const byte COUNT = sizeof(PERIOD_US) / sizeof(PERIOD_US[0]);
byte which = 0;
unsigned long since = 0;
void showPeriod() {
Serial.print("25% duty, period ");
Serial.print(PERIOD_US[which] / 1000);
Serial.print(" ms = ");
Serial.print(1000000.0 / PERIOD_US[which], 0);
Serial.println(" Hz");
}
void setup() {
Serial.begin(9600);
// D13 is not a ~ pin: analogWrite can only turn it off or on.
analogWrite(LAMP, 100); // under 128: plain LOW, lamp off
Serial.println("analogWrite(13, 100): off");
delay(2000);
analogWrite(LAMP, 200); // 128 or more: plain HIGH, lamp on
Serial.println("analogWrite(13, 200): fully on");
delay(2000);
showPeriod();
since = millis();
}
void loop() {
unsigned long per = PERIOD_US[which];
unsigned long into = micros() % per;
digitalWrite(LAMP, into < per / 100 * DUTY_PCT ? HIGH : LOW);
if (millis() - since >= 4000) { // four seconds per speed
since = millis();
which = (which + 1) % COUNT;
showPeriod();
}
}At 1 Hz and 4 Hz you see a blink that is on a quarter of the time. Somewhere past 50 Hz the blink becomes a steady, dim glow. The duty cycle never changed, only the speed.
When it does not work
13 has no PWM timer, so analogWrite cannot pulse it. The Arduino core turns any value under 128 into a plain LOW and 128 or more into a plain HIGH. Fade the lamp in software, as this sketch does, or use one of the six ~ pins.
Software PWM only works while loop() runs freely. A delay() or a long Serial.print in the loop stops the switching, and the lamp holds whatever state it was in. Keep the loop short, or use a ~ pin where a timer does the switching without the sketch.
Most meters average, so they show duty × 5 V: 25% of 5 V is 1.25 V. The pin is never at 1.25 V. A part that needs a real in-between voltage needs a filter after the pin, not analogWrite.
It is 250 ms on and 750 ms off. Watch the Serial Monitor's timestamps, or count: on for a quick beat, then three beats dark.
Which pins have PWM, which timer drives each, and how to measure it with one wire.
The six ~ pins →Edit this page — content/books/atmega328p/what-pwm-is.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.