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.
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.
What a timer is
A counter, a prescaler and a compare register, then PWM on the AVR.
| Lesson | Time | What 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 |
|
| 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 |
|
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".
Three more timer designs
The ESP32-C3’s LEDC, the RP2040’s slices and the STM32’s TIM3.
| Lesson | Time | What 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 |
|
| 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 |
|
| 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 |
|
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".
Measuring
Input capture, overflow counting and a reciprocal counter.
| Lesson | Time | What 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 |
|
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".
Project
One PWM driver with the same calls on four chips.
| Lesson | Time | What 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 |
|
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".
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.