Pi hardware in Python: I2C, SPI and PWM lesson plan
The Raspberry Pi’s hardware from Python: gpiozero beyond blinking, I2C sensors with smbus2, an SPI ADC with spidev, a servo gauge needle, your own OLED driver and callbacks, ending in a Pi weather terminal.
Objectives and materials
By the end, students can
- gpiozero’s devices, and a sensor register by register.
- An ADC over SPI, and a servo needle on PWM.
- Your own OLED driver, and code the hardware calls.
- A Pi weather terminal from the whole bench.
Materials for the whole unit
- Eight-channel ADC, MCP3008
- Hobby servo
- LED
- OLED display, 128x64
- Potentiometer
- Pressure, humidity and temperature, BME280
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor
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.
Devices and I2C
gpiozero’s devices, and a sensor register by register.
| Lesson | Time | What students do |
|---|---|---|
| gpiozero beyond basicsDevices with a value, not just on and off: PWMLED brightness, blink a set number of times, a pulse that runs in the background, and devices wired to each other with source. | 55 min |
|
| smbus2 sensorsTalk to an I2C chip yourself: scan the bus as i2cdetect does, read one register and a block of them from a BME280, let a driver do the datasheet’s math, and read the error when nobody answers. | 55 min |
|
Materials
- Eight-channel ADC, MCP3008
- Hobby servo
- LED
- OLED display, 128x64
- Potentiometer
- Pressure, humidity and temperature, BME280
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor
Check for understanding
Each student finishes "smbus2 sensors" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Reading a datasheet".
SPI and PWM
An ADC over SPI, and a servo needle on PWM.
| Lesson | Time | What students do |
|---|---|---|
| spidev ADCThe Pi has no analog inputs, so an MCP3008 converts for it over SPI. Send the three bytes yourself with spidev and decode the 10-bit answer, then let gpiozero’s MCP3008 do the same in one line. | 40 min |
|
| PWM servos: a gauge needleA servo as a barometer’s needle: a 50 Hz pulse from 1 ms to 2 ms sets its angle, a duty cycle from 5 to 10 percent with RPi.GPIO PWM, and air pressure mapped onto 180 degrees. | 55 min |
|
Materials
- Eight-channel ADC, MCP3008
- Hobby servo
- LED
- OLED display, 128x64
- Potentiometer
- Pressure, humidity and temperature, BME280
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor
Check for understanding
Each student finishes "PWM servos: a gauge needle" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Power for servos".
Drivers and events
Your own OLED driver, and code the hardware calls.
| Lesson | Time | What students do |
|---|---|---|
| An OLED driver in PythonWrite the driver yourself: the SSD1306’s setup commands, a 1024-byte framebuffer where each byte is eight stacked pixels, pixel() with bit arithmetic, and show() in 16-byte I2C writes. Then a bar graph of the knob. | 45 min |
|
| CallbacksLet the button call your code: when_pressed, when_released and when_held with hold_time, a counter shared with the callbacks, and a main loop that stays free the whole time. | 55 min |
|
Materials
- Eight-channel ADC, MCP3008
- Hobby servo
- LED
- OLED display, 128x64
- Potentiometer
- Pressure, humidity and temperature, BME280
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor
Check for understanding
Each student finishes "Callbacks" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Callbacks and bounce".
Boss project
A Pi weather terminal from the whole bench.
| Lesson | Time | What students do |
|---|---|---|
| Boss: a Pi weather terminalThe whole bench as one device: BME280 readings on the OLED, a knob that turns the pages, a servo pressure needle, a damp-air light, and a button that logs readings to a CSV file. | 60 min |
|
Materials
- Eight-channel ADC, MCP3008
- Hobby servo
- LED
- OLED display, 128x64
- Potentiometer
- Pressure, humidity and temperature, BME280
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor
Check for understanding
Each student finishes "Boss: a Pi weather terminal" on their own, without opening the hints. The simulator checks 8 of its 8 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Your own damp line".
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.