Exact square with wheel encoders (ESP32, two 37D gearmotors with encoders)
A two-wheel robot counts its wheels' turns and drives an exact 50 cm square.
- L298N
- Gearmotors with encoders
Open in the simulatorNo account needed: press Simulate and it runs.

How it works
Both gearmotors carry a Hall encoder on the motor shaft: 64 counts per motor turn, 1920 per wheel turn. The ESP32 counts channel A in an interrupt and reads B for the direction, 3.82 counts per millimetre on the 80 mm wheels. Straight on, the wheel that is ahead by some counts is slowed down, so the robot keeps its line; near the end it slows down and brakes on the mark; turning on the spot, each wheel rolls a quarter of the circle between the wheels. The tape square shows where it should go. Set KS to 0 in the sketch and see it drift, or change SIDE_MM and TURN_COUNTS.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | A1 GNDBT1 −ML GNDMR GNDR2 2R4 2R6 2R8 2U3 GND |
| V5 | A1 5VML VCCMR VCCU3 +5V |
| 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 |
| LA | ML AR1 1 |
| LA_3V3 | A1 GPIO34R1 2R2 1 |
| LB | ML BR3 1 |
| LB_3V3 | A1 GPIO35R3 2R4 1 |
| ML_A | ML +U3 OUT1 |
| ML_B | ML −U3 OUT2 |
| MR_A | MR −U3 OUT3 |
| MR_B | MR +U3 OUT4 |
| RA | MR BR5 1 |
| RA_3V3 | A1 GPIO36/VPR5 2R6 1 |
| RB | MR AR7 1 |
| RB_3V3 | A1 GPIO39/VNR7 2R8 1 |
Parts
Firmware
sketch.ino · 82 lines
// Exact square with wheel encoders: each gearmotor has a Hall encoder on its motor shaft (64 counts
// per motor turn, 1920 per wheel turn at 30:1). The sketch counts channel A's edges in an interrupt
// (960 per wheel turn: 3.82 counts per mm on an 80 mm wheel) and reads B for the direction.
//
// Straight on: both wheels must turn the same, so the faster one is slowed by how many counts it is
// ahead (KS). Near the end it slows down, at the mark it brakes (the L298N shorts the motors). Turning on the spot: each wheel
// rolls a quarter of the circle between the wheels (170 mm apart: 133.5 mm, 510 counts). On a real
// robot the tyres slip a little: tune TURN_COUNTS until four turns make a full circle.
//
// Try it: change SIDE_MM or TURN_COUNTS, or set KS to 0 and watch it drift off the square.
constexpr int ENA = 25, IN1 = 26, IN2 = 27; // left motor
constexpr int ENB = 14, IN3 = 32, IN4 = 33; // right motor
constexpr int LA = 34, LB = 35; // left encoder A / B (through 10 k / 20 k dividers)
constexpr int RA = 36, RB = 39; // right encoder
constexpr float COUNTS_PER_MM = 960.0 / (80 * PI);
constexpr int SIDE_MM = 500;
constexpr long TURN_COUNTS = 510; // 90°: a quarter of the 170 mm circle per wheel
constexpr float SPEED = 0.45; // cruising PWM share
constexpr float MIN_SPEED = 0.18; // slower than this the motors stall
constexpr float KS = 0.01; // straightness: PWM per count of difference
constexpr int PWM_BITS = 10, PWM_MAX = (1 << PWM_BITS) - 1;
volatile long cntL = 0, cntR = 0;
// A changed: with B equal it went back, else forward
void IRAM_ATTR isrL() { cntL += digitalRead(LA) == digitalRead(LB) ? -1 : 1; }
void IRAM_ATTR isrR() { cntR += digitalRead(RA) == digitalRead(RB) ? -1 : 1; }
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); }
// brake: both inputs low with the bridge enabled short each motor (it stops at once instead of rolling on)
void brake() {
digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
ledcWrite(ENA, PWM_MAX); ledcWrite(ENB, PWM_MAX);
}
// move until each wheel has rolled `target` counts (dirL / dirR: +1 forward, -1 back)
void move(long target, int dirL, int dirR) {
long l0 = cntL, r0 = cntR; // 32-bit reads are atomic
unsigned long t0 = millis();
while (true) {
long l = (cntL - l0) * dirL, r = (cntR - r0) * dirR;
long done = (l + r) / 2;
if (done >= target) break;
float ramp = min(1.0f, (millis() - t0) / 600.0f); // soft start over 0.6 s
float brake = (target - done) / (3.0f * COUNTS_PER_MM * 25); // slow down over the last 75 mm
float u = max(MIN_SPEED, SPEED * min(ramp, min(1.0f, brake)));
float k = KS * (l - r); // the wheel ahead slows down
drive(dirL * (u - k), dirR * (u + k));
delay(2);
}
brake();
delay(300);
drive(0, 0);
}
void setup() {
Serial.begin(115200);
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
for (int p : { LA, LB, RA, RB }) pinMode(p, INPUT);
attachInterrupt(digitalPinToInterrupt(LA), isrL, CHANGE);
attachInterrupt(digitalPinToInterrupt(RA), isrR, CHANGE);
ledcAttach(ENA, 1000, PWM_BITS);
ledcAttach(ENB, 1000, PWM_BITS);
drive(0, 0);
Serial.println("encoder robot ready");
}
int side = 1;
void loop() {
move(SIDE_MM * COUNTS_PER_MM, 1, 1);
Serial.printf("side %d: L %ld R %ld counts\n", side, cntL, cntR);
move(TURN_COUNTS, -1, 1); // left on the spot
side = side % 4 + 1;
}Open in the simulator