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 componentsAdd a safety layer that overrides everything: if an object comes too close, stop and reverse immediately. This is a proximity safety interrupt. The distance check happens at the very top of loop(), before any driving logic, and it returns early when the path is blocked, so nothing further down can override the stop. Two distances are used: below 15 cm the robot reverses, and it will not resume until it has at least 25 cm of clearance. That gap prevents it from lurching forward and back at exactly 15 cm.
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
LEDs from the kit's red, green, yellow and blue packs
220 ohm resistors: one in series with every LED segment
HC-SR04 ultrasonic distance sensor
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_42_auto_reverse_robot.inoVerify & upload in Electrolarkint ENA = 9, IN1 = 4, IN2 = 5, IN3 = 6, IN4 = 7, ENB = 10;
int trigPin = 11, echoPin = 12;
int led = 8, buzzer = 3;
int dangerDistance = 15;
int clearDistance = 25;
int speedValue = 150;
bool blocked = false;
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 backward() { motors(LOW, HIGH, LOW, HIGH); }
void stopBot() { motors(LOW, LOW, LOW, LOW); analogWrite(ENA, 0);
analogWrite(ENB, 0); }
long readDistance()
{
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(led, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
delay(2000);
}
void loop()
{
long distance = readDistance();
Serial.println(distance);
// Safety check runs first and overrides everything
if (distance < dangerDistance)
{
blocked = true;
stopBot();
digitalWrite(led, HIGH);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
backward();
delay(600);
stopBot();
delay(300);
return;
}
if (distance > clearDistance)
{
blocked = false;
}
if (blocked)
{
stopBot();
return;
}
digitalWrite(led, LOW);
forward();
}
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.
Turn it round: the long leg (anode) goes to the pin side through the 220 ohm resistor, the short leg to GND.
The active buzzer has a + leg: it goes to the pin, the other to GND. Peel off the sticker on top if it has one.
Point the sensor at a flat object 5 to 200 cm away and check TRIG and ECHO are not swapped.
This is a proximity safety interrupt. The distance check happens at the very top of loop(), before any driving logic, and it returns early when the path is blocked, so nothing further down can override the stop. Two distances are used: below 15 cm the robot reverses, and it will not resume until it has at least 25 cm of clearance. That gap prevents it from lurching forward and back at exactly 15 cm.
The robot drives forward across clear floor. Put your hand within 15 cm: it stops, the LED and buzzer fire, and it reverses. It waits until there is 25 cm of clearance before moving forward again.
Yes. It is project 42 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.
12 parts: Generic Nano CH340 Development Board (Soldered), USB Cable, Dupont Jumper Wires (40 pcs), Through-Hole LED Pack (Pack of 10), Carbon Film Resistor 1/4W 5% (Pack of 10), PCB Mounted Active Buzzer Module, HC-SR04 Ultrasonic Range Finder Sensor Module for Arduino, 2WD Smart Robot Car Chassis Kit, L298N Dual H-Bridge Motor Driver Module, Arduino Nano IO Expansion Shield (328P Compatible), plus 2 more. On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1 hour. The build on this page has 9 steps.
No. This page has the complete, tested code (explorer_kit_42_auto_reverse_robot.ino).
About ₹1,291 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Proximity safety interrupts; Using return to make a check override later code; Two thresholds to prevent oscillation; Ordering checks by priority.
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.
Learning project. Check every circuit and the datasheet of each part before you power it up.