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.
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.
Outputs
Pins high and low, PWM, and signals you can see and hear.
| Lesson | Time | What students do |
|---|---|---|
| LEDs and PWMDrive 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 |
|
| Buttons and eventsRead 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 |
|
| Buzzers and tunesMake 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 |
|
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".
Motion and sensing
Pulses that set an angle, echoes that measure, and heat that moves.
| Lesson | Time | What students do |
|---|---|---|
| ServosAim 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 |
|
| Distance sensorBuild 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 |
|
| Motion sensorWatch 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 |
|
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".
Project
Sensing, timing and state in one build: the desk alarm.
| Lesson | Time | What students do |
|---|---|---|
| Project: a desk alarmThe 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 |
|
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".
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.