Hindernis-Roboter (ESP32, HC-SR04, zwei Getriebemotoren)
Ein zweirädriger Roboter fährt durch einen Raum mit Wänden und weicht aus, wenn sein Ultraschallsensor etwas sieht.
- L298N
- Gearmotors
- HC-SR04
Im Simulator öffnenOhne Konto: Simulieren drücken, und es läuft.

So funktioniert es
Ein klassischer zweirädriger Roboter: zwei Getriebemotoren mit Rädern hinten unter der Platine, vorne eine Kugelrolle, ein 3S-Akku, ein L298N und ein ESP32. Der HC-SR04 an der Vorderkante vermisst den Raum: Sein Schallkegel (blau) trifft auf Wände und Kartons, und das Echo kommt nach der echten Laufzeit des Schalls zurück. Ist etwas näher als 30 cm, hält der Roboter an, setzt zurück und dreht weg. Verschieb während der Fahrt Kartons und Wände in der Welt, stell ihn in eine Ecke oder ändere SPEED und STOP_CM im Sketch.
Verdrahtung
Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.
| Netz | 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 |
Bauteile
Firmware
sketch.ino · 78 Zeilen
// 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
}Im Simulator öffnen