Introduction: Line Follower Robot Using Only One IR Sensor

About: An Electronics engineer and a hobbyist. I love to keep experimenting with microcontrollers.

Hello everyone, Today we will make a line follower robot. Nope, its not like normal line followers with two sensors trying to keep the robot in the center of the line. Instead it will use only one sensor. I have mounted the sensor on a servo motor to get the readings from both the sides of a line. Note : This project is something that I tried for the first time and its working but not as per my expectations. So please send me your feedback on how I could make it better. Maybe using interrupts or timers will improve its performance. Do let me know in the comments section.

Step 1: Working of the Project

Now we all must be aware of how the basic line follower robot works. When both the sensors are on the white line, the robot moves forward. When one of the sensor is on the black line, the motor opposite to that sensor turns on and pushes it back to the other side of the line. This is how the basic line follower works. Now this one uses similar principle but it uses only one sensor. You might have a question, "Then how do you get the readings from the other side of the line?"

Well, for that I mounted the sensor on a servo motor. When the motor is at 0 degrees, the sensor takes 1st reading and when the motor is at 180 degrees, the sensor takes the second reading. Both these readings are then compared and the robot is given instructions to move accordingly.

Hope you like this project. Do support me by liking this video. Also comment your views on this project.

Let's get started !!!

Step 2: Gather the Supplies

I would suggest you guys to buy the components from . They provide high quality components at reasonable rates with on time delivery. If you need a professional PCB, then they also provide PCB Services at affordable rates. Do check them out.

The things we need for this project are -

1. Arduino Uno Board

2. L293D Motor Driver Module

3. IR Sensor

4. Servo Motor

5. 9 V Battery x 2

6. Robot Chassis with Wheels and Motors

7. Breadboard

8. Header Wires

That's just it. Once you gather all the supplies we are good to go.

Step 3: Circuit Diagram

Follow the circuit diagram from above to make all the connections. The motors are connected to the motor driver module. And the motor driver receives input from the Arduino Board. The Ir sensor is connected to the digital pin 3 and the Servo motor is connected to the digital pin 5. I used two batteries to power the Arduino and the motor driver module separately. You can also go with lithium ion packs instead. Do check your connections twice to make sure everything is good. Once you do that we can move forward.

Step 4: Coding

Now download the code or you can just copy it from here.

#include <servo.h>
int sensorValue1;
int sensorValue2;
//Motor A right
const int motorPin1  = 6;  
const int motorPin2  = 9; 
//Motor B left
const int motorPin3  = 10; 
const int motorPin4  = 11;  
void setup()
  pinMode(motorPin1, OUTPUT); 
 pinMode(motorPin2, OUTPUT);
 pinMode(motorPin3, OUTPUT);
 pinMode(motorPin4, OUTPUT);
void loop()
    digitalWrite(motorPin1, LOW);//RIGHT WHEEL
    digitalWrite(motorPin2, LOW);//STOP
    digitalWrite(motorPin3, LOW );//LEFT WHEEL
    digitalWrite(motorPin4, LOW);
    myservo.write(180); //0 = right 180 = left
    sensorValue1 = digitalRead(3);//read at 180 (left)
    sensorValue2 = digitalRead(3);//read at 0 (right)
   // Serial.println("S1");
   // Serial.println(sensorValue1);//LEFT
   // Serial.println("S2");
  //  Serial.println(sensorValue2);//RIGHT
void check()
  if(sensorValue1==HIGH && sensorValue2==HIGH)//WW
    Serial.println("Go Fwd");
    digitalWrite(motorPin1, LOW);//RIGHT WHEEL
    digitalWrite(motorPin2, HIGH);//FORWARD
    digitalWrite(motorPin3, LOW );//LEFT WHEEL
    digitalWrite(motorPin4, HIGH);
  else if(sensorValue1==LOW && sensorValue2==HIGH)//BW
    Serial.println("Go RIGHT");
    digitalWrite(motorPin1, HIGH);//RIGHT WHEEL
    digitalWrite(motorPin2, LOW);//RIGHT
    digitalWrite(motorPin3, LOW );//LEFT WHEEL
    digitalWrite(motorPin4, HIGH);
  else if(sensorValue1==HIGH && sensorValue2==LOW)//WB
    Serial.println("Go LEFT");
    digitalWrite(motorPin1, LOW);//RIGHT WHEEL
    digitalWrite(motorPin2, HIGH);//LEFT
    digitalWrite(motorPin3, HIGH );//LEFT WHEEL
    digitalWrite(motorPin4, LOW);
  else if(sensorValue1==LOW && sensorValue2==LOW)//BB
    digitalWrite(motorPin1, LOW);//RIGHT WHEEL
    digitalWrite(motorPin2, LOW);//STOP
    digitalWrite(motorPin3, LOW );//LEFT WHEEL
    digitalWrite(motorPin4, LOW);

As I explained in the working the code is pretty straight forward. The motor is initially at 180 degree when it takes first reading and then the motor rotates to 0 degrees and takes the second reading. Both these readings are used to see if the robot is following the line accurately or not. If you have trouble understanding this code, feel free to drop a comment below. I will try my best to help you out.

Now compile and upload the code to your Arduino Board and then connect the batteries.

Step 5: Final Project

Remember to adjust the preset pot such that it can detect the white and black lines without any problem.

I have attached few images and a video to show you how the project works.

I faced few problems at the turns with the sensor missing a reading. And I want you guys to solve that problem. Also update me if you make this project. I would like to hear your experience about it. Hope you enjoyed this kind of revised version of the line follower with just one sensor. Do show your support. Also follow me for more projects like this.

That's it for today. See you guys with another instructable soon. Thank you.