Source: https://mokxi.com/teachers/plans/cpp-modern-embedded
Updated: 2026-11-19

Lesson plan

# 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.

4 chapters, 7 lessons, about 5 hours, on the ESP32-C3-DevKitM-1. Every lesson runs in the browser, with nothing to install.

Start a class from this course

The unit

## 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.

Chapter 1. About 1 h 25 min

## Let the compiler work

Values and names checked at build time.

Lesson | Time | What students do

constexpr Work 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

- Run it: the compiler did the math
- The same sum at run time
- A table built at build time
- Ask the compiler for more
- A blink rate checked at build time
- Let static_assert say no

enum class An 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

- Run it: a one-byte state
- What a plain enum gets wrong
- Cycle the airlock
- Add a state
- Why it matters

### 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".

Chapter 2. About 1 h 30 min

## Safe by construction

Arrays that know their size, and cleanup you cannot forget.

Lesson | Time | What students do

std::array A 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

- Run it: temperatures in a std::array
- It costs nothing
- No decay
- at() without exceptions
- One number to change
- Sort it

RAII guards A 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

- Run it: a guard in braces
- The early return
- A timer that reports itself
- Time the transmit
- Fix the legacy send
- RAII in the wild

### 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".

Chapter 3. About 1 h 20 min

## Generic code

Lambdas, templates, and what they really cost.

Lesson | Time | What students do

Lambdas Little 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

- Run it: sorted by a lambda
- A task table
- Captures and their cost
- Flip the order
- Add a task

Templates without bloat An 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

- Run it: an array of your own
- Three sizes, three copies
- One more size
- Share the code
- Rules for templates on a chip

### 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".

Chapter 4. About 45 min

## Project

A pin driver with no run-time cost.

Lesson | Time | What students do

Project: a zero-cost pin driver A 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

- Run it: OutputPin<8>
- The race
- Nothing to store
- Write the input half
- Faster heartbeat
- Your version

### 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".

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.

Start a class from this course See the demo class

More units

## Other lesson plans

Electronics basics 20 lessons, about 10 h. Embedded on the Arduino Uno 16 lessons, about 12 h. Embedded on the ESP32-C3 13 lessons, about 11 h. Embedded on the Raspberry Pi Pico 7 lessons, about 6 h. Build your own fight stick 5 lessons, about 4 h. Embedded on the STM32F411 6 lessons, about 6 h. C++ on a board 13 lessons, about 10 h. Digital logic and memory 4 lessons, about 4 h. Interactive devices on the Uno 9 lessons, about 8 h. From gates to a computer 23 lessons, about 16 h. Build a claw machine 8 lessons, about 6 h. Every unit All the courses as ready-made units, on the teachers page.
