Linienfolger (ESP32, zwei TCRT5000, zwei Getriebemotoren)
Ein Zweirad-Roboter folgt mit zwei Infrarot-Liniensensoren einem Oval aus schwarzem Band auf einer weißen Matte.
- L298N
- Gearmotors
- TCRT5000

So funktioniert es
Der Zweirad-Roboter mit zwei TCRT5000-Liniensensoren unter der Vorderkante, etwa 5 mm über dem Boden, links und rechts vom 19 mm breiten Band. Über der weißen Matte kommt viel Infrarot zurück und DO ist low; über dem schwarzen Band kommt wenig zurück und DO geht auf high. Rutscht das Band unter den linken Sensor, bremst der ESP32 das linke Rad und der Roboter dreht der Linie nach, rechts genauso. Die Sensoren folgen ihrem Datenblatt (Signal über dem Abstand) und sehen den Boden dort, wo die Physik sie hinbringt. Verschiebe oder biege in der Welt das Band während der Fahrt, erhöhe SPEED im Sketch, bis er die Linie in den Kurven verliert, oder hebe die Sensoren mit ihrem Abstandsbolzen an.
Verdrahtung
Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.
| Netz | Pins |
|---|---|
| GND | A1 GNDBT1 −U3 GNDU4 GNDU5 GND |
| V5 | A1 5VU3 +5V |
| V33 | A1 3V3U4 VCCU5 VCC |
| VBAT | BT1 +U3 +12V |
| ENA | A1 GPIO25U3 ENA |
| ENB | A1 GPIO14U3 ENB |
| IN1 | A1 GPIO26U3 IN1 |
| IN2 | A1 GPIO27U3 IN2 |
| IN3 | A1 GPIO32U3 IN3 |
| IN4 | A1 GPIO33U3 IN4 |
| LINE_L | A1 GPIO18U4 DO |
| LINE_R | A1 GPIO19U5 DO |
| ML_A | ML +U3 OUT1 |
| ML_B | ML −U3 OUT2 |
| MR_A | MR −U3 OUT3 |
| MR_B | MR +U3 OUT4 |
Bauteile
Firmware
sketch.ino · 52 Zeilen
// Line follower: two TCRT5000 sensors under the front of the robot look at the floor, one left and
// one right of the black tape. Over the white mat a sensor's DO is LOW; over the tape it goes HIGH.
// When the tape slips under the left sensor, the line bends left: the left wheel slows down (or even
// turns back a little) and the robot turns after it. The same on the right.
//
// Try it: change SPEED (faster: it starts to lose the line in the curves) or INNER (how hard it
// corrects), then press Simulate again. Move the tape in the World while it runs.
// pins
constexpr int ENA = 25, IN1 = 26, IN2 = 27; // left motor
constexpr int ENB = 14, IN3 = 32, IN4 = 33; // right motor
constexpr int SENSE_L = 18, SENSE_R = 19; // line sensors' DO: HIGH over the black tape
constexpr float SPEED = 0.35; // straight on: share of full PWM
constexpr float INNER = -0.15; // the inner wheel while it corrects (negative: backwards)
constexpr int RAMP_MS = 800; // soft start
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));
}
void drive(float left, float right) { motor(ENA, IN1, IN2, left); motor(ENB, IN3, IN4, right); }
void setup() {
Serial.begin(115200);
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
pinMode(SENSE_L, INPUT); pinMode(SENSE_R, INPUT);
ledcAttach(ENA, 1000, PWM_BITS);
ledcAttach(ENB, 1000, PWM_BITS);
Serial.println("line follower ready");
// start softly, straight on (the tape runs between the two sensors)
for (int k = 1; k <= RAMP_MS / 10; k++) { float u = SPEED * k / (RAMP_MS / 10); drive(u, u); delay(10); }
}
int last = -1;
void loop() {
bool l = digitalRead(SENSE_L) == HIGH, r = digitalRead(SENSE_R) == HIGH;
int state = l && !r ? 1 : r && !l ? 2 : 0;
if (state == 1) drive(INNER, SPEED); // line under the left sensor: turn left
else if (state == 2) drive(SPEED, INNER); // under the right one: turn right
else drive(SPEED, SPEED); // between them (or both on it, a crossing): straight on
if (state != last) {
Serial.println(state == 1 ? "line left: turn left" : state == 2 ? "line right: turn right" : "straight on");
last = state;
}
delay(2);
}Im Simulator öffnen