Lesson plan

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.

4 chapters, 7 lessons, about 6 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

The unit

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.

Chapter 1. About 1 h 50 min

Devices and I2C

gpiozero’s devices, and a sensor register by register.

LessonTimeWhat 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
  1. Run it: the weather bench
  2. Full brightness
  3. Blink a set number of times
  4. A job in the background
  5. Devices wired together
  6. Five blinks
  7. Clean up
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
  1. Run it: who is on the bus?
  2. One register
  3. A block of registers
  4. A driver on top
  5. Warm it
  6. Nobody answers
  7. Reading a datasheet

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

Chapter 2. About 1 h 35 min

SPI and PWM

An ADC over SPI, and a servo needle on PWM.

LessonTimeWhat 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
  1. Run it: a knob the Pi can read
  2. Three bytes each way
  3. gpiozero does it for you
  4. Turn it up
  5. SPI or I2C
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
  1. Run it: a test sweep
  2. The other end
  3. Pressure on a dial
  4. Where the horn really is
  5. A storm coming
  6. Rescale the dial
  7. Power for servos

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

Chapter 3. About 1 h 40 min

Drivers and events

Your own OLED driver, and code the hardware calls.

LessonTimeWhat 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
  1. Run it: our own driver
  2. Commands to wake it
  3. A pixel is a bit
  4. Turn the knob
  5. A taller bar
  6. From here to a full driver
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
  1. Run it: a free main loop
  2. Press
  3. Release
  4. Hold
  5. The loop never noticed
  6. A quicker hold
  7. Callbacks and bounce

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

Chapter 4. About 1 h

Boss project

A Pi weather terminal from the whole bench.

LessonTimeWhat 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
  1. Run it: the weather terminal
  2. Pages on a knob
  3. Turn to temperature
  4. Turn to pressure
  5. The needle
  6. Damp air
  7. Log a reading
  8. Your own damp line

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

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.