Skip to content

C++ Hardware Programming

Start with a program that prints text, then add one circuit at a time. The course separates software compilation, Linux device access and electrical behaviour so a failure can be traced to the right layer.

Use the chapter-end practice tasks and understanding checks as you work through the course. Physical circuits are not required for these software and reasoning exercises.

Before you start

You need a working Raspberry Pi OS login and the Basic Operations course. No electronics are required for the first lesson or the programs' planning modes.

The examples use C++17. LED/button programs use the Linux GPIO character-device v2 API, not a mixture of WiringPi, libgpiod bindings and old ioctl examples. Hardware PWM uses its own kernel subsystem, not the slow GPIO blink loop.

Pi 4B is the software-validation board. Standard Pi 4/5 header positions are stated explicitly, but Linux chip enumeration and PWM routing must be identified on the actual board. The PWM boot-configuration example is specifically for Pi 4B, not an unverified Pi 5 recipe.

Follow the hardware learning path

Lesson Learn Check before continuing
Part 1: Development Setup · GitHub Save, compile, run and read diagnostics A text program prints the expected message
Part 2: LED Output · GitHub GPIO numbering, series resistor and a line request Chip/wiring identified; planning mode understood before output
Part 3: Button Input · GitHub Pull-up, active-low input and stable-state debounce Contact pairs and accepted transitions understood
Part 4: Buzzer and Hardware PWM · GitHub Period, duty, driver routing and finite cleanup Compatible passive piezo and confirmed PWM controller
Part 5: Positional Servo · GitHub External power, pulse units and calibration Nominal centre and small safe changes, not an endpoint sweep
Part 6: Continuous Servo · GitHub Neutral, direction and open-loop speed Neutral observed while pulses are active before rotation
Part 7: SSD1306 OLED · GitHub I2C wiring, device access and a monochrome framebuffer Correct module and address; HELLO PI visually checked after output
Part 8: SSD1306 Title Animation · GitHub Deterministic particles, frame pacing and shutdown Preview first; a finite run turns the verified display off

The hardware checks require your actual circuit. Dry-run output is not proof that a wire, waveform or mechanism works. Each lesson distinguishes what was software-tested from physical tests not performed for this revision.

Get the samples from GitHub

The C++ Hardware Programming samples provide a source file, a CMake target and a README for each lesson. Use the GitHub links in the table above to open the matching example rather than searching the repository.

For a full build, follow the repository's CMake build and test instructions. That route requires Linux, CMake 3.28 or newer, Make and a C++17 compiler. The articles' direct g++ commands remain an alternative and do not require CMake.

Repository sources are named main.cpp; CMake target names such as hello_world can differ from an article's manually built executable such as hello. Each lesson links its target and source explicitly. The servo lessons share the PWM executable. Build/test and dry-run success do not prove physical hardware operation.

Prepare components when you reach them

  • LED lesson: low-current indicator LED, 1 kΩ series resistor, breadboard and wires.
  • Button lesson: a normally open unpowered momentary switch.
  • PWM lesson: documented low-current, 3.3 V-compatible passive piezo circuit.
  • Servo lessons: exact documented motor type, external regulated supply and a mechanically safe bench setup.
  • OLED lesson: a documented 3.3 V-compatible, four-pin, 128×64 SSD1306 I2C module and four wires.

Do not buy an arbitrary “sensor kit” and assume every item can connect directly to a signal pin. Read the required voltage/current and power wiring first.

The checkpoints matter more than copying commands

Before enabling any hardware output, explain the physical pin, signal name, Linux controller and what the program does on exit. Read error messages and stop on a mismatch; do not run as root merely to avoid understanding permissions.

Programs default to a no-hardware planning mode. GPIO/PWM examples use --apply and the OLED example uses --show to enable output explicitly after checks, not as commands to paste automatically.

When changing a circuit, shut down and disconnect all relevant power supplies. An external motor supply can keep a circuit powered even when the Pi is unplugged.

If an exercise fails

Compile a text-only program again to confirm the software environment, then inspect the relevant device and permissions. Check wiring with power removed. Change one layer at a time.

A chip path is not portable merely because the GPIO number is the same. A motor that does not move is not proof that its pulse width should be increased.

Next step

Start with Development Setup. After completing the course, consider Modern C++ Masterclass for language depth or Tips for project ideas. Motor feedback, watchdogs and fault-safe power control are further design work, not provided by these bench examples.

Donate