Creating a Line Follower Robot using Arduino Nano

Well, guys this is one of the project that never gets old. This was the first thing I did when I started learning about Arduino. An Arduino Line Follower Robot – A Line Follower Robot Using Arduino…

Well, guys this is one of the project that never gets old. This was the first thing I did when I started learning about Arduino. An Arduino Line Follower Robot – A Line Follower Robot Using Arduino UNO and IR Sensor, which follows a line without user interaction. A small autonomous robot which will “see” and follow the line and take decision when it sees a turn by itself.

New to Robotics?

We have a beginners guide on “Getting Started with Robotics” which will give you a kick start in this field. Check out our free video tutorial below for a brief introduction.

Arduino Line Follower Robot

In this tutorial, we will discuss the working of an Arduino line following robot which will follow a black line in white background and take the correct turn whenever it reaches curves in its path.

Arduino Line Follower Components

Before jumping into line following robot tutorial, let’s get familiarized with the components used. If you are an expert and familiar with these components you can skip this section and straight away jump to the tutorial and start building Arduino Line Follower.

The Chasis of the Arduino Line Follower Robot

The first thing to do is build a chassis for WiFi Robot using Arduino. You can build it the way you like. The only thing you should keep in mind is, it should have enough space for Arduino, L293D Motor Driver, and a LIPO battery. For our project, I will be using a 12V LiPo Battery. You can use foam board or aluminum sheet or wood piece for building the base. These are some of the best robot chassis available for you to build this project. Check out the link below.

Get the Best Robot Chassis Online

Arduino

You all might be familiar with Arduino; which is the most widely used and fastly evolving electronic platform with so many microcontroller boards and software. For our line following robot, I will be using Arduino UNO which is the most commonly used board. The Arduino Nano is the best option to get started with electronics and coding if this is your first experience with Arduino Platform. You can use any Arduino Board for this project.

IR Sensor

As mentioned earlier, our line following robot will be following a black line in a white background. So we need something that will ‘see’ the line and tell the line follower to follow the line or to turn around if it is going away from the line. For this purpose, we will be using an IR (Infra Red) Sensor. This IR sensor mainly consists of an IR transmitter (IR LED) and an IR receiver. IR LED always emits IR rays to the direction it is pointing to. When the IR rays hit a surface, some rays will be reflected back depending upon the color of the surface. Means, the brighter the color is, the more IR will be reflected back. Darker the color is, more IR will be absorbed by the surface and lesser IR rays will be reflected back. These reflected rays are sensed by the IR receiver and depending upon the received IR rays, the resistance of the receiver varies which will, in turn, varies the output voltage. Thus it is possible to sense the color of the surface where the robot is running by looking into the reflected IR rays. So it is very easy to measure how bright the surface is which will make it easy for us to track the line.
There are so many cheap IR sensors available online; you can purchase any of them. You will need at least two of them for making the line follower robot.

H Bridge and L293D Motor Driver

An H-bridge can be any switching circuit using Bipolar Junction Transistors (BJT) or Field Effect Transistors ( MOSFET or MESFET) which allows a voltage to be applied across a DC motor without making physical or hardware changes in the circuit. H Bridge circuits are widely used in the field of robotics to switch the direction of DC motor rotation as well as for creating square waves for so many purposes.
Click Here to Learn the working of an H Bridge Motor Driver L293D is a 16 pin compact H Bridge circuit available in the form of an IC. The advantage of using L293D is, since there are two H Bridge circuits in one IC, you can control two DC motors simultaneously using a single chip. In this project, we will be using this IC to drive the motors in our line following robot.

Lets Get Started

Let us Build a Line Follower Robot Using Arduino UNO and IR Sensor – A Simple Arduino Line Follower Robot.

Getting the PCB from JLCPCB

EasyEDA is an easier but powerful online PCB design tool which allows electronics engineers, hackers, educators, hobbyists, makers, and enthusiasts to design and share their projects’ schematics as well as PCB layout. This is a design tool integrated LCSC components catalog and JLCPCB PCB service that helps users to save time to make their ideas into real products.Simply speaking, PCB layout is kind of like a map. A map that connects all components to each other using conducting tracks. It is this design we imprint on a copper cladded board which is then developed to a PCB. Surface Mount Technology is the technique of assembling PCBs by mounting the components on the surface of the board. Unlike the traditional method of placing the components through holes and soldering them on the other side, in SMT, the components are placed over the board and the leads are soldered on the same side.Advantages of SMT is the Reduced Cost, More efficient Usage of PCB Space, Improved repeatability due to higher level of automation and the ability to build Complex circuits in small PCBs. SMT was developed to reduce manufacturing costs and also to make more efficient use of PCB space.  As a result of the introduction of surface mount technology it is now possible to build highly complex electronic circuits into smaller and smaller assemblies with good repeatability due to the higher level of automation. EasyEDA is an easier but powerful online PCB design tool which allows electronics engineers, hackers, educators, hobbyists, makers, and enthusiasts to design and share their projects’ schematics as well as PCB layout. This is a design tool integrated LCSC components catalog and JLCPCB PCB service that helps users to save time to make their ideas into real products.

