Class 3 · The Pi takes over¶
Until now a laptop on a USB cable has been the car's boss. Today the boss moves onto the car: a Raspberry Pi 5 rides on top, talks to the Uno over USB with the commands you wrote in Classes 1 and 2, and gets a webcam. You'll drive the car from your laptop over Wi-Fi, watching what it sees.
By the end of class
You drive the car from across the room with your laptop's arrow keys while watching its camera.
New parts today¶
| Part | What it's for |
|---|---|
| Raspberry Pi 5 + active cooler + microSD | The car's second brain, running Python |
| USB-C PD power bank | Powers the Pi; the Pi powers the Uno |
| USB webcam | The car's eye |
Build it¶
- Fit the active cooler to the Pi and put in the microSD card you flashed before Class 1.
- Mount the Pi on standoffs above the Uno, and the power bank where the car stays balanced. Strap the power bank down: it's the heaviest thing on the car.
- Mount the webcam at the front, looking straight ahead, with its lens about 8 cm above the floor. That's where the AprilTags will be in Class 4.
- Connect: power bank → Pi's USB-C port. Uno's USB cable → one of the Pi's USB ports. Webcam → another USB port.
- The AA pack stays where it is, still powering only the motors.
The Pi 5 on a power bank
The Pi 5 wants 5 V at 5 A. Most power banks give 5 V at 3 A, so the Pi warns you at startup and limits its USB ports to 600 mA in total. That's usually enough for the Uno and a webcam. If the camera drops out or the Pi complains about USB current, add this line to the end of /boot/firmware/config.txt and reboot:
If the Pi reboots by itself when the car starts moving, check that the motors are really on the AA pack and not on the Pi.
Uploading new Arduino code
The Uno can only talk to one computer at a time. To upload from your laptop, quit the Python program on the Pi, unplug the Uno's USB from the Pi, and plug it into your laptop. Plug it back into the Pi when you're done.
Tool: SSH and a Python environment on the Pi
SSH gives you a terminal on the Pi from your laptop, over Wi-Fi. Your laptop and the Pi must be on the same network. In a terminal on your laptop (use the username and hostname you chose when you flashed the card):
Everything you type now runs on the Pi. exit comes back to your laptop.
Raspberry Pi OS wants each project's Python packages in its own virtual environment (venv), a folder with its own copy of Python and packages. Make one once:
Every time you log in, run source ~/car-env/bin/activate again before you run your programs. Your prompt starts with (car-env) when it's on.
To edit files on the Pi, use VS Code's Remote - SSH extension from your laptop, or nano in the terminal.
Mission 1 · The Pi is alive
Boot the Pi on the power bank, SSH in from your laptop, and set up the venv. Find the Uno: with it plugged in, run ls /dev/ttyACM* /dev/ttyUSB*. A genuine Uno usually shows up as /dev/ttyACM0; some clones as /dev/ttyUSB0.
Done when
python -c "import cv2, serial; print(cv2.__version__)" prints a version number inside your venv, and you know your Uno's port name.
Tool: talking to an Arduino from Python
The pyserial package opens the same USB connection the Serial Monitor uses. Example: the LED sketch from Class 1's Serial tool, now controlled by Python instead of by you typing.
import time
import serial
uno = serial.Serial("/dev/ttyACM0", 115200, timeout=1)
time.sleep(2) # opening the port restarts the Uno; let it boot
for level in [0, 64, 128, 255, 0]:
uno.write(f"B {level}\n".encode()) # text → bytes
reply = uno.readline().decode().strip() # bytes → text
print("sent", level, "got", reply)
time.sleep(0.5)
uno.close()
Two things trip everyone up: the Uno restarts every time Python opens the port (hence the 2 s wait), and only one program can have the port open at a time. Close the Serial Monitor first.
Tool: reading keys over SSH with curses
input() waits for Enter. To react to single keys, Python has curses, built in. Example: a counter you change with the arrow keys.
import curses
def main(screen):
screen.timeout(100) # getch() gives up after 100 ms
count = 0
while True:
key = screen.getch() # -1 if no key was pressed
if key == curses.KEY_UP:
count += 1
elif key == curses.KEY_DOWN:
count -= 1
elif key == ord("q"):
break
screen.erase()
screen.addstr(0, 0, f"count = {count} (arrows change it, q quits)")
curses.wrapper(main)
screen.timeout(100) means the loop runs at least 10 times a second even when no key is pressed. You'll want that below.
Mission 2 · Drive from the laptop
The bridge. Write a small Python module, car.py, that opens the Uno's port and has one function per command: forward(speed), turn(degrees), stop(), corridor(speed), distances(). This is the Serial bridge, and it's one of the places Claude Code may help (see the overview). You decide the commands; it's your protocol from Classes 1 and 2.
The safety stop. Add this to the Uno: if no command has arrived for 500 ms, stop the motors. Use millis(). Then if the Wi-Fi drops or the Python program crashes, the car stops by itself instead of driving into a wall. The keyboard program must then resend the current command several times a second, which is what screen.timeout(100) is for.
The driver. Write drive.py: arrow keys drive (up = forward, down = back, left/right = spin), space stops, q stops and quits. Run it over SSH.
Done when
You drive the car around the room over SSH. When you close the laptop lid (or press Ctrl+C), the car stops within half a second.
Tool: the webcam with OpenCV
cv2.VideoCapture(0) opens the first camera. read() gives you one frame, a grid of pixels. Example: show the camera with its average brightness written on the picture.
import cv2
cam = cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
ok, frame = cam.read()
if not ok:
print("no frame")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.putText(frame, f"brightness {gray.mean():.0f}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("camera", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cam.release()
cv2.destroyAllWindows()
This runs on your laptop too, with its built-in camera. That's how you can work on vision code before the car is ready.
A window can't open over a plain SSH terminal. To see the Pi's screen from your laptop, use screen sharing (Raspberry Pi Connect or VNC; Dr. Alvear will help you set up whichever works on the school network), and run the camera program from a terminal on the Pi's desktop.
Mission 3 · Drive by camera
Run the camera window on the Pi's desktop (seen through screen sharing) and drive.py over SSH at the same time. Put the car in another part of the room, behind something, where you can't see it directly.
Done when
Without looking at the car, you drive it from across the room through a doorway or between two chairs, using only the camera view.
If you finish early¶
- Show the three distances from
?on the camera window, as text. - Make the car's speed adjustable from the keyboard (
+and-). - Time the loop: how many frames per second does the camera give at 640 × 480? At 320 × 240?
Sign-off with Dr. Alvear
- Pi boots on the power bank; motors still only on the AA pack
-
car.pybridge works; you can say what each function sends - Uno stops by itself when commands stop
- Driven across the room by camera
- Build log updated (photo + two sentences)