Source: https://mokxi.com/teachers/plans/python-robotics-on-the-pi
Updated: 2026-11-19

Lesson plan

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

4 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

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

Chapter 1. About 1 h 20 min

## 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 Robot Meet 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

- Run it: roll out
- Reverse
- Spin on the spot
- Part speed and curves
- Drive a square

Motor drivers Open 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

- Run it: one wheel by hand
- Reverse through the H-bridge
- How far does it roll?
- Speed with PWM
- It was a Motor all along

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

Chapter 2. About 1 h 20 min

## 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 sensing Give 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

- Run it: how far?
- Where the number comes from
- Stop before the wall
- Slow down smoothly
- Back away and turn

Line sensing Read 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

- Run it: five eyes on the floor
- Where is the line?
- Steer: if and else
- Steer in proportion
- The finish bar

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

Chapter 3. About 1 h 20 min

## Think and remember

States and transitions that never freeze, and a log of every run.

Lesson | Time | What students do

A state machine Organize 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

- Run it: one state
- A second state: AVOID
- A timer that does not freeze
- Too close: BACKUP
- The machine as a table

Logging a run Make 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

- Run it: odometry
- A flight recorder: CSV
- Speed from the log
- Levels: logging what matters
- A summary to share

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

Chapter 4. About 50 min

## Boss

One autonomous rover that follows, waits, searches, finishes and fails safe.

Lesson | Time | What students do

Boss: an autonomous Pi bot Put 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

- Run it: the mission skeleton
- WAIT, with hysteresis
- SEARCH for a lost line
- DONE, with a mission report
- Fail safe
- Run the course

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

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.
