16 MHz÷644 µsTCNT002550OCR0A= ?6401PIN 6R1analogWrite(6, 64);2analogWrite(6, 192);3for (int v=0; v<256; v++){4 analogWrite(6, v);5 delay(4);6}DUTY CYCLE0%100%100% · avg 5.00 V64192
01.0242.0483.07202.55time (ms)VPIN 6 VOLTAGEpinaverage
Back: Timer0's PWM path on an Uno (clock, prescaler, counter, compare register, pin 6, resistor, LED)Front: your sketch and the duty cycle; below: pin 6's voltage over three periodsTokens: the values analogWrite() writes into OCR0APWM on an Arduino Uno: Timer0's counter TCNT0 counts 0 to 255 every 1,024 µs (976.5625 Hz). Pin 6 goes HIGH at 0 and LOW when the count matches OCR0A, all in hardware. analogWrite(6, value) only writes the value into OCR0A: 64 gives a duty cycle of 25.4% (average 1.27 V), 192 gives 75.4% (3.77 V). The LED is always fully on or fully off; switched fast enough, it looks steady at a brightness set by the duty cycle. The motion is slowed down a great deal.

The idea

  1. A digital pin has two states. HIGH puts 5 V on pin 6 and the LED shines fully. LOW puts 0 V on it and the LED goes dark. There is no half-way setting. So how do we dim it?

The timer makes the wave

analogWrite() in your sketch

What happens

;; step 1/10 · onoff
digitalWrite(6, HIGH);   ->  5 V, LED fully on
digitalWrite(6, LOW);    ->  0 V, LED off
nothing in between: a digital pin is a switch

On pins 5 and 6 the Uno uses fast PWM, so the pin is HIGH for value + 1 ticks: analogWrite(6, 64) gives 65/256 = 25.4%, not 64/255. That is why the Arduino reference warns these pins have "higher-than-expected duty cycles". Values 0 and 255 skip the timer and use digitalWrite. The LED really is blinking: fully on, then fully off, about a thousand times a second. The Arduino PWM guide puts it simply: repeat the on-off pattern fast enough and "the result is as if the signal is a steady voltage". The figure slows the timer down a great deal so you can watch it.