ATmega328P/PWM/27. The six ~ pins
PWM · 27 of 33

The six ~ pins

Six pins can do PWM: 3, 5, 6, 9, 10 and 11, the ones marked ~ on the UNO and on the Nano's PinPulse shield. Three timers drive them in pairs, 5 and 6 at 976.6 Hz and the other four at 490.2 Hz, and one jumper wire into D8 lets the board measure each one itself.

Your board

Three timers, two pins each

A PWM pin is not a feature of the pin. It belongs to a timer, a counter inside the chip that counts the 16 MHz clock and flips an output pin when it passes a number you set. The ATmega328P has three timers, and each drives two pins. The six marked ~ on the silkscreen are those pins.

PinTimerFrequencyAlso used by
~5, ~6Timer0976.6 Hzmillis(), micros(), delay()
~9, ~10Timer1490.2 Hzthe Servo library
~3, ~11Timer2490.2 Hztone()
Six pins, three timers, two speeds
Timer1, 490.2 Hz
Pin
Timer
Timer1
Frequency
490.2 Hz
Period
2.04 ms
~9 runs at 490.2 Hz, on Timer1, with its partner ~10. Two pins on one timer always share one frequency; each keeps its own duty. Timer1 is also what the Servo library uses, so when that runs, both pins lose their PWM.

Two pins on one timer share one frequency, because they share one counter. Each keeps its own duty: the counter is compared against two numbers, one per pin.

Why two speeds

The Arduino core sets all three timers to count at 16 MHz ÷ 64 = 250 kHz. Timer0 counts 0 to 255 and starts again, 256 counts a cycle: 976.6 Hz. Timer1 and Timer2 count up to 255 and back down, 510 counts a cycle: 490.2 Hz. The second way keeps the middle of each pulse in the same place, which matters to motors more than to lamps.

Timer0 runs the faster way because it also keeps time. Every time it overflows, 1.024 ms, the core adds to the count that millis() and micros() report. That is why Timer0 is the one timer you never retune, and the last article of this chapter is about what happens to the others.

Measuring it with one wire

pulseIn(pin, HIGH) waits for the pin to go HIGH and returns how many microseconds it stays there; pulseIn(pin, LOW) does the same for LOW. Add the two and you have the period, and 1,000,000 ÷ period is the frequency.

The sketch drives all six ~ pins at once, each with its own value, and makes D8 an INPUT_PULLUP to listen. One jumper wire from a ~ pin to 8 joins an output to an input, which is safe: the pin drives, D8 only listens. With the wire on ~9, the Serial Monitor shows about 1020 µs HIGH, 1020 µs LOW, 490 Hz and 50%. Move it to ~5 and the frequency doubles. Put it on 13, which has no tilde, and it reports no pulses at all.

The code

Measure_Pwm_Pins.ino

It puts a different duty on each of the six ~ pins and uses pulseIn on D8 to time whatever arrives. Plug one jumper wire from 9 to 8, open the Serial Monitor, then move the wire's other end from ~ pin to ~ pin, leaving the end in 8.

// Measure_Pwm_Pins.ino — D8 times the ~ pin wired to it
// 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: one jumper wire from a ~ pin (3, 5, 6, 9, 10 or 11) to 8.
//   The ~ pins are outputs, D8 is the input. Move the wire's ~ end
//   from pin to pin; never put it on GND, 5V or 3V3.

const byte PWM_PINS[] = {3, 5, 6, 9, 10, 11};
const byte VALUES[]   = {32, 64, 96, 128, 160, 192};
const byte PROBE = 8;

void setup() {
  Serial.begin(9600);
  pinMode(PROBE, INPUT_PULLUP);   // no wire: reads a steady HIGH
  for (byte i = 0; i < 6; i++) analogWrite(PWM_PINS[i], VALUES[i]);
}

void loop() {
  unsigned long hi = pulseIn(PROBE, HIGH, 20000);   // us, or 0
  unsigned long lo = pulseIn(PROBE, LOW, 20000);
  if (hi == 0 || lo == 0) {
    Serial.println("no pulses on 8: is the wire on a ~ pin?");
    delay(1000);
    return;
  }
  float hz = 1000000.0 / (hi + lo);
  float duty = 100.0 * hi / (hi + lo);

  // Each pin was given its own value, so the duty says which it is.
  byte best = 0;
  for (byte i = 1; i < 6; i++) {
    if (abs(duty - VALUES[i] / 2.55) < abs(duty - VALUES[best] / 2.55))
      best = i;
  }
  Serial.print("HIGH ");
  Serial.print(hi);
  Serial.print(" us, LOW ");
  Serial.print(lo);
  Serial.print(" us = ");
  Serial.print(hz, 1);
  Serial.print(" Hz, ");
  Serial.print(duty, 1);
  Serial.print("% duty: that is ~");
  Serial.println(PWM_PINS[best]);
  delay(1000);
}

~9 should print about 490 Hz at 50%, and ~5 about 976 Hz at 25%. The sketch names the pin from the duty, so you can check it is the one you think. Never move the wire's ~ end onto GND, 5V or 3V3: the ~ pins are outputs.

When it does not work

It prints no pulses on 8

The wire is not on a ~ pin, or not in 8. pulseIn waits 20 ms for an edge and gives up with 0; a pin with no PWM never makes one. Check that it is 3, 5, 6, 9, 10 or 11, and that the other end of the wire is in 8, not 7.

~5 and ~6 read a little above their duty

Timer0 runs fast PWM, where a value of v is HIGH for v + 1 counts of 256; the other two timers count v of 255. 64 is 25.4% on ~5 and 25.1% on ~9. The difference is real and too small to see.

The frequency is off by a hertz or two

pulseIn counts in loops of the processor, with a few microseconds of overhead, and the HIGH and LOW are measured from two different cycles. The timer itself runs from the board's 16 MHz clock (a crystal on the UNO, a ceramic resonator on the Nano) and is exact to that part's tolerance.

analogWrite works on 11 until SPI starts

10 and 11 are also the SPI pins, and 3 is an interrupt pin. A pin can do one job at a time, so a project that uses SPI loses 11 (and, in practice, 10) for PWM. Plan PWM onto the pins nothing else wants: 5, 6 and 9.

Where this goes next

A fade that looks even to the eye, done in software on D13.

Fade the pin-13 lamp →

Edit this page — content/books/atmega328p/the-six-pwm-pins.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.

Browse Modules and blocks on the forum →