Obstacle-avoiding robot (ESP32, HC-SR04, two gearmotors)
A two-wheel robot drives through a walled room and turns away from what its ultrasonic sensor sees.
- L298N
- Gearmotors
- HC-SR04
Open in the simulatorNo account needed: press Simulate and it runs.

How it works
A classic two-wheel robot: two gearmotors with wheels under the back of the board, a ball caster at the front, a 3S pack, an L298N and an ESP32. The HC-SR04 at the front edge measures the room: its beam (the blue cone) hits the walls and boxes, and the echo comes back after the sound's real travel time. Closer than 30 cm, the robot stops, backs off and turns away. Move the boxes and walls in the World while it runs, put it in a corner, or change SPEED and STOP_CM in the sketch.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | A1 GNDBT1 −R2 2U2 GNDU3 GND |
| V5 | A1 5VU2 VCCU3 +5V |
| VBAT | BT1 +U3 +12V |
| ECHO | R1 1U2 ECHO |
| ECHO_3V3 | A1 GPIO18R1 2R2 1 |
| ENA | A1 GPIO25U3 ENA |
| ENB | A1 GPIO14U3 ENB |
| IN1 | A1 GPIO26U3 IN1 |
| IN2 | A1 GPIO27U3 IN2 |
| IN3 | A1 GPIO32U3 IN3 |
| IN4 | A1 GPIO33U3 IN4 |
| ML_A | ML +U3 OUT1 |
| ML_B | ML −U3 OUT2 |
| MR_A | MR −U3 OUT3 |
| MR_B | MR +U3 OUT4 |
| TRIG | A1 GPIO5U2 TRIG |
Parts
Firmware
sketch.ino · 78 lines
// Obstacle-avoiding robot: an ESP32 drives two gearmotors through an L298N and looks ahead with an
// HC-SR04. It drives straight on; when something is closer than 30 cm it stops, backs off a little,
// turns away (alternately left and right) and drives on. Every measurement goes to the serial console.
//
// The HC-SR04 runs on 5 V: its ECHO comes through a 1 kΩ / 2 kΩ divider to the 3.3 V input.
// Every change of speed or direction is ramped (RAMP_MS): the 30:1 gearmotors are strong, a sudden
// full start makes the robot rear up, a sudden reverse tips it back.
// Try it: move the boxes in the World, put the robot in a corner, change SPEED or STOP_CM.
// pins
constexpr int ENA = 25, IN1 = 26, IN2 = 27; // left motor
constexpr int ENB = 14, IN3 = 32, IN4 = 33; // right motor
constexpr int TRIG = 5, ECHO = 18; // HC-SR04
// behaviour
constexpr float SPEED = 0.45; // driving speed: share of full PWM
constexpr float TURN = 0.40; // turning on the spot
constexpr float STOP_CM = 30; // closer than this: avoid
constexpr int BACK_MS = 350, TURN_MS = 450; // how long to back off and to turn
constexpr int RAMP_MS = 250; // soft changes: from one command to the next in this time
constexpr int PWM_BITS = 10, PWM_MAX = (1 << PWM_BITS) - 1;
// a signed command -1..1 for one motor: direction pins and PWM duty
void motor(int en, int a, int b, float u) {
u = constrain(u, -1.0f, 1.0f);
digitalWrite(a, u > 0 ? HIGH : LOW);
digitalWrite(b, u > 0 ? LOW : HIGH);
ledcWrite(en, (int)(fabsf(u) * PWM_MAX));
}
float curL = 0, curR = 0;
void drive(float left, float right) { motor(ENA, IN1, IN2, left); motor(ENB, IN3, IN4, right); curL = left; curR = right; }
// from the present command to the new one in steps of 10 ms over ms milliseconds
void rampTo(float left, float right, int ms) {
int n = max(1, ms / 10);
float l0 = curL, r0 = curR;
for (int k = 1; k <= n; k++) { drive(l0 + (left - l0) * k / n, r0 + (right - r0) * k / n); delay(10); }
}
// one measurement: the echo's length is the sound's way there and back (343 m/s); no echo within
// 30 ms (beyond 4 m): nothing ahead
float distanceCm() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
unsigned long us = pulseIn(ECHO, HIGH, 30000);
return us ? us * 0.0343f / 2 : 999;
}
void setup() {
Serial.begin(115200);
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
ledcAttach(ENA, 1000, PWM_BITS);
ledcAttach(ENB, 1000, PWM_BITS);
drive(0, 0);
Serial.println("obstacle robot ready");
}
bool turnLeft = true;
void loop() {
float d = distanceCm();
if (d < STOP_CM) {
Serial.printf("obstacle at %.0f cm: back off and turn %s\n", d, turnLeft ? "left" : "right");
rampTo(0, 0, RAMP_MS);
rampTo(-SPEED, -SPEED, RAMP_MS); delay(BACK_MS);
rampTo(0, 0, RAMP_MS);
if (turnLeft) rampTo(-TURN, TURN, RAMP_MS); else rampTo(TURN, -TURN, RAMP_MS);
delay(TURN_MS);
rampTo(0, 0, RAMP_MS);
turnLeft = !turnLeft;
} else {
if (curL != SPEED || curR != SPEED) rampTo(SPEED, SPEED, 2 * RAMP_MS);
static uint32_t lastPrint = 0;
if (millis() - lastPrint > 500) { lastPrint = millis(); if (d < 999) Serial.printf("free ahead: %.0f cm\n", d); else Serial.println("free ahead: no echo"); }
}
delay(60); // the data sheet's measurement cycle: 60 ms, so an old echo is not taken for a new one
}Open in the simulator