Robots in Python on the Pi lesson plan
Build an exploring rover on a Raspberry Pi 4 in Python: gpiozero Robot and the L298N underneath, sonar and line sensors, a state machine, a flight recorder, and an autonomous bot that runs a course on its own.
Objectives and materials
By the end, students can
- Drive the rover with gpiozero, then open up the motor driver underneath: H-bridge, encoder and PWM.
- Sonar for obstacles and infrared for the line, each turned into a number the rover steers by.
- States and transitions that never freeze, and a log of every run.
- One autonomous rover that follows, waits, searches, finishes and fails safe.
Materials for the whole unit
- Line tracker, 5 channel
- Motor driver module, L298N
- Raspberry Pi 4 Model B
- Resistor x 2
- Robot car chassis, 2WD
- 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.
Make it move
Drive the rover with gpiozero, then open up the motor driver underneath: H-bridge, encoder and PWM.
| Lesson | Time | What students do |
|---|---|---|
| gpiozero RobotMeet the rover: a Pi 4, an L298N and a two-wheel chassis. Drive it with gpiozero's Robot: forward, backward, spin on the spot, curve at part speed, and set both wheels at once with a value pair to drive a square. | 40 min |
|
| Motor driversOpen up the L298N: drive one wheel with three plain pins, reverse it through the H-bridge, measure how far it rolls with the wheel's encoder, set the speed with PWM on the enable pin, and see that gpiozero's Motor is exactly those three pins. | 40 min |
|
Materials
- Line tracker, 5 channel
- Motor driver module, L298N
- Raspberry Pi 4 Model B
- Resistor x 2
- Robot car chassis, 2WD
- Ultrasonic ranger
Check for understanding
Each student finishes "Motor drivers" 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: "It was a Motor all along".
Sense the world
Sonar for obstacles and infrared for the line, each turned into a number the rover steers by.
| Lesson | Time | What students do |
|---|---|---|
| Distance sensingGive the rover eyes: read the HC-SR04 with DistanceSensor, time its echo by hand to see where the number comes from, stop before a wall, slow down in proportion to the gap, and back away and turn when blocked. | 40 min |
|
| Line sensingRead five infrared line sensors as a pattern, turn the pattern into the tape's position in millimeters, steer with if and else, then steer smoothly in proportion to the error, and stop at the finish bar. | 40 min |
|
Materials
- Line tracker, 5 channel
- Motor driver module, L298N
- Raspberry Pi 4 Model B
- Resistor x 2
- Robot car chassis, 2WD
- Ultrasonic ranger
Check for understanding
Each student finishes "Line sensing" 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: "The finish bar".
Think and remember
States and transitions that never freeze, and a log of every run.
| Lesson | Time | What students do |
|---|---|---|
| A state machineOrganize a robot's behavior as states and transitions: explore, avoid on a timer without freezing, back up when too close, then turn the whole thing into a table where a new behavior, coming home to base, is one more row. | 40 min |
|
| Logging a runMake the rover keep a flight recorder: measure distance from its wheel encoders, write a timestamped CSV while it drives, work out its speed from the log, flag the odd moments with the logging module, and save a JSON summary. | 40 min |
|
Materials
- Line tracker, 5 channel
- Motor driver module, L298N
- Raspberry Pi 4 Model B
- Resistor x 2
- Robot car chassis, 2WD
- Ultrasonic ranger
Check for understanding
Each student finishes "Logging a run" 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 summary to share".
Boss
One autonomous rover that follows, waits, searches, finishes and fails safe.
| Lesson | Time | What students do |
|---|---|---|
| Boss: an autonomous Pi botPut it all together in one autonomous rover: follow the tape with a proportional controller, wait for obstacles with hysteresis, search for a lost line, stop at the finish bar with a mission report, and fail safe with a timeout and try/finally. | 50 min |
|
Materials
- Line tracker, 5 channel
- Motor driver module, L298N
- Raspberry Pi 4 Model B
- Resistor x 2
- Robot car chassis, 2WD
- Ultrasonic ranger
Check for understanding
Each student finishes "Boss: an autonomous Pi bot" 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: "Run the course".
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.