Designing the PCB

To get Started, First Go to EasyEDA website and create a free account. Go to “Editor” and create a new project. For now, JLCPCB have 689 Basic components and 30k+ Extended components at your disposal. See the complete list of components here. Make sure you add the components from this list while drawing the schematics in EasyEDA. You can even search for the components and check its availability. Now you can get your layout done using inbuilt tools in EasyEDA. You can now download the Gerber file and use it to manufacture your PCB from JLCPCB. Gerber File contains information about your PCB such as PCB layout information, Layer information, spacing information, tracks to name a few. BOM File or Bill Of Material contains the list of all components in the Layout. CPL file(Component Placement List / Pick & Place File (PNP) file), it is used by automated SMT Assembly machines to determine where each part should be located on the board.

JLCPCB PCB Ordering

Go to JLCPCBs website and Click on “Quote Now” and upload your Gerber File. Once the Gerber file is uploaded, it will show you a preview of your circuit board. Make sure this is the PCB Layout of the board you want. Below the PCB preview, you will see so many options such as PCB Quantity, Texture, Thickness, Color etc. Choose all that are necessary for you. Click on “Assemble your PCB boards”. Now, you will have to upload the BOM and CPL file that we downloaded earlier. Select all the components you want JLCPCB to assemble in your PCB. Simply click on the confirm box to select the components. In this page, you can review your order. You can check the layout, see all the components and if there is any problem, you can click on “Go Back” to edit your order. Once everything is done, click on “Save To Cart”. In the next page, you can choose a shipping and payment option and Check Out Securely. You can either use Paypal or Credit/Debit Card to pay. The PCB will be manufactured and shipped within days and will be delivered to your doorstep within the mentioned time period.  Here is the completely assembled PCB.

Step 1 – Setting up the IR Sensors

Let us start building our line following robot arduino by testing out the IR Sensors and getting the readings out of them. You can either use individual sensors or the array sensor for our Line Following Robot. I will explain both. Most of the individual sensors will be somewhat like this.
It will be having 3 pins. One for VCC (5V), GND (0V) and Vout (Output Voltage). Connect them as follows.
  • IR Sensor —–> Motor Driver
  • 5V —–> 5 Vout of Motor Driver
  • GND —–> GND of Motor Driver
  • Vout of IR Sensor 1 —–> A0 of Arduino
  • Vout of IR Sensor 2 —–> A1 of Arduino
Upload the below code and see if it is working. Code
void setup() {
 Serial.begin(9600);
}
void loop() {
int s1 = digitalRead(A0);
int s2 = digitalRead(A1);
Serial.print(s1);
Serial.print(" -- ");
Serial.print(s2);
Serial.println("");
Serial.println("");
delay(100);
}
Once the code is uploaded, open the serial monitor and you will see output like this
0 -- 0

0 -- 1

0 -- 1

1 -- 1

1 -- 1
These values will be changing depending upon the surface where you are pointing this sensors. Note: If you are using sensors with analog output, and use analogRead instead of digitalRead you will be getting values from 0 to 1023. What do these outputs means? Sensor output ‘1’ means that most of the IR rays are reflected back to the sensor and the surface which is pointed by the sensor is white. And sensor output ‘0’ means that most of the rays are absorbed and the surface is black. When the arduino line follower is on track, the line will be in between the two sensors and will be pointing to the white surface. Which means the output of all the sensors will be 1. In that condition, the line following robot should move forward. When there is a right turn on the track, the first sensor that crosses the line will be S4 and all other sensors will be pointing white surface. Thus the output of S4 will be 0 and all others will be 1. In this case out Arduino line follower should turn right. When there is a Left turn on the track, the first sensor that crosses the line will be S3 and all other sensors will be pointing white surface. Thus the output of S3 will be 0 and all others will be 1. In this case out Arduino line follower should turn left. Awesome. Now you can move to the next step and start building our line following robot arduino.

Step 2 – Building the Robot

Now let us start building the Robot of our Arduino Line Follower. Here we are going to build a 4 wheeled robot, with 2 DC Motors connected on either side (front) and two dummy wheels on the back side. As mentioed earlier, we will be using Arduino UNO board to get input from the sensors, process them and send signals to L293D motor driver IC to drive the DC motor motor of Line Following Robot Arduino.

L293D

