Build a Robot That
Follows a Line 🤖
Two tiny IR sensors and an Arduino UNO are all it takes to build a robot that drives itself along a black track — no remote control needed!
How does a line follower robot work? 🧠
The robot has two IR (infrared) sensors mounted on its belly, pointing down at the floor. Each sensor sends out invisible infrared light and checks if it bounces back.
White surface → light bounces back strongly → sensor reads LOW (0).
Black line → light is absorbed, barely bounces back → sensor reads HIGH (1).
The Arduino reads both sensors 100+ times per second and decides which way to steer the motors — forward, turn left, turn right, or stop. That's it! The robot "thinks" about 100 times every second to stay on the line.
Sensor over white
IR light bounces back
Sensor over black
IR light is absorbed
What the robot decides — the logic table 📊
This is the "brain" of the robot. The Arduino checks these four possible combinations of sensor readings and tells the motors what to do:
| Left Sensor | Right Sensor | What it means | Motor action |
|---|---|---|---|
| 0 (white) | 0 (white) | Both on white — robot is on the line! | ⬆ GO FORWARD |
| 1 (black) | 0 (white) | Left is on black — drifted left of line | ↪ TURN RIGHT to correct |
| 0 (white) | 1 (black) | Right is on black — drifted right of line | ↩ TURN LEFT to correct |
| 1 (black) | 1 (black) | Both on black — reached junction or end | ⬛ STOP |
Components you need 🛒
All parts available at your local electronics shop or online. Total cost: roughly ₹500–₹800.
How to connect everything 🔌
Study this circuit carefully before wiring. One wrong wire can make the motors go backward or damage the board!
Complete Pin Connection Table 📌
| From | Pin / Terminal | To | Pin / Terminal | Notes |
|---|---|---|---|---|
| IR Sensor Left | VCC | Arduino | 5V | Powers the sensor |
| IR Sensor Left | GND | Arduino | GND | Common ground |
| IR Sensor Left | OUT | Arduino | A0 | Reads HIGH over black line |
| IR Sensor Right | VCC | Arduino | 5V | Powers the sensor |
| IR Sensor Right | GND | Arduino | GND | Common ground |
| IR Sensor Right | OUT | Arduino | A1 | Reads HIGH over black line |
| Arduino | D2 | L298N | IN1 | Left motor direction bit A |
| Arduino | D3 | L298N | IN2 | Left motor direction bit B |
| Arduino | D4 | L298N | IN3 | Right motor direction bit A |
| Arduino | D7 | L298N | IN4 | Right motor direction bit B |
| Arduino | D5 (PWM) | L298N | ENA | Left motor speed (PWM) |
| Arduino | D6 (PWM) | L298N | ENB | Right motor speed (PWM) |
| L298N | OUT1, OUT2 | Left Motor | + – | Drive left wheel |
| L298N | OUT3, OUT4 | Right Motor | + – | Drive right wheel |
| 9V Battery | + | L298N | 12V | Motor power supply |
| L298N | 5V out | Arduino | VIN | L298N's onboard regulator powers Arduino! |
| L298N | GND | Arduino | GND | Common ground — very important! |
Step-by-step instructions 🔧
Follow these steps in order. Don't skip the testing steps — they save a lot of frustration!
Assemble the chassis
Attach both BO motors to the bottom plate of the acrylic chassis using the motor brackets and screws. Fit the wheels onto the motor shafts. Attach the castor (ball wheel) at the front — this keeps the robot balanced and lets it turn smoothly.
Mount the boards on the second layer
Use standoffs (small brass spacers) to mount the Arduino UNO and L298N Motor Driver on the top layer of the chassis. Keep them apart so their pins don't touch. Use M3 screws to secure them firmly — a board that wobbles causes bad connections!
Mount the IR sensors under the chassis
Attach both IR sensor modules to the underside of the bottom plate using small screws or cable ties. Position them at the very front, about 5–8 mm above the ground, and space them so they are about 2 cm apart — one on the left, one on the right of centre.
Connect the motors to L298N
Connect the left motor's two wires to L298N's OUT1 and OUT2 terminals. Connect the right motor's two wires to OUT3 and OUT4. Tighten the screw terminals firmly — a loose motor wire causes stuttering and strange behaviour!
Wire the L298N to Arduino
Connect IN1→D2, IN2→D3, IN3→D4, IN4→D7, ENA→D5, ENB→D6 using jumper wires. Then connect L298N's GND to Arduino GND, and L298N's 5V output pin to Arduino's VIN — the L298N's built-in voltage regulator powers your Arduino from the 9V battery!
Connect the IR sensors to Arduino
Left sensor: VCC→5V, GND→GND, OUT→A0. Right sensor: VCC→5V, GND→GND, OUT→A1. Use the breadboard to share the 5V and GND connections neatly between both sensors.
Connect the battery
Connect the 9V battery clip's red wire (+) to L298N's 12V terminal, and the black wire (–) to L298N's GND terminal. Do NOT connect the battery yet — wait until after uploading the code!
Upload the code
Connect the Arduino to your computer via USB cable. Open Arduino IDE, paste the code from the next section, and click the Upload button (→ arrow). Wait for "Done uploading" message. Then disconnect the USB cable.
Calibrate the IR sensors
Connect the battery. Place the robot on white paper. Use a small screwdriver to turn the blue potentiometer on each IR sensor module. The red indicator LED on the sensor should be ON when over white and OFF when you place black tape under it. Adjust until the switch is crisp and reliable.
Test on your track!
Make a track on white A3 chart paper with 2-inch black electrical tape. Keep curves smooth and gradual. Place the robot on the line, make sure sensors straddle the line (one on each side), turn it on and let go! Fine-tune motor speed values in the code if the robot overshoots corners.
The code that makes it think 💻
Every section is explained with comments. Adjust the speed values at the top to tune your robot's performance on different track shapes.
// ========================================================= // LINE FOLLOWER ROBOT — Arduino UNO // Two IR sensors + L298N Motor Driver // Written in a kids-friendly style with full comments! // ========================================================= // ─── MOTOR DRIVER PINS ─────────────────────────────────── #define IN1 2 // Left motor direction A #define IN2 3 // Left motor direction B #define IN3 4 // Right motor direction A #define IN4 7 // Right motor direction B #define ENA 5 // Left motor speed (PWM pin!) #define ENB 6 // Right motor speed (PWM pin!) // ─── SENSOR PINS ───────────────────────────────────────── #define IR_LEFT A0 // Left IR sensor output #define IR_RIGHT A1 // Right IR sensor output // ─── SPEED SETTINGS (0-255) ────────────────────────────── // ⚡ Tune these if your robot goes too fast/slow const int BASE_SPEED = 180; // Forward speed const int TURN_SPEED = 160; // Outer wheel in a turn const int SLOW_SPEED = 80; // Inner wheel in a turn (braking) // ─── SENSOR THRESHOLD ──────────────────────────────────── // Analog value above this = sensor detects BLACK LINE const int THRESHOLD = 500; // Range 0-1023; adjust if needed // ========================================================= // SETUP — runs once when Arduino turns on // ========================================================= void setup() { // Set motor control pins as outputs pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT); // Sensor pins are inputs (default, but good to be explicit) pinMode(IR_LEFT, INPUT); pinMode(IR_RIGHT, INPUT); Serial.begin(9600); Serial.println("Line Follower Robot Ready!"); // Small delay before starting — gives you time to place the robot delay(2000); } // ========================================================= // LOOP — runs forever, making decisions 100x per second // ========================================================= void loop() { // Read both IR sensors (analog 0–1023) int leftVal = analogRead(IR_LEFT); int rightVal = analogRead(IR_RIGHT); // Convert to simple true/false: is sensor over the black line? bool leftOnLine = (leftVal > THRESHOLD); bool rightOnLine = (rightVal > THRESHOLD); // Print sensor values to Serial Monitor for debugging Serial.print("L:"); Serial.print(leftVal); Serial.print(" R:"); Serial.println(rightVal); // ─── DECISION MAKING ───────────────────────────────── if (!leftOnLine && !rightOnLine) { // Both sensors on WHITE → go straight! goForward(); } else if (leftOnLine && !rightOnLine) { // LEFT sensor on black → robot drifted left → turn RIGHT turnRight(); } else if (!leftOnLine && rightOnLine) { // RIGHT sensor on black → robot drifted right → turn LEFT turnLeft(); } else { // BOTH sensors on black → junction or end → STOP stopMotors(); } delay(5); // Tiny delay to avoid sensor noise } // ========================================================= // MOTOR CONTROL FUNCTIONS // ========================================================= void goForward() { // Left motor FORWARD digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); // Right motor FORWARD digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); // Set full speed on both wheels analogWrite(ENA, BASE_SPEED); analogWrite(ENB, BASE_SPEED); Serial.println("→ FORWARD"); } void turnLeft() { // Left motor SLOW (inner wheel) digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); // Right motor FAST (outer wheel) digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); analogWrite(ENA, SLOW_SPEED); // Left slows down analogWrite(ENB, TURN_SPEED); // Right speeds up Serial.println("↩ TURN LEFT"); } void turnRight() { // Left motor FAST (outer wheel) digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); // Right motor SLOW (inner wheel) digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); analogWrite(ENA, TURN_SPEED); // Left speeds up analogWrite(ENB, SLOW_SPEED); // Right slows down Serial.println("↪ TURN RIGHT"); } void stopMotors() { // Pull both motor directions LOW = brake digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW); analogWrite(ENA, 0); analogWrite(ENB, 0); Serial.println("⬛ STOP"); }
Interactive Robot Simulator 🎮
Move the robot around and see exactly what both sensors read and what decision the Arduino makes — just like it does in real life!
Cool upgrades to try 🚀
Once your basic robot follows the line smoothly, try these exciting enhancements!
3-Sensor Array
Add a third centre sensor for smoother line tracking — especially good for sharp S-curves
PID Control
Upgrade from simple on/off logic to a PID algorithm — the robot will glide around curves without wobbling
OLED Display
Show real-time sensor values and current action on a tiny 0.96" OLED screen mounted on the robot
Obstacle Avoider
Add an HC-SR04 ultrasonic sensor at the front to pause the robot when something blocks its path
Bluetooth Override
Add an HC-05 Bluetooth module to manually control the robot from your phone, then release it back to line-follow mode
Speed Racer Mode
Use an 11.1V LiPo battery and motor encoders to calculate and maximise speed around a competition track

Comments
Post a Comment