Obstacle-avoiding rover
A small rover that rolls forward until an ultrasonic sensor sees something in the way, then backs up and turns. A friendly second mobile-robot project, with an AI helper writing the behavior loop.

Safety first
- Do not run near stairs or table edges until the avoid loop is reliable. Ultrasonic sensors miss thin legs, foam and some fabrics.
- Unplug power before rewiring. Motor voltage must not feed the ESP32 3.3 V pin.
- If you use lithium batteries, use a protected pack, the correct charger, and do not leave charging unattended.
- This is a low-voltage project. No mains wiring.
Parts
| Part | Qty | Approx. each | Subtotal |
|---|---|---|---|
| Espressif ESP32-DevKitC-32E (DevKitC V4) development boardOfficial Espressif 38-pin DevKitC V4 with ESP32-WROOM-32E (4 MB). Micro-USB port, not USB-C. Buy only the official 38-pin Micro-USB board, not a USB-C or 30-pin clone. Primary listing: Mouser 356-ESP32-DEVKITC32E, $10.20, 1,701 in stock on Nov 12, 2026, per Findchips (https://www.findchips.com/search/ESP32-DEVKITC-32E). The Mouser US page itself returned 403 to a direct fetch, so click-confirm the US storefront before you order. Mouser Netherlands listed 1,622 in stock at €8.60 the same day. Do not use DigiKey as the default today (search snippets showed 0 in stock / long lead time). Official Amazon listing https://www.amazon.com/dp/B09MQJWQN2 was still unavailable; use it if it returns. Amazon search for the official board: https://www.amazon.com/s?k=ESP32-DevKitC-32E. Adafruit #3269 remains $15.00 and was out of stock (https://www.adafruit.com/product/3269). Typical $10-16.Mouser · price seen on 2026-11-12 | 1 | $10.20 | $10.20 |
| SparkFun Motor Driver - Dual TB6612FNG (with headers)Price seen ~$14.50 on SparkFun, Nov 12, 2026. Price seen $14.50 on SparkFun. Amazon listings exist but had few reviews and a higher price, so the official page is the source.SparkFun · price seen on 2026-11-12 | 1 | $14.50 | $14.50 |
| TT 3-6 V gearbox motor with wheel (2-set)Typical $3.5-$6.5 each (or see pack note). ASIN confirmed for a 2-motor kit. Typical $8-13 for the set. Unit price is an estimate.Amazon | 1 | $5 | $5 |
| ELEGOO HC-SR04 ultrasonic sensor (5-pack)One sensor from the pack is enough. Do not feed a 5 V echo pin straight into an ESP32. Typical $1.5-$2.2 each (or see pack note). ASIN confirmed (well-known ELEGOO 5-pack). Typical $8-11 for the pack. Use a 3.3 V-safe echo path on an ESP32.Amazon | 1 | $1.80 | $1.80 |
| Small swivel caster or ball wheel2WD only. Typical $2-4, no verified link.Amazon (search) | 1 | $2.50 | $2.50 |
| 4x AA battery holder with switch, or a 5 V USB power bank you already ownTypical $3-8 for a holder, no verified link.Amazon (search) | 1 | $5 | $5 |
| M3 screws, standoffs, and jumper wiresTypical $4-8 for a small hardware kit, no verified link.Amazon (search) | 1 | $5 | $5 |
| Estimated total (parts only) | $44 | ||
Typical US prices, checked 2026-11-12. Prices change and multi-packs are often cheaper per piece.
As an Amazon Associate I earn from qualifying purchases. Prices change; check the store.
Filament
PLA (1.75 mm): about 110 g (36.9 m), roughly 7 h of printing.
PLA, 0.4 mm nozzle, 0.2 mm layers, 25% infill, 3 walls, supports only if the motor bays or sensor bracket have overhangs.
AI agents you'll use
- Code helper (ChatGPT, Claude, Gemini or Grok): Writes the distance function with a timeout and a back-up-and-turn behavior, then fixes compile errors you paste in.
- Wiring checker: Flags a 5 V echo pin into an ESP32 GPIO and checks VM vs VCC on the motor driver.
Tools
- 3D printer or a kit chassis
- Small screwdriver
- Computer with Arduino IDE or PlatformIO
- USB data cable
- Tape measure or a notebook
What you'll learn
- Driving two or four DC motors with a dual motor driver
- Measuring distance with an HC-SR04, including a timeout so a missed echo cannot hang the loop
- Why cheap ultrasonic sensors miss some objects
- A simple wander-and-avoid behavior
The rover drives forward while the path looks clear, then backs up and turns when the ultrasonic sensor reports a close object. It is a short build, and a good place to learn that cheap distance sensors are noisy and miss plenty of real obstacles.
What you'll build
A 2WD (or 4WD) chassis with an HC-SR04 looking straight ahead, a TB6612FNG driver, and an ESP32 running a simple behavior loop. Size is typically similar to a lunchbox.
Print the parts
Search Printables or Thingiverse for an open-licensed "ESP32 rover chassis TT motor HC-SR04" (or use a kit chassis) and credit the designer. The sensor bracket should be unobstructed, level, and a few centimeters above the floor.
Print with the settings above, or skip the print if you already have a kit. Expect about 110 g of PLA for a typical printed chassis; confirm in your slicer.
Wiring
| From | To (ESP32, unless noted) |
|---|---|
| TB6612 PWMA, AIN1, AIN2 | GPIO 25, 26, 27 |
| TB6612 PWMB, BIN1, BIN2 | GPIO 14, 32, 13 |
| TB6612 STBY | GPIO 33 |
| TB6612 VM | Motor battery, within the driver limits |
| TB6612 VCC and GND | As marked on the module; common ground |
| HC-SR04 Trig | GPIO 16 |
| HC-SR04 Echo | GPIO 17, only through a 3.3 V-safe path (a divider, a level shifter, or a 3.3 V-labeled module) |
| HC-SR04 VCC | 5 V for a classic HC-SR04 (many still need 5 V on VCC even if Echo is shifted) |
Step-by-step
- Assemble or print the chassis and mount the motors, wheels, and the HC-SR04 so it looks straight ahead.
- Install the Arduino IDE and the ESP32 board package.
- Wire the driver and sensor, including a 3.3 V-safe echo path.
- Flash a distance-only sketch and compare readings to a tape measure at 10, 30 and 80 cm.
- Hang the wheels and test motor direction, then put the rover on a hard floor with a "forward if clear, else back and turn" loop.
- Start with a conservative stop distance, such as 20-25 cm, and a short backup before the turn.
- Add a left/right glance (turn, sample, choose the clearer side) if it keeps clipping corners.
- Secure the battery, add a switch, and clip loose wires away from the wheels. A front bumper switch is a good later backup for objects the HC-SR04 cannot see.
Code
Prompt for the code helper:
Write an Arduino-ESP32 sketch for a two-motor rover using a TB6612FNG (PWMA 25, AIN1 26, AIN2 27, PWMB 14, BIN1 32, BIN2 13, STBY 33) and an HC-SR04 on GPIO 16 (Trig) and 17 (Echo).
Include a distance function that uses pulseIn with a timeout so a missed echo cannot hang the loop, and a behavior that drives forward when the path is clear and otherwise backs up and turns.
Do not treat the "microseconds / 58" conversion as more precise than a tape-measure check. Comment the code for a beginner.
Distance helper to test first:
const int TRIG = 16, ECHO = 17;
long distanceCm() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 30000); // ~30 ms timeout
if (us == 0) return -1;
return us / 58; // rule of thumb; confirm with a tape measure
}
Troubleshooting
- Always -1 / no echo: Echo is not reaching the GPIO, the module is unpowered, or you are feeding a shifted pin the wrong way. Recheck the 3.3 V path.
- Reads are wild at close range: many HC-SR04 modules have a minimum range of a few centimeters. Raise the stop threshold.
- It rams chair legs or curtains: ultrasonic sensors often miss them. Slow down and add a bumper switch.
- ESP32 GPIO damage concern: a 5 V Echo line must not be wired straight in. Add a divider or buy a 3.3 V-safe module.
- Board will not boot or flash after rewiring: avoid GPIO 12 for motor pins. It is a strapping pin (flash-voltage select), which is why BIN1 uses GPIO 32 here.
Files, code and docs
- Installing the Arduino-ESP32 core (Espressif docs) (Vendor docs)
- TB6612FNG hookup guide (SparkFun) (Vendor docs)
Last checked 2026-11-12. Double-check parts against your own board and seller listings before you buy.