Below you can pin out diagram of the L293D IC. As you can see it has two pins for inputting voltage. One of them is for powering the internal circuit of the IC and the other for driving the motor.
L293d for Line Following Robot Arduino
Pin 8 – Driving the Motors – 4.5 V to 33 V Pin 16 – Working of the IC– 5V If you happen to reverse this connection accidentally, you may burn off the chip. This IC have two H Bridge circuits and so it is capable of controlling two motors individually at the same time. One side of this IC controls one motor and the other side controls the second motor. For the motor to work, the Enable pin of that side should be High. The enable pins can also be used to control the speed of the motor using PWM (Pulse Width Modulation). If you want to know more about L293D and working of H-Bridge, follow the link below. Click Here to Learn the working of an H Bridge Motor Driver So we have two wheels. How does this line follower goes forward, backward, left or right?The logic is pretty simple. When both motors rotate the same direction (clock wise or anti clock wise), the arduino line follower will move forward or backward. If both moves in opposite direction, the line following robot will turn left or right.
  • Motor 1 (Left Side) ——— Motor 2 (Right Side) —————-Robot Status
  • Clockwise—————————- Clockwise————————–Forward
  • Clockwise——————————— Stop —————————-Right
  • Clockwise————————- Anti Clockwise ——————– Sharp Right
  • Stop———————————— Clock wise ————————Left
  • Anti Clock wise——————— Clock wise ———————–Sharp Left
  • Anti Clockwise—————— Anti Clockwise ———————Backward
Simple as that. 🙂

Step 3 – The Connections

Now let us connect our L293D and sensors to Arduino Uno L293D
  • 5Vcc – 5V of Arduino
  • 5-32 Vcc2 – 12 V LiPo Battery
  • Gnd – Gnd of Arduino
Motor 1
  • Enable – D3
  • In 10 – D2
  • In 11 – D4
Motor 2
  • Enable – D5
  • In 12 – D7
  • In 13 – D8
IR Sensor (Individual) – For Method 1
  • IR Sensor —–> Arduino
  • 5V —–> 5Vout of Arduino
  • GND —–> GND of Arduino
  • Vout of IR Sensor 1 —–> A0 of Arduino
  • Vout of IR Sensor 2 —–> A1 of Arduino

The Code

int ledPin = 6;
void setup() { Serial.begin(9600); pinMode (2, OUTPUT); pinMode (3, OUTPUT); pinMode (4, OUTPUT); pinMode (5, OUTPUT); pinMode (8, OUTPUT); pinMode (7, OUTPUT); pinMode (A0, INPUT); pinMode (A1, INPUT); pinMode(ledPin, OUTPUT); }
void loop() { int s1 = digitalRead(A0); int s2 = digitalRead(A1); Serial.print(s1); Serial.print(” — “); Serial.print(s2); Serial.println(” “); if ( (s1==1) && (s2==1)) { forward(); } else if ( (s1==0) && (s2==0) ) { stop(); } else if ( (s1==1) && (s2==0) ) { right(); } else if ( (s1==1) && (s2==0) ) { left(); } else{ forward(); } } void forward() { digitalWrite(3, HIGH); digitalWrite(5, HIGH); digitalWrite(2, HIGH); digitalWrite(4, LOW); digitalWrite(7, HIGH); digitalWrite(8, LOW); digitalWrite(ledPin, HIGH); Serial.println(” Moving Forward”); } void backward() { digitalWrite(3, HIGH); digitalWrite(5, HIGH); digitalWrite(2, LOW); digitalWrite(4, HIGH); digitalWrite(7, LOW); digitalWrite(8, HIGH); digitalWrite(ledPin, HIGH); Serial.println(“Moving Backward”); } void stop() { digitalWrite(3, LOW); digitalWrite(5, LOW); digitalWrite(2, LOW); digitalWrite(4, LOW); digitalWrite(7, LOW); digitalWrite(8, LOW); digitalWrite(ledPin, LOW); Serial.println(“Stop”); } void left() { digitalWrite(3, HIGH); digitalWrite(5, HIGH); digitalWrite(2, HIGH); digitalWrite(4, LOW); digitalWrite(7, LOW); digitalWrite(8, LOW); digitalWrite(ledPin, HIGH); Serial.println(” Rotate Left”); } void right() { digitalWrite(3, HIGH); digitalWrite(5, HIGH); digitalWrite(2, LOW); digitalWrite(4, LOW); digitalWrite(7, HIGH); digitalWrite(8, LOW); digitalWrite(ledPin, HIGH); Serial.println(” Rotate Right”); }
The code is really easy to understand and if you have any queries regarding the codes, feel free ask it in the comments or in our community. Upload the code, power up, and place your Arduino Line Follower Robot in black line and see the robot in action. Had fun? In the next chapter, I will show you how to include PID Algorithm in our Arduino Line Follower to make our robot more smooth and fast by controlling the speed of the motor. Subscribe RootSaid for more awesome projects.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *