Modern C++ for microcontrollers lesson plan
The parts of modern C++ that make firmware safer and cost nothing on the chip, measured on an ESP32-C3: constexpr, enum class, std::array, RAII, lambdas and templates without bloat. Ends with a zero-cost pin driver that races digitalWrite.
Objectives and materials
By the end, students can
- Values and names checked at build time.
- Arrays that know their size, and cleanup you cannot forget.
- Lambdas, templates, and what they really cost.
- A pin driver with no run-time cost.
Materials for the whole unit
- ESP32-C3-DevKitM-1
- Full-size breadboard
- LED x 2
- Pushbutton
- Resistor x 2
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.
Let the compiler work
Values and names checked at build time.
| Lesson | Time | What students do |
|---|---|---|
| constexprWork done by the compiler, not the chip: a Fibonacci number that costs nothing at run time against the 21891 calls it takes live, a lookup table built at build time, and static_assert guarding a blink rate. | 45 min |
|
| enum classAn airlock state machine with names that stay in their scope, never turn into an int by accident, fit in one byte, and make the compiler check every switch. | 40 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- LED
- Pushbutton
- Resistor
Check for understanding
Each student finishes "enum class" 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: "Why it matters".
Safe by construction
Arrays that know their size, and cleanup you cannot forget.
| Lesson | Time | What students do |
|---|---|---|
| std::arrayA fixed array that knows its size and costs nothing extra. No decay to a pointer in a function, range-for, std::sort, and what Mokxi's small standard library does instead of throwing. | 45 min |
|
| RAII guardsA constructor takes, a destructor gives back. A lamp guard that survives an early return, a scoped timer that reports itself, and a hand-written version that leaves the lamp on. | 45 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- LED
- Pushbutton
- Resistor
Check for understanding
Each student finishes "RAII guards" 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: "RAII in the wild".
Generic code
Lambdas, templates, and what they really cost.
| Lesson | Time | What students do |
|---|---|---|
| LambdasLittle functions written where they are used: a task table of captureless lambdas, a sort by distance from the Sun, captures by value and by reference, and what each one costs in bytes. | 40 min |
|
| Templates without bloatAn array template of your own in eight lines, ring buffers of three sizes, and what every new size costs in the build log. Then the shared-base trick, measured: less code, a little more RAM. | 40 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- LED
- Pushbutton
- Resistor
Check for understanding
Each student finishes "Templates without bloat" 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: "Rules for templates on a chip".
Project
A pin driver with no run-time cost.
| Lesson | Time | What students do |
|---|---|---|
| Project: a zero-cost pin driverA pin driver where the pin number is part of the type: every call is one register write, it races digitalWrite and wins by more than ten times, and you add the input half and a blinker built from templates. | 45 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- LED x 2
- Pushbutton
- Resistor x 2
Check for understanding
Each student finishes "Project: a zero-cost pin driver" 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: "Your version".
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.