Source: https://mokxi.com/teachers/plans/python-hardware-playground
Updated: 2026-11-19

Lesson plan

# Python on the Pi's pins lesson plan

Drive real parts from Python with gpiozero: LEDs and PWM, buttons and events, buzzer signals and tunes, servos, a sonar and a motion sensor, then build a desk alarm as a state machine.

3 chapters, 7 lessons, about 5 hours, on the Raspberry Pi 4 Model B. 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

- Pins high and low, PWM, and signals you can see and hear.
- Pulses that set an angle, echoes that measure, and heat that moves.
- Sensing, timing and state in one build: the desk alarm.

### Materials for the whole unit

- Half breadboard
- Hobby servo
- LED x 2
- Motion sensor
- Pushbutton
- Raspberry Pi 4 Model B
- Raspberry Pi 5
- Resistor x 3
- Traffic light module
- Ultrasonic ranger

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 2 h 20 min

## Outputs

Pins high and low, PWM, and signals you can see and hear.

Lesson | Time | What students do

LEDs and PWM Drive the launch pad lights with gpiozero: on and off, a countdown that blinks, blink() running in the background, PWM for brightness, the duty cycle worked out in milliseconds, and pulse() for a breathing engine glow. | 50 min

- Run it: a pin you control
- A countdown
- Blink in the background
- Dim it with PWM
- Count the milliseconds
- Breathing engines

Buttons and events Read a pushbutton three ways: polling is_pressed in a loop, counting presses by watching for the change, and letting gpiozero call your functions with when_pressed and when_held; then build a reaction timer. | 45 min

- Run it: polling
- Count the presses
- Events: let it call you
- Hold to abort
- A reaction timer

Buzzers and tunes Make ship alarms with gpiozero: a Buzzer on and off, beep() in the background, Morse code sent from a dict, the math that turns note names into frequencies, and a rising tune on a TonalBuzzer, with the circuit's LED standing in for the buzzer so you can see every signal. | 45 min

- Run it: a buzzer is a pin
- Beep in the background
- Morse code
- Notes are numbers
- A tune

### Materials

- Half breadboard
- LED x 2
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor x 2

### Check for understanding

Each student finishes "Buzzers and tunes" 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: "A tune".

Chapter 2. About 1 h 55 min

## Motion and sensing

Pulses that set an angle, echoes that measure, and heat that moves.

Lesson | Time | What students do

Servos Aim an antenna dish with a hobby servo: Servo min and max, why its default pulses miss this servo's range, AngularServo with the right pulse widths, angles typed in the Serial Monitor and checked first, and a sweep that ends with detach(). | 35 min

- Run it: pulses become angles
- The right pulse widths
- Aim it by hand
- Sweep the sky

Distance sensor Build a sonar with an HC-SR04 and gpiozero's DistanceSensor: read the distance, move the target and see it change, light a warning when something comes within 20 cm, draw a bar graph that grows as it nears, and work out the echo times from the speed of sound, in air and under the sea. | 45 min

- Run it: a ping and an echo
- Move the target
- Too close!
- A bar graph
- The physics of the echo

Motion sensor Watch an airlock with a PIR sensor: wait for motion and stillness, switch to when_motion events so the program is free, log every visit with a time, and add a cooldown so one person does not raise ten alerts. | 35 min

- Run it: wait for a warm body
- Events, not waiting
- A log with times
- A cooldown

### Materials

- Half breadboard
- Hobby servo
- LED
- Motion sensor
- Raspberry Pi 4 Model B
- Raspberry Pi 5
- Resistor x 3
- Ultrasonic ranger

### Check for understanding

Each student finishes "Motion sensor" on their own, without opening the hints. The simulator checks 4 of its 4 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A cooldown".

Chapter 3. About 50 min

## Project

Sensing, timing and state in one build: the desk alarm.

Lesson | Time | What students do

Project: a desk alarm The finale: a drawer alarm as a state machine. Arm it from the Serial Monitor, let the drawer switch trigger an entry delay, sound a flashing siren when time runs out, stand it down with a secret knock on the same switch, and keep a timed log of what happened. | 50 min

- Run it: states
- The drawer opens
- The siren
- The secret knock
- A timed log
- Make it yours

### Materials

- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module

### Check for understanding

Each student finishes "Project: a desk alarm" 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: "Make it yours".

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.
