An ESP32 Dev Module continuously measures forward clearance using a bow mounted HC-SR04 ultrasonic distance sensor and switches a 5V submersible water pump through a TIP120 NPN Darlington transistor on GPIO 4. When open water exceeds 15.0 cm ahead, the pump jets forward for propulsion; whenever an obstacle enters 15.0 cm or closer, GPIO 4 drops LOW and stops the pump automatically.
Building your first autonomous vehicle does not require expensive brushless thruster pods or custom printed circuit boards. With a single ESP32 Dev Module, a budget HC-SR04 ultrasonic sensor, a TIP120 transistor, and a 5V submersible mini water pump, you can assemble a working tabletop prototype boat that drives itself across a test tub and halts cleanly before bumping into walls.
01. How the Autonomous Boat Works (Beginner Overview)
At its heart, every autonomous robot runs a continuous sense and react loop. This boat checks what lies directly ahead twice per second and decides whether to run its stern water jet pump:
- Send an Ultrasonic Ping: The ESP32 pulls the HC-SR04
TrigpinHIGHfor exactly 10 microseconds. The left metal transducer emits an inaudible burst of 40 kHz sound waves straight ahead across the water surface. - Measure the Echo Time: When those sound waves strike a wall or floating obstacle, they bounce back to the right transducer. The HC-SR04 holds its
EchopinHIGHfor the exact round trip flight time in microseconds, which Arduino reads usingpulseIn(echoPin, HIGH). - Convert Time Into Centimeters: Sound travels through air at roughly
0.034 cmper microsecond. Because the sound pulse travels out to the obstacle and back again, we divide the total distance by 2:
distance_cm = duration * 0.034 / 2
- Control the Water Pump: If
distance_cm > 15.0, the path ahead is clear, so the ESP32 drives GPIO 4HIGHto switch on the 5V submersible pump. The moment an obstacle comes within15.0 cmor closer, GPIO 4 dropsLOWand cuts propulsion immediately.
02. Circuit Theory: Why We Use a TIP120 Transistor Instead of Direct GPIO Power
Every beginner eventually asks: why not wire the water pump red wire straight into ESP32 GPIO 4?
Microcontroller pins are logic signals, not power outlets. Each ESP32 GPIO pin outputs 3.3V and can safely supply only 20 mA to 40 mA of continuous current. By contrast, a 5V submersible mini water pump easily pulls 150 mA to 350 mA while pushing water, plus a brief startup surge when the impeller begins spinning. Connecting a motor directly to an ESP32 GPIO pin will permanently burn out the output transistor inside the silicon chip.
To bridge a tiny 3.3V signal to a heavier 5V motor load, we insert a TIP120 Glossary / Darlington TransistorTwo bipolar transistors combined inside one package so a tiny microcontroller base current (just a few milliamps) can switch a much larger motor or pump current safely. as a low side switch:
- TIP120 TO-220 Pinout Orientation: Hold the TIP120 upright with the flat black plastic face pointing toward you (printing visible, silver metal heatsink tab facing away behind it) and the three metal legs pointing down:
- Left Pin = Base (B): Receives the tiny 3.3V control signal from ESP32 GPIO 4 through a resistor.
- Middle Pin = Collector (C): Connects to the negative (black) wire of the 5V water pump.
- Right Pin = Emitter (E): Connects straight to ESP32
GND.
- Role of the 330 Ohm Base Resistor: Without a resistor between GPIO 4 and the TIP120 Base, the transistor base emitter junction behaves like a forward biased diode and draws excessive current from the ESP32 pin. Placing a 330 Ohm resistor in series caps base current safely around 5 mA to 6 mA, which is plenty to saturate the TIP120 fully ON. Check your bands with the Circuit Manual Resistor Decoder before wiring.
- Role of the 1N4007 Flyback Diode: Spinning pump motors contain copper wire coils (Glossary / Inductive LoadAny component with coiled wire such as a motor, pump, or relay coil that stores energy in a magnetic field and releases a sharp reverse voltage spike when switched off.). The instant the TIP120 shuts the pump OFF, the collapsing magnetic field tries to keep current flowing and creates a high voltage spike. Wiring a 1N4007 Glossary / Flyback DiodeA protective diode placed in parallel across a motor or pump coil with its striped cathode facing +5V so reverse voltage spikes loop harmlessly back through the coil instead of damaging the transistor. directly across the pump wires (silver stripe facing the
+5Vred wire, plain black end facing the Collector / pump black wire) absorbs that spike safely.
03. Bill of Materials and Hull Assembly (V1 Prototype)
Autonomous Mini Boat V1 BOM
9 Line Items / 9 Parts Total| Part Name | Specification | Qty | Beginner Notes |
|---|---|---|---|
| ESP32 Dev Module (30 Pin) | 3.3V Logic, 5V VIN via USB | 1 | Main microcontroller running Arduino C++ firmware |
| HC-SR04 Ultrasonic Distance Sensor | 5V DC, 2 cm to 400 cm range | 1 | Mounted at the bow to detect obstacles ahead |
| 5V Submersible Mini Water Pump | 3V to 5V DC brushless/brushed micro pump | 1 | Zip tied to the stern for water jet propulsion |
| TIP120 NPN Darlington Transistor | TO-220 package, 60V 5A rated | 1 | Acts as a low side electronic switch for the 5V pump |
| 330 Ohm Resistor | 1/4W Carbon or Metal Film (Orange, Orange, Brown, Gold) | 1 | Limits base current between ESP32 GPIO 4 and the TIP120 Base |
| 1N4007 Rectifier Diode (Recommended Protection) | 1000V 1A flyback diode | 1 | Placed across pump terminals (striped cathode to +5V) to absorb motor voltage spikes |
| Solderless Breadboard and Jumper Wires | 400 or 830 tie points, Male to Male / Female | 1 | Holds the prototype control circuit on the deck |
| Plastic Water Bottle, Cardboard Deck and Zip Ties | 500 ml to 1L bottle | 1 | Simple buoyancy hull and mounting deck for V1 testing |
| 5V USB-C Power Bank (e.g., Anker) | 5V DC output | 1 | Portable power supply for untethered water testing |
Assembling the Prototype V1 Hull
Before investing in waterproof enclosures or custom hulls, Prototype V1 validates buoyancy, weight balance, and sensor height using zero cost household materials:
- Seal the Buoyancy Hull: Screw the cap tightly onto an empty 500 ml to 1L plastic water bottle so zero water can leak inside.
- Mount the Submersible Pump at the Stern: Wrap two nylon zip ties firmly around the lower rear section of the bottle to hold the 5V submersible pump beneath the waterline, pointing its outlet nozzle straight backward for clean forward thrust.
- Build the Dry Electronics Deck: Cut a flat piece of stiff corrugated cardboard wider than the bottle diameter and secure it horizontally across the top of the bottle with tape or zip ties. Position the solderless breadboard and portable USB power bank near the center of gravity so the bottle does not roll sideways.
- Mount the HC-SR04 at the Front Bow: Tape or bracket the ultrasonic sensor vertically at the front edge of the deck with both cylindrical transducers facing straight ahead and completely clear of the bottle neck so the deck never triggers false close range echoes.
04. Wiring Schedule
Always unplug the USB cable while placing components on the breadboard. Double check the TIP120 flat face orientation (Left = Base, Middle = Collector, Right = Emitter) before powering up.
ESP32 Boat Control Circuit Wiring Schedule
8 Jumpers| Source Pin | Destination Pin | Wire Color | Signal / Purpose |
|---|---|---|---|
| ESP32 VIN (5V) | HC-SR04 VCC | Red | Power supply for the ultrasonic sensor |
| ESP32 GND | HC-SR04 GND | Black | Common ground reference |
| ESP32 GPIO 5 | HC-SR04 Trig | Yellow | Sends the 10 microsecond ultrasonic trigger pulse |
| ESP32 GPIO 18 | HC-SR04 Echo | Blue | Reads the returning ultrasonic echo pulse width |
| ESP32 GPIO 4 | 330 Ohm Resistor -> TIP120 Base (Left Pin) | Orange | Low current 3.3V control signal to switch the transistor on |
| ESP32 VIN (5V) | Water Pump Positive (Red Wire) | Red | Direct 5V power feed to the pump |
| Water Pump Negative (Black Wire) | TIP120 Collector (Middle Pin) | Blue | Switched ground path through the Darlington transistor |
| TIP120 Emitter (Right Pin) | ESP32 GND | Black | Completes the high current pump return path to ground |
05. Bench Troubleshooting Note: Why We Moved From GPIO 13 to GPIO 4
Real hardware prototyping rarely succeeds on the first jumper wire. During initial bench testing of Prototype V1, the pump control signal was first assigned to GPIO 13. The Serial Monitor reported accurate ultrasonic distances above 15 cm, yet the water pump stayed completely silent.
Checking the breadboard with a digital multimeter (DC Volts mode, black probe on GND, red probe on the GPIO header pin) immediately revealed the root cause: GPIO 13 measured 0.00V even when the sketch commanded HIGH. Reassigning the signal wire and firmware constant to GPIO 4 (const int pumpPin = 4;) immediately restored a crisp 3.3V output on the base resistor, saturating the TIP120 and spinning the 5V submersible pump reliably on every cycle.
Whenever a motor or transistor fails to respond on your own bench, measure the microcontroller output pin directly with a multimeter before replacing parts. You can also check safe pin behavior in our Interactive ESP32 GPIO Explorer.
06. Tested Arduino IDE Firmware
Open Arduino IDE 2.3.10, select ESP32 Dev Module under Tools > Board, choose your active USB COM port, and upload the verified sketch below. No external third party libraries are needed.
const int trigPin = 5;
const int echoPin = 18;
const int pumpPin = 4;
long duration;
float distance_cm;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pumpPin, OUTPUT);
digitalWrite(pumpPin, LOW);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance_cm = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
if (distance_cm > 15.0) {
digitalWrite(pumpPin, HIGH);
} else {
digitalWrite(pumpPin, LOW);
}
delay(500);
}07. Bench Verification and V2.0 Upgrade Roadmap
Step by Step Dry Bench Verification
Never place a freshly wired prototype straight into water before proving the logic on a dry desk:
- Open Serial Monitor at
115200Baud: Keep the ESP32 plugged into your computer USB COM port with the dry pump resting on the desk (run dry only for brief 1 to 2 second checks so the impeller bearings stay cool). - Verify Clear Path Switching: Point the HC-SR04 into open air (
> 15.0 cm). The Serial Monitor prints live readings in centimeters and the pump hums smoothly. - Verify Obstacle Stop Behavior: Hold your open hand roughly 10 cm in front of the HC-SR04. Within half a second, the printed distance drops below
15.0 cmand the TIP120 switches the pump cleanly OFF. - Switch to Portable Battery Power: Disconnect the computer USB cable, plug the ESP32 into a portable 5V Anker USB-C power bank, verify weight trim on the cardboard deck, and run your first supervised test in a bathtub or shallow storage bin.
Planned Prototype V2.0 Upgrade Roadmap
Once V1 proves reliable obstacle detection in water, three targeted mechanical and electrical upgrades turn it into a durable pond ready vessel:
- Permanent Soldered Perfboard: Migrate the 330 Ohm resistor, TIP120 transistor, 1N4007 flyback diode, and 1k/2k Echo voltage divider off the solderless breadboard onto a compact soldered perfboard inside a gasketed plastic project box so vibration never loosens a jumper wire.
- Twin Pontoon PVC Catamaran Hull: Replace the single plastic water bottle with two parallel sealed PVC pipe pontoons bridged by a flat deck. A twin hull eliminates roll wobble when carrying a heavier USB power bank.
- Active Steering Control: Upgrade from simple stop and go propulsion by adding either a second stern pump (differential thrust turning left or right) or a waterproof micro servo rudder behind the jet stream so the boat steers smoothly around obstacles instead of stopping in front of them.
Testing and Troubleshooting FAQ
Why does my water pump refuse to spin even though the Serial Monitor shows open distance above 15 cm?+
First check which GPIO pin drives the 330 Ohm base resistor. During bench prototyping, GPIO 13 measured 0.00V under load, whereas moving both the jumper wire and pumpPin definition to GPIO 4 restored a clean 3.3V output. Also confirm the TIP120 Emitter shares a solid common GND wire with the ESP32.
Why does the TIP120 Base, Collector, and Emitter pin order matter so much?+
A Darlington transistor only switches current cleanly from Collector into Emitter when controlled by a small signal on the Base. With the flat black front face pointing toward you and the silver metal tab behind, Left is Base, Middle is Collector, and Right is Emitter. Reversing the chip backward can keep the pump stuck on permanently or overheat the transistor.
Why does my 5V USB power bank shut itself off automatically during testing?+
Many portable power banks include an automatic sleep circuit that turns off 5V output if total current falls below roughly 60 mA to 100 mA for 30 seconds. When the boat stops before an obstacle and the pump switches off, an idle ESP32 alone can dip below that threshold. Enable low current trickle charge mode if your Anker power bank supports it, or trigger periodic pump pulses.
How do I keep the breadboard and ESP32 dry during tub or pond tests?+
Keep the breadboard and USB power bank elevated on a rigid deck above the bottle hull, tape a clear plastic splash shield or food container lid over the electronics, and never run the open Prototype V1 build in choppy water or deep ponds where a capsize could submerge the USB battery.

Chathura Prabashana
Chathura Prabashana builds, tests, and documents beginner friendly ESP32 circuits, microcontroller firmware, and practical electronics prototypes from scratch.
About the Author & Verification Standards →