Lesson plan

Timers, PWM and capture on four chips lesson plan

The counter, prescaler, top and compare behind every timer, set up by hand on the Uno, the ESP32-C3’s LEDC, the RP2040’s PWM slices and the STM32’s TIM3, every frequency checked by a counter on the pin; input capture as a reciprocal counter; and one PWM driver with the same calls on all four chips.

4 chapters, 7 lessons, about 6 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • A counter, a prescaler and a compare register, then PWM on the AVR.
  • The ESP32-C3’s LEDC, the RP2040’s slices and the STM32’s TIM3.
  • Input capture, overflow counting and a reciprocal counter.
  • One PWM driver with the same calls on four chips.

Materials for the whole unit

  • Arduino Uno R3
  • ESP32-C3-DevKitM-1
  • Frequency counter x 4
  • LED x 4
  • Raspberry Pi Pico
  • Resistor x 4
  • STM32F411 Black Pill

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 50 min

What a timer is

A counter, a prescaler and a compare register, then PWM on the AVR.

LessonTimeWhat students do
Timer basicsA timer is a counter, a prescaler and a compare register. Timer1 on the Uno toggles pin 9 in hardware once a second and fires an interrupt on every match; one register change makes it sing at 1953 Hz.60 min
  1. Run it: a counter that keeps time
  2. A pin the CPU never touches
  3. An interrupt on every match
  4. Choosing a prescaler
  5. Make it sing
  6. The vocabulary
AVR timersTimer1 in fast PWM mode 14, with ICR1 as the top: 1000 Hz at 25 percent on pin 9 with 2000 steps of resolution, beside analogWrite()’s fixed 490 Hz on pin 3, and then 5 kHz from one register.50 min
  1. Run it: PWM you set yourself
  2. Measure it
  3. What analogWrite() gives you
  4. Frequency against resolution
  5. 5 kHz from one register

Materials

  • Arduino Uno R3
  • Frequency counter x 2
  • LED
  • Resistor

Check for understanding

Each student finishes "AVR timers" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "5 kHz from one register".

Chapter 2. About 2 h 30 min

Three more timer designs

The ESP32-C3’s LEDC, the RP2040’s slices and the STM32’s TIM3.

LessonTimeWhat students do
ESP32 LEDCThe ESP32-C3’s LED PWM controller: a channel on any pin, a frequency and a resolution traded against an 80 MHz clock, a 440 Hz tone on a second pin, and a pair of settings the hardware refuses.50 min
  1. Run it: ask for a frequency
  2. Measure it
  3. A tone
  4. What the hardware refuses
  5. Above hearing
RP2040 PWM slicesThe RP2040’s eight PWM slices by register: a divider, a TOP and two compare values. GP2 and GP3 share slice 1 and so one frequency, each with its own duty, and an 8.4 fixed-point divider doubles the frequency.50 min
  1. Run it: a slice by register
  2. Which pin is which slice
  3. Two pins, one counter
  4. Fixed point
  5. Slices compared
STM32 timersTIM3 on the STM32F411 Black Pill with two PWM channels on PA6 and PA7 sharing one period: PSC and ARR from an 84 MHz clock, a CCR per channel, and center-aligned counting that halves the frequency.50 min
  1. Run it: TIM3, two channels
  2. Pins and alternate functions
  3. Shared period, separate duty
  4. Center-aligned
  5. Four chips in one table

Materials

  • ESP32-C3-DevKitM-1
  • Frequency counter x 2
  • LED
  • Raspberry Pi Pico
  • Resistor
  • STM32F411 Black Pill

Check for understanding

Each student finishes "STM32 timers" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Four chips in one table".

Chapter 3. About 50 min

Measuring

Input capture, overflow counting and a reciprocal counter.

LessonTimeWhat students do
Measuring a frequencyInput capture: Timer1 timestamps every rising edge on pin 8 in hardware, an overflow count makes the time 32 bits wide, and timing many periods at once makes a reciprocal counter that reads 1000.0 Hz, then 50.1 Hz.50 min
  1. Run it: the timer as a stopwatch
  2. Check against the reference
  3. Wider than 16 bits
  4. Measure something slow
  5. Capture everywhere

Materials

  • Arduino Uno R3
  • Frequency counter
  • LED
  • Resistor

Check for understanding

Each student finishes "Measuring a frequency" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Capture everywhere".

Chapter 4. About 1 h

Project

One PWM driver with the same calls on four chips.

LessonTimeWhat students do
Project: one PWM driver, four chipsThe same three calls (pwmBegin, pwmDuty, pwmTone) and the same application on an Uno, an ESP32-C3, a Pico and a Black Pill at once: a breathing lamp at 1 kHz and a 440 Hz tone, each driver on its own chip’s timers.60 min
  1. Run it: four boards, one program
  2. The Uno’s driver
  3. The ESP32-C3 and the Pico
  4. The Black Pill
  5. Change the application, once
  6. Design the API

Materials

  • Arduino Uno R3
  • ESP32-C3-DevKitM-1
  • Frequency counter x 4
  • LED x 4
  • Raspberry Pi Pico
  • Resistor x 4
  • STM32F411 Black Pill

Check for understanding

Each student finishes "Project: one PWM driver, four chips" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Design the API".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.