Look below the sketch

You can skip this page. It shows what analogWrite does inside the chip.

Inside the chip, a counter called Timer1 makes pin 9’s on-⁠off pattern4.

Timer1 counts from 0 up to 255 and back down, over and over2. Pin 9 is on while the count is below OCR1A, and off above it2. So OCR1A sets the share of time the pin is on2.

Timer1's count and the pin 9 output for OCR1A = 64, 128 and 192The count climbs from 0 to 255 and falls back to 0 once every 2.04 ms. Pin 9 is HIGH while the count is below OCR1A, so each pulse is centred on the count's lowest point: HIGH 25.1%, 50.2%, 75.3% of each cycle at OCR1A = 64, 128, 192.TCNT1TOP025564128192BOTTOMBOTTOMOCR1AHIGH6425.1%12850.2%19275.3%one period: 510 ticks × 4 µs = 2.04 msTimer1's count and the pin 9 output for OCR1A = 64, 128 and 192The count climbs from 0 to 255 and falls back to 0 once every 2.04 ms. Pin 9 is HIGH while the count is below OCR1A, so each pulse is centred on the count's lowest point: HIGH 25.1%, 50.2%, 75.3% of each cycle at OCR1A = 64, 128, 192.TCNT1TOP025564128192BOTTOMBOTTOMOCR1AHIGH6425.1%12850.2%19275.3%period 2.04 ms
Timer1 in 8-⁠bit phase-⁠correct PWM. The count climbs to TOP and falls back to BOTTOM. With COM1A1 set, pin 9 is cleared when the count passes OCR1A on the way up and set again on the way down, so every HIGH pulse is centred on BOTTOM and lasts OCR1A ÷ 255 of the cycle2. One cycle is 510 ticks; at 16 MHz with Arduino’s prescaler of 64, a tick is 4 µs, so the period is 2.04 ms3, 5.

Set it up with registers

TCCR1A and TCCR1B are registers that pick how Timer1 runs2. In TCCR1A, WGM10 picks the count up to 255 and back. COM1A1 makes the count switch pin 9 that way2. In TCCR1B, CS11 and CS10 set the prescaler to 643. So pin 9 repeats about 16 MHz ÷ (2 × 64 × 255) = 490 times a second2, 5. Arduino’s startup code sets Timer1 up the same way3.

Two ways

l3-fade-arduino.ino
// Lesson 3, Arduino version: the knob on A0 sets how
// bright the LED on pin 9 is.
void setup() {
  pinMode(9, OUTPUT);
}

void loop() {
  int reading = analogRead(A0);  // 0 to 1023
  analogWrite(9, reading / 4);   // 0 to 255
}
l3-fade-registers.ino
// Lesson 3, register version: Timer1 drives pin 9 (OC1A)
// directly.
void setup() {
  DDRB |= (1 << DDB1);  // pin 9 (PB1, OC1A) is an output
  // Non-inverting PWM on OC1A, 8-bit phase-correct (mode 1)
  TCCR1A = (1 << COM1A1) | (1 << WGM10);
  // Timer clock = 16 MHz / 64
  TCCR1B = (1 << CS11) | (1 << CS10);
}

void loop() {
  // Still read with analogRead: this lesson is about PWM
  int reading = analogRead(A0);
  OCR1A = reading / 4;  // duty = OCR1A / 255
}

Registers in this lesson (ATmega328P data-⁠space addresses)2

RegisterAddressBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
TCCR1A0x80COM1A1COM1A0COM1B1COM1B0––WGM11WGM10
TCCR1B0x81ICNC1ICES1–WGM13WGM12CS12CS11CS10
OCR1AL0x88bit 7bit 6bit 5bit 4bit 3bit 2bit 1bit 0

Watch it

Switch between the two programs. At 1.25 V, OCR1A is 64, so pin 9 is on about 25% of the time2.

Sources for this page

  1. Microchip ATmega328P datasheet DS40002061A: Timer1 PWM frequency (p135), extreme values (p136), Tables 16-3 and 16-4 (p141); ADC result (p256)
  2. ArduinoCore-⁠avr, cores/arduino/wiring.c: Timer1 prescaler 64, 8-⁠bit phase-⁠correct PWM
  3. ArduinoCore-⁠avr, variants/standard/pins_arduino.h: pin 9 is TIMER1A
  4. Arduino UNO R3 technical specifications: Clock speed: ATmega328P 16 MHz; DC Current per I/O Pin: 20 mA