A two-wheel robot that follows a black tape line using three IR sensors, an Arduino Uno and an L298N driver.
Guide
Three IR sensors look down at the floor. Black tape reflects almost no IR light, white floor reflects a lot, so each sensor tells the Arduino "line" or "no line". The robot steers to keep the middle sensor on the tape.
7 parts · about ₹1,581
What you need
Step 1 of 5
Milestone
Chassis assembled
Wheels spin freely by hand and the sensors sit just above the floor.
Step 2 of 5
Pull the two jumpers off ENA and ENB on the L298N so the Arduino can control the speed.
Milestone
Driver wired
With the battery switched on, the L298N power LED lights and the Uno powers up.
Step 3 of 5
Each sensor has VCC, GND and D0 (digital out). Power them from the Uno 5V and connect D0 of the left, centre and right sensors to A0, A1 and A2. Upload this test sketch and open the Serial Monitor at 9600 baud:
| 1 | // Prints the three sensors: 0 = line (black) on most TCRT5000 boards, 1 = floor (white) |
| 2 | void setup() { Serial.begin(9600); } |
| 3 | |
| 4 | void loop() { |
| 5 | Serial.print(digitalRead(A0)); Serial.print(' '); |
| 6 | Serial.print(digitalRead(A1)); Serial.print(' '); |
| 7 | Serial.println(digitalRead(A2)); |
| 8 | delay(200); |
| 9 | } |
Slide a piece of tape under each sensor in turn. Turn the little potentiometer on each module until it switches cleanly between tape and floor. If yours shows 1 on black, change == LOW to == HIGH in the next sketch.
Milestone
Sensors read black and white
Each sensor flips when the tape passes under it.
Step 4 of 5
| 1 | // Line follower: 3 IR sensors + L298N |
| 2 | const int L_IR = A0, C_IR = A1, R_IR = A2; |
| 3 | const int ENA = 5, IN1 = 7, IN2 = 8; // left motor |
| 4 | const int ENB = 6, IN3 = 9, IN4 = 10; // right motor |
| 5 | const int SPEED = 150; // 0-255: start slow, raise once it tracks well |
| 6 | const int TURN = 130; |
| 7 | |
| 8 | int lastSeen = 0; // -1 = line was last on the left, 1 = right |
| 9 | |
| 10 | void motor(int en, int a, int b, int speed) { |
| 11 | digitalWrite(a, speed > 0); |
| 12 | digitalWrite(b, speed < 0); |
| 13 | analogWrite(en, abs(speed)); |
| 14 | } |
| 15 | |
| 16 | void drive(int left, int right) { |
| 17 | motor(ENA, IN1, IN2, left); |
| 18 | motor(ENB, IN3, IN4, right); |
Milestone
It follows the line
The robot completes a lap of a simple oval without leaving the tape.
Step 5 of 5
SPEED, or raise TURN so it corrects harder.Labs
All labs