Try Embedded

ESP32-C3 line follower with wheel encoders

The two-wheel robot on an ESP32-C3: five IR readings on its 12-bit ADC, PD steering on LEDC PWM, and the wheels counted on interrupts from slotted encoder…

Control · Advanced

In a real product: Line-following robot (Robotics): A Wi-Fi robot follows a line and reports its distance travelled: the same loop, on a chip with a radio.

The same PD line follower as on the Uno, on an ESP32-C3. Its ADC has 12 bits at 3.3 V, so the readings run from about 250 over the mat to 3690 over the tape, and analogWrite drives the LEDC peripheral at the motor driver's PWM pins. Open the World tool's Arena view to watch it.

What you'll learn

  • Reading five IR sensors on the ESP32-C3's ADC1 and scaling the 12-bit readings
  • PWM on the ESP32 (LEDC through analogWrite) into a TB6612FNG
  • Counting wheel encoders on interrupts, and the edge rate an interrupt can keep up with
  • Choosing an encoder for the board: on the motor shaft or on the wheel

How it works

The robot's encoders can sit on the motor shafts (7 pulses before the 1:30 gearbox: 210 cycles a wheel turn, about 4200 edges a second per wheel flat out) or on the wheels (a 20-slot disc: about 400 a second). On the ESP32 the simulated pins deliver an edge up to a millisecond late, so decoding is exact only up to about 900 edges a second: this robot's popover sets wheel encoders to on the wheel. The sketch counts both edges of each A on CHANGE, two counts a slot, 40 a wheel turn.

The wiring saves pins: each motor's second direction input is tied to GND and STBY to 3V3, so the wheels only turn forwards and the PD output is clamped at 0. The line's position is the weighted mean of the five readings, and the steering Kp·pos + Kd·(pos − last) comes off one wheel and goes on the other, every 5 ms.

Try this

  • Run it with the Arena view open; compare the encoder counts it prints with the robot's own in the HUD.
  • Switch the robot's wheel encoders to on the motor, count both A and B, and watch the counts go wrong at speed.
  • Work out the distance travelled from the counts (a 42 mm wheel, 40 counts a turn) and print it each lap.
  • Lower BASE and raise it: what is the fastest lap that stays on the tape?

sketch.ino

// Line follower on an ESP32-C3: PD on the line's position, through a TB6612FNG. Its wheel encoders are the slotted
// discs on the wheels (encoder "wheel": 20 cycles a turn), slow enough for the ESP32's pins.
//   IR1..IR5 on GPIO 0..4 (ADC1, 12 bits). PWMA 5, PWMB 6 (LEDC through analogWrite), AIN1 7, BIN1 10; AIN2 and
//   BIN2 to GND and STBY to 3V3, so the wheels only go forwards. Encoders' A on GPIO 18 (left) and 19 (right).
// Prints "ir3:<raw> l:<left count> r:<right count>" every 250 ms.
const int IR[5] = {0, 1, 2, 3, 4};
const int PWMA = 5, PWMB = 6, AIN1 = 7, BIN1 = 10, ENC_L = 18, ENC_R = 19;
const float BASE = 170, KP = 12.0, KD = 150.0;

volatile long left = 0, right = 0;   // edges of A, two a slot
float lastPos = 0;
int raw3 = 0;
unsigned long lastRun = 0, lastPrint = 0;

void IRAM_ATTR countLeft() { left = left + 1; }
void IRAM_ATTR countRight() { right = right + 1; }

void setup() {
  Serial.begin(115200);
  pinMode(AIN1, OUTPUT);
  pinMode(BIN1, OUTPUT);
  digitalWrite(AIN1, HIGH);
  digitalWrite(BIN1, HIGH);
  pinMode(ENC_L, INPUT);
  pinMode(ENC_R, INPUT);
  attachInterrupt(ENC_L, countLeft, CHANGE);
  attachInterrupt(ENC_R, countRight, CHANGE);
}

void loop() {
  unsigned long now = millis();
  if (now - lastRun >= 5) {
    lastRun = now;
    float sum = 0, weighted = 0;
    for (int i = 0; i < 5; i++) {
      int raw = analogRead(IR[i]);
      if (i == 2) raw3 = raw;
      // 12 bits at the robot's 3.3 V: about 250 over the mat, 3690 over the tape.
      float c = constrain((raw - 250) / 3440.0, 0.0, 1.0);
      sum += c;
      weighted += c * (2 - i) * 12.0;
    }
    float pos = sum > 0.2 ? weighted / sum : (lastPos > 0 ? 30 : -30);
    float u = KP * pos + KD * (pos - lastPos);
    lastPos = pos;
    analogWrite(PWMA, constrain((int)(BASE - u), 0, 255));
    analogWrite(PWMB, constrain((int)(BASE + u), 0, 255));
  }
  if (now - lastPrint >= 250) {
    lastPrint = now;
    noInterrupts();
    long a = left, b = right;
    interrupts();
    Serial.print("ir3:"); Serial.print(raw3);
    Serial.print(" l:"); Serial.print(a);
    Serial.print(" r:"); Serial.println(b);
  }
}

Lessons behind it