📚 Course contents
AI & Computer Vision, lesson 35 of 52
Computer Vision to Arduino or ESP32
Introduction
Until now your detector's reactions stayed on the screen. Today it reaches into the real world: a goal flashes an LED strip, sounds a buzzer, or moves a servo flag, through an Arduino or ESP32 plugged into your laptop. You'll design the conversation between Python and the board, and make sure the match goes on even if someone trips over the cable.
By the end of this lesson you'll be able to:
- Describe a serial protocol as a table of commands and replies
- Write a sketch that listens without blocking
- Send commands from Python and read the replies
- Handle a missing, silent or unplugged board without crashing
- Test the Python side with a pretend board
- State two safety rules for hardware in the classroom
Working principle
Software meets the world
Until now an event (a goal) changed a number on a screen. Today it changes the world: a light and a sound in the room. The idea is the same as lesson 31; we add one more bus.on("goal", ...) handler, and this one talks to hardware.
Serial: a conversation, one line at a time
The USB cable carries bytes in both directions at an agreed speed (the baud rate, 115200). We agree a tiny language of lines:
| Python sends | Board answers | Meaning |
|---|---|---|
PING | PONG | Are you there? |
GOAL | OK GOAL | Alarm for 1.5 s |
SCORE 3 | OK SCORE 3 | Blink three times |
RESET | OK RESET | Everything off |
| anything else | ERR unknown | I did not understand |
This is a protocol. Both sides must follow it exactly. Writing it as a table first, before any code, saves hours.
Always acknowledge
Every command gets a reply. Without it, Python cannot tell "worked" from "board not listening". With it, Python can retry, warn, or give up gracefully.
The sketch must keep listening
A common mistake is delay(1500) to hold the alarm. While the board is delayed it hears nothing. Instead the sketch notes when the alarm should end (millis()) and checks the clock on each pass of loop(). This is the same idea as the doorbell in lesson 31: don't busy-wait, react.
The one delay() we kept (the score blinks) is short. So the sketch replies first and blinks after, so Python's half-second wait is never exceeded.
Hardware fails, and the match must go on
Cables are unplugged, boards reset, a port belongs to another program. The Python side treats the board as optional:
| Failure | What happens |
|---|---|
| No pyserial / no port | Runs without hardware, says so |
| Silent board | Retries once, then reports |
| Cable pulled mid-match | Catches the error, switches to no-board mode |
Board says ERR | Logs the command that was not understood |
If a goal log fails because a buzzer is unplugged, that is a bad design. The log (lesson 31) and the board are separate handlers for exactly this reason.
Arduinos reset when the port opens
Many boards (Uno, Nano) restart when a program opens the port. If you send a command at once, it is lost. We wait two seconds after opening, then clear the input buffer.
Uno versus ESP32
The same sketch runs on both with different pin numbers. The ESP32 runs at 3.3 V, which is fine for an LED with a resistor. An active buzzer rated for 5 V may be quiet on 3.3 V. ESP32 can also do Wi-Fi, so later you could send the same commands over the network (an extension) instead of a cable.
Safety
Keep to low-voltage parts on a breadboard. Never connect mains-powered devices (a big floodlight, a siren) directly. If students want to switch a bigger load, that needs a properly rated relay module and a teacher's check. Buzzers can also be loud: keep them away from ears and warn the room.

