deckle studioOpen in the editor

Line follower (ESP32, two TCRT5000, two gearmotors)

A two-wheel robot follows an oval of black tape on a white mat with two infrared line sensors.

  • L298N
  • Gearmotors
  • TCRT5000
Open in the simulatorNo account needed: press Simulate and it runs.
Line follower (ESP32, two TCRT5000, two gearmotors) in the World view
In the World view

How it works

The two-wheel robot with two TCRT5000 line sensors under its front edge, about 5 mm above the floor, one left and one right of the 19 mm tape. Over the white mat much infrared comes back and a sensor's DO is low; over the black tape little comes back and DO goes high. When the tape slips under the left sensor, the ESP32 slows the left wheel and the robot turns after the line, the same on the right. The sensors follow their data sheet (signal over the distance) and see the floor where the physics puts them. In the World, move or bend the tape while it runs, change SPEED in the sketch until it loses the line in the curves, or raise the sensors with their standoff.

Wiring

Each line is one connection (net) and the pins it joins.

NetPins
GNDA1 GNDBT1 −U3 GNDU4 GNDU5 GND
V5A1 5VU3 +5V
V33A1 3V3U4 VCCU5 VCC
VBATBT1 +U3 +12V
ENAA1 GPIO25U3 ENA
ENBA1 GPIO14U3 ENB
IN1A1 GPIO26U3 IN1
IN2A1 GPIO27U3 IN2
IN3A1 GPIO32U3 IN3
IN4A1 GPIO33U3 IN4
LINE_LA1 GPIO18U4 DO
LINE_RA1 GPIO19U5 DO
ML_AML +U3 OUT1
ML_BML −U3 OUT2
MR_AMR −U3 OUT3
MR_BMR +U3 OUT4

Parts

Firmware

sketch.ino · 52 lines

// 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);
}
Open in the simulator

More examples: Robots