What you are building and how it works
Obstabot is a small robot that drives forward until something is in its way, then backs up, turns and drives on. Every robot, from a vacuum cleaner to a Mars rover, does the same three jobs in a loop:
| Job | In Obstabot | Part |
|---|---|---|
| Sense | “How far is the nearest wall?” | HC-SR04 ultrasonic sensor |
| Decide | “Too close? Back up. Close? Turn. Clear? Drive.” | ESP32-C3 SuperMini running MicroPython |
| Act | Turn the two wheels | two continuous-rotation servos |
The loop runs about 15 times per second. That is fast enough that the robot reacts before it hits the wall.
Sense: how a sensor measures distance with sound
The HC-SR04 has two round “eyes”. One is a tiny loudspeaker, the other a microphone. It sends a short click of sound at 40 kHz (too high for you to hear), and the microphone listens for the echo. The ESP32 measures how long the echo took.
Sound travels about 343 metres per second. The click goes to the wall and back, so:
With the time in microseconds and the distance in centimetres this becomes the number you will see in the code: d = t / 58.2.
Decide: three distances, three actions
| Distance | What Obstabot does |
|---|---|
| less than 5 cm | back up for a moment, then turn |
| 5 to 15 cm | turn on the spot until the path is clear |
| more than 15 cm (or no echo) | drive forward |
The guide’s code adds two small ideas that make the robot much less silly:
- it alternates the turn direction, so it does not spin into the same corner forever,
- if it turns for more than 3 seconds without finding space, it backs up and tries again.
Act: wheels that spin all the way round
Normal hobby servos turn to an angle and stop (0° to 180°). Obstabot uses continuous-rotation servos: the same pulse that would mean “go to 90°” means “stand still”, a longer pulse means “spin one way”, a shorter one “spin the other way”. The two servos are mounted mirrored, so to drive forward one turns clockwise and the other anticlockwise. The code handles that for you, and a small calibration tool finds the exact “stand still” pulse of your servos.
What is in this guide
- What you are building and how it works (this chapter)
- Parts and where to buy them
- Wiring the sensor and the servos, safely
- Putting MicroPython on the board and running the code, line by line
- Calibration and troubleshooting
- Challenge upgrades: edge detection, speed control, a second sensor
- What to learn next

