Line follower robot with L298N and TCRT5000
The classic robotics starter: a two-wheeled car that tracks a black line on its own.
Shop by category
Shop all componentsUse the MPU6050 gyroscope so the car drives dead straight and turns exactly 90 degrees, then drives a perfect square. Two motors are never exactly the same, so a car left to itself drifts. The gyroscope measures how fast the car rotates; adding that up over time gives the heading. While driving, the sketch speeds up one wheel and slows the other in proportion to the heading error. For a turn it spins until the heading has changed by 90 degrees.
The ESP32 is a 3.3V board: never connect a 5V signal straight to a GPIO pin.
Lift the wheels off the table for the first test, and switch the battery holder off before you change any wiring.
Charge 18650 cells only in a proper Li-ion charger and never short the holder's wires.
Every part for this build comes in the Geekobot Titan Kit box, a Geekobot exclusive, together with the parts for all 120 projects, from a first blinking LED to a smart fan and a reaction game. The wiring below is the kit's own wiring.
Tick anything you already have and the total updates.
ESP32 DevKit V1 (38-pin): the brain of every Titan Kit project, with Wi-Fi and Bluetooth
Male-to-female and female-to-female leads for the modules
2WD chassis with two gear motors, wheels and a caster
L298N motor driver: two H-bridges, one for each wheel
MPU6050 motion sensor: its gyroscope measures how fast the car turns
ESP32 expansion board from the kit: every GPIO has its own S, V, G header
Follow the colours: red is power, dark grey is ground, blue is a signal.
The motors run from the 2 x 18650 pack (7.4V) through the L298N, and the L298N's 5V output powers the ESP32 when the USB cable is out. Switch the pack off whenever the USB cable is plugged in for an upload.
titan_kit_119_gyro_straight_line_robot_c.inoVerify & upload in Electrolark#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// L298N: IN1-IN4 set each wheel's direction, ENA/ENB its speed (0-255)
int in1 = 27, in2 = 26, in3 = 25, in4 = 33;
int enA = 14, enB = 32;
void drive(int left, int right) // -255 (full reverse) to 255 (full forward)
{
digitalWrite(in1, left > 0 ? HIGH : LOW);
digitalWrite(in2, left < 0 ? HIGH : LOW);
digitalWrite(in3, right > 0 ? HIGH : LOW);
digitalWrite(in4, right < 0 ? HIGH : LOW);
analogWrite(enA, constrain(abs(left), 0, 255));
analogWrite(enB, constrain(abs(right), 0, 255));
}
void motorsBegin()
{
int pins[6] = {in1, in2, in3, in4, enA, enB};
for (int i = 0; i < 6; i++)
{
pinMode(pins[i], OUTPUT);
}
drive(0, 0);
}
Adafruit_MPU6050 mpu;
int startButton = 0;
float heading = 0; // degrees, positive = turned left
float gyroBias = 0; // the gyro's reading when still
unsigned long lastMicros = 0;
int base = 170; // cruising speed
float kp = 6; // correction per degree of error
void updateHeading()
{
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
unsigned long now = micros();
float dt = (now - lastMicros) / 1000000.0;
lastMicros = now;
heading += (g.gyro.z - gyroBias) * dt * 57.2958; // rad/s to degrees
}
void straight(float target, unsigned long ms)
{
unsigned long start = millis();
while (millis() - start < ms)
{
updateHeading();
int fix = (int)((heading - target) * kp); // turned left: speed up the left wheel
drive(base + fix, base - fix);
delay(5);
}
drive(0, 0);
Serial.printf("Side done, heading %.1f\n", heading);
}
void turnTo(float target)
{
while (heading < target - 2)
{
updateHeading();
drive(-150, 150); // spin left
delay(5);
}
drive(0, 0);
delay(300);
}
void setup()
{
Serial.begin(9600);
motorsBegin();
pinMode(startButton, INPUT_PULLUP);
if (!mpu.begin())
{
Serial.println("MPU6050 not found");
while (true) { }
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.println("Calibrating: keep the car still");
float sum = 0;
for (int i = 0; i < 400; i++)
{
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
sum += g.gyro.z;
delay(5);
}
gyroBias = sum / 400;
lastMicros = micros();
Serial.println("Ready: press BOOT");
}
void loop()
{
updateHeading();
if (digitalRead(startButton) == HIGH)
{
delay(5);
return;
}
delay(1000);
heading = 0;
lastMicros = micros();
for (int side = 0; side < 4; side++)
{
straight(side * 90, 1500);
turnTo((side + 1) * 90);
}
Serial.println("Square done");
}
Use a micro-USB data cable (some cables only charge), install the CP210x or CH340 driver if no port appears, pick Tools > Board > ESP32 Dev Module and hold the BOOT button on the board until the upload starts.
Check SDA on GPIO21 and SCL on GPIO22 and power it from 3V3. The AD0 pin must stay unconnected (address 0x68).
Keep it still during calibration. If it wobbles, lower kp to 4; if it drifts, raise it to 8.
Switch the battery holder on, check +12V and GND on the L298N, take the ENA and ENB jumper caps off, and make sure battery GND joins ESP32 GND.
Two motors are never exactly the same, so a car left to itself drifts. The gyroscope measures how fast the car rotates; adding that up over time gives the heading. While driving, the sketch speeds up one wheel and slows the other in proportion to the heading error. For a turn it spins until the heading has changed by 90 degrees.
Keep the car still for two seconds after power-on while it calibrates. Press BOOT and it drives a square with sharp 90-degree corners. The Serial Monitor prints the heading during each side.
Yes. It is project 119 of 120 in the Geekobot Titan Kit, in level 17 (Robot car). Every part on the list comes in the kit box, and you can also buy the parts one by one from the list above.
No. Every Titan Kit project is wired with jumper wires on the ESP32 expansion board and the chassis.
9 parts: ESP32-WROOM-32 38-Pin Development Board, USB Cable, Dupont Jumper Wires (40 pcs), 2WD Smart Robot Car Chassis Kit, L298N Dual H-Bridge Motor Driver Module, GY-521 MPU6050 6-Axis Gyro and Accelerometer Module, ESP32 38-Pin Expansion Board, 18650 Battery Holder and 2 × 18650 Li-ion Rechargeable Cell. On this page you can tick the parts you already have and add only the rest to your cart.
It is a advanced project and takes about 1 hour 30 minutes. The build on this page has 7 steps.
No. This page has the complete, tested code (titan_kit_119_gyro_straight_line_robot_c.ino).
About ₹1,578 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Integrating a gyroscope to get a heading; Removing gyro drift with a start-up calibration; Correcting a course with proportional control; Turning by angle instead of by time.
The ESP32 is a 3.3V board: never connect a 5V signal straight to a GPIO pin. Lift the wheels off the table for the first test, and switch the battery holder off before you change any wiring. Charge 18650 cells only in a proper Li-ion charger and never short the holder's wires.
The classic robotics starter: a two-wheeled car that tracks a black line on its own.
A robot that looks left and right with its ultrasonic "head" and picks the clearest way.
Roams a tabletop without ever driving off the edge.
Drive a 4WD car from your Android phone over Bluetooth.
Lessons are free to read. Maker (₹499/month) and Pro add quizzes, worked problems and certificates, paid with your Geekobot account. See plans
Learning project. Check every circuit and the datasheet of each part before you power it up.