📚 Course contents

AI & Computer Vision, lesson 35 of 52

Computer Vision to Arduino or ESP32

advanced28 min readFull course

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 sendsBoard answersMeaning
PINGPONGAre you there?
GOALOK GOALAlarm for 1.5 s
SCORE 3OK SCORE 3Blink three times
RESETOK RESETEverything off
anything elseERR unknownI 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:

FailureWhat happens
No pyserial / no portRuns without hardware, says so
Silent boardRetries once, then reports
Cable pulled mid-matchCatches the error, switches to no-board mode
Board says ERRLogs 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.

Computer Vision to Arduino or ESP32 | Electronics Simplified