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 componentsCombine a forward ultrasonic sensor with two side IR sensors and apply the left-hand rule to work through a simple maze. The left-hand rule says: if you can turn left, turn left; otherwise go straight; otherwise turn right; otherwise turn around. Follow it consistently and you will escape any maze without loops, with no map at all. Each rule is one branch of an if- else chain, and the order of the branches is the whole algorithm, swap two of them and the robot behaves completely differently.
Switch the battery holder off before you change any wiring, and lift the wheels off the table for the first test.
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 Explorer Kit box, a Geekobot exclusive, together with the parts for all 61 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.
The brain of every Explorer Kit project
Male-to-female and female-to-female leads for the modules
HC-SR04 ultrasonic distance sensor
IR obstacle sensor: OUT goes LOW when something is in front
2WD chassis with two gear motors, wheels and a caster
Nano I/O shield from the kit: every pin has its own S, V, G header, so modules plug in with jumpers
Follow the colours: red is power, dark grey is ground, blue is a signal.
Motors run from the 2 x 18650 pack (7.4V) through the L298N. The USB cable powers the Nano while you test; unplugged, the L298N's 5V output powers it.
explorer_kit_39_maze_solver_bot.inoVerify & upload in Electrolarkint ENA = 9, IN1 = 4, IN2 = 5, IN3 = 6, IN4 = 7, ENB = 10;
int trigPin = 11, echoPin = 12;
int leftIR = 2, rightIR = 3;
int speedValue = 140;
int wallDistance = 20;
void motors(int l1, int l2, int r1, int r2)
{
digitalWrite(IN1, l1); digitalWrite(IN2, l2);
digitalWrite(IN3, r1); digitalWrite(IN4, r2);
analogWrite(ENA, speedValue);
analogWrite(ENB, speedValue);
}
void forward() { motors(HIGH, LOW, HIGH, LOW ); }
void turnLeft() { motors(LOW, HIGH, HIGH, LOW ); }
void turnRight(){ motors(HIGH, LOW, LOW, HIGH); }
void stopBot() { motors(LOW, LOW, LOW, LOW); analogWrite(ENA, 0); analogWrite(ENB,
0); }
long frontDistance()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 25000);
if (duration == 0)
{
return 200;
}
return duration * 0.034 / 2;
}
void setup()
{
pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(leftIR, INPUT);
pinMode(rightIR, INPUT);
Serial.begin(9600);
delay(2000);
}
void loop()
{
long front = frontDistance();
bool leftWall = (digitalRead(leftIR) == LOW);
bool rightWall = (digitalRead(rightIR) == LOW);
Serial.print("Front: ");
Serial.print(front);
Serial.print(" Left wall: ");
Serial.print(leftWall);
Serial.print(" Right wall: ");
Serial.println(rightWall);
// Left-hand rule
if (!leftWall)
{
stopBot();
delay(150);
turnLeft();
delay(450);
forward();
delay(300);
}
else if (front > wallDistance)
{
forward();
delay(60);
}
else if (!rightWall)
{
stopBot();
delay(150);
turnRight();
delay(450);
}
else
{
stopBot();
delay(150);
turnRight();
delay(900); // Dead end - turn around
}
}
Use the kit's mini-USB cable, install the CH340 driver on Windows if no port appears, pick Tools > Board > Arduino Nano and try Tools > Processor > ATmega328P (Old Bootloader) if the upload times out.
Point the sensor at a flat object 5 to 200 cm away and check TRIG and ECHO are not swapped.
Turn its trim pot: the module's LED should light when your hand is about 10 cm away.
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 Nano GND.
The left-hand rule says: if you can turn left, turn left; otherwise go straight; otherwise turn right; otherwise turn around. Follow it consistently and you will escape any maze without loops, with no map at all. Each rule is one branch of an if- else chain, and the order of the branches is the whole algorithm, swap two of them and the robot behaves completely differently.
Serial Monitor prints the front distance and both wall flags. The robot hugs the left wall through the maze. At a dead end it turns a full half circle and comes back.
Yes. It is project 39 of 61 in the Geekobot Explorer Kit, in level 7 (Robotics and motion). 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 Explorer Kit project is wired on the Nano I/O shield and the breadboard with jumper wires.
10 parts: Generic Nano CH340 Development Board (Soldered), USB Cable, Dupont Jumper Wires (40 pcs), HC-SR04 Ultrasonic Range Finder Sensor Module for Arduino, 2 × Generic Infrared Obstacle Avoidance Sensor Module (Active Low) – LM393 Comparator, 2-30cm Range, 2WD Smart Robot Car Chassis Kit, L298N Dual H-Bridge Motor Driver Module, Arduino Nano IO Expansion Shield (328P Compatible), 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.
No. This page has the complete, tested code (explorer_kit_39_maze_solver_bot.ino).
About ₹1,281 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Complex if-else sensor algorithms; The left-hand maze rule; Combining two sensor types in one decision; How branch order changes behaviour.
Switch the battery holder off before you change any wiring, and lift the wheels off the table for the first test. 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.