Introduction: Basic Line Following Robot With Arduino

Picture of Basic Line Following Robot With Arduino

It has been a few years since we initially posted this Basic Line Following Robot with Arduino tutorial, and it appears so many people found it helpful that we should post an update which runs current Arduino Libraries, includes the newer robot kits that assemble faster/easier, and further explain some of these steps for those who may be very new to robots and Arduino.

In this tutorial, the goal and approach is the same as before. We are building and programming a robot that can follow a lined course with the use of an Arduino based micro controller system programmed to run a basic Proportional speed control. Proportional is a simpler and shortened version of a PID control. You can visit these Wikis to learn more about both systems :

Proportional Control


Updated 3/3/2017

Step 1: Build a Course...

Picture of Build a Course...

Before you can run the Basic Line Follower, you will need a lined course. Use white poster board, foam board, white board, expanded PVC, or even a white floor or table and create a line course with black electrical tape. You may have sharp corners, but it will run smoother if the turns are gradual. Keep a minimum of 6" between any meandering lines.

Step 2: Robot Build List...

Picture of Robot Build List...

Zagros Robotics Start Kit - Magician Version (


Zagros Robotics Starter Kit - Gobbit Version (

Step 3: Robot Assembly...

Picture of Robot Assembly...

After assembling the chassis per the chassis instructions, secure the battery, Arduino and Ardumoto shield to the chassis. For the Magician we used zip ties, the Gobbit has screws and Velcro included.

Next secure the QTR sensor so that it is just slightly above the driving surface. For the Magician we used thin wire so that it was easy to adjust the sensors position but strong enough to hold it, as it might take some adjusting. Try to set the clearance between the ground and the IR sensors (the little black boxes on the QTR board) about 1/8." Also take notice of the orientation of the sensor with the header pins towards the inside as noted in the wiring diagram in the next step. Otherwise, the sensor pins will not match the Arduino sketch and will run wild. The Gobbit has a predefined location for easily screwing the QTR in place which then maintains proper distance and orientation.

Step 4: Wiring...

Picture of Wiring...

A detailed wiring diagram can be found here:

Connect the following with the leads or jumpers :

  • Ardumoto Digital pin 2 - QTR Sensor pin1
  • Ardumoto Digital pin 4 - QTR Sensor pin2
  • Ardumoto Digital pin 5 - QTR Sensor pin3
  • Ardumoto Digital pin 6 - QTR Sensor pin4
  • Ardumoto Digital pin 7 - QTR Sensor pin5
  • Ardumoto Digital pin 8 - QTR Sensor pin6
  • Ardumoto Digital pin 9 - QTR Sensor pin7
  • Ardumoto Digital pin 10 - QTR Sensor pin8
  • Ardumoto GND - QTR Sensor pin GND
  • Ardumoto 5V - QTR Sensor pin VCC
  • Ardumoto Terminal A 1 - Black wire of Left motor
  • Ardumoto Terminal A 2 - Red wire of Left motor
  • Ardumoto Terminal B 3 - Black wire of Right motor
  • Ardumoto Terminal B 4 - Red wire of Right motor
  • Ardumoto Terminal VIN (+) - Positive/Red wire from battery holder (switched)
  • Ardumoto Terminal VIN (-) - Negative/Black wire from battery holder

Note : Digital Pin 3 is skipped and used by the Ardumoto motor driver. Ardumoto also uses pins 11, 12, and 13.

Important : While the Gobbit should run proper with the connections noted, switching of the red/black motor wires, and/or the A/B side of the Ardumoto may be necessary with the Magician.

Step 5: Arduino Sketch...

Picture of Arduino Sketch...

If you are new to Arduino, or need some further detail on how to install the software and where to save Sketches, you can find detailed instructions for windows here :

Download the Sketches and Library here :

Extract the zipfile and move the " BasicLineFollower_08232015a" and “MotorDirTest_08232015a” folders to your Arduino sketches folder.

Move the "QTRSensors" folder to the Arduino libraries folder.

Plug in your Arduino/Redboard to your computer with the USB cable.

Open and upload the “MotorDirTest_08232015a” sketch.

Open the Serial Monitor and test your motors for correct wiring. See comments at the top of the sketch to help you make wiring changes if needed.

After the motors are turning correctly, open and upload the " BasicLineFollower_08232015a" sketch.

Step 6: Test and Tune...

Unplug your robot from the USB and set your robot on your lined course and turn it on. You should see the robot turn back and forth over the line and begin following.

You can "tune" some of the variables in the sketch to affect its performance. Until you understand how the sketch works, only adjust the couple of variables noted for "Tune."

The sketch is only a basic example to help you learn several fundamentals of sensing and motor control. The real fun begins as you tune some of the variables and add further refined control functions while developing an understanding of how to instruct the robot to accomplish tasks.

Step 7: Conclusion...

Hopefully, with this tutorial, you have gained some basic understandings of control methods like Proportional control and or PID which can be useful in robotics and automated control systems.

Additional information on programming and tuning with PID can be found in our Instructable :

Programming and basic loop tuning

This is a great project for Boy Scouts looking to earn their Robotics Merit Badge like these Scouts did at Scoutbotics 2016.

Another fun extension of line following is Maze Solving. With the same robot you made here, you can make it run mazes like this Instructable:

Here are a couple short videos of the robots maneuvering under the conditions we suggested previously :


1231hope. (author)2017-03-29

hello ı got 1 problem , what must ı chance for white line follower

LoucasP1 (author)2017-02-24

how did you use the qtr-8rc in fritzing?

SakinB1 (author)2016-11-19

im using the following hardwares to build this project.....where am i supposed to make a change??

1) L293D (in stead of sparkfun motor shield)

2) arduino UNO

3) QTR 8RC

i had to declare the "motor input 1/2 pins and pwm pins" for L293D, and changed code accordingly inside the sketch.

But i used only 6 from 8RC sensor pins.... The rest of ur code remained the same....

where else am i supposed to make a change in the code?

it's noted that, after I uploaded this changed code, the robot only moves on the white spaces and stops as soon as it reaches the black line......but it is supposed to do the opposite :D !

ExA_S (author)2016-11-12

i'm testing the motors but nothing happens. can't be the wiring 'cause there are 4 wires of the motors. i tried both 9v and 5v power. the shield? i can't understand how to use it.

casecaseclick (author)2016-08-15

can this be scaled up to carry something like 70lbs? I know the motors would all bee much larger, but is the tech essentially the same?

ZRob314 (author)casecaseclick2016-08-15

Yes, this concept could be scaled up to a larger robot.
The Max series have been used for this purpose:

Here is Max based project:

In most cases, you could use the same controller, motor driver and sensor (just check the current and voltage ratings)

RedwanK (author)2016-05-04

What can be used instead of Sparkfun Ardumoto shield??

Can somebody help me with the programming???

Did you figure it out?

The first sequence is a calibration. That is the rightleft repeat. After should start following if it is wired correctly and the calibration was successful.

Are you using the same hardware as the instructable?

yes, same hardware, same program, same wiring...

What robot are you using?

Have you confirmed the qtr sensor is orientated correctly with the headers/wires towards the middle of the bot and sensor located at the front of the bot?

When you say Left left left left, do you mean short corrections to the left or just spinning forever to the left?

The difference in operation between battery vs. Computer powered, is it a difference in speed only?

HakkıO (author)PrimalEng2016-04-26

hi, it seems like i'm having the same problem. my robot when powered by computer does the calibration sequence then spins constantly to left, when powered my battery it just spins to right in short bursts.

PrimalEng (author)HakkıO2016-04-27

It sounds like a wiring problem. Double check each wire.

What hardware and robot are you using?

dose the black line width matters???

What works well is black vinyl electrical tape, which is about 3/4" wide. Very narrow may be problematic. Super wide (nearly as wide as the entire sensor array) will probably be more difficult for smooth tracking with the basic code.

The program is also set for a white background with dark tape. You can change the code if you want the opposite.

VVVBATMAN (author)2016-03-30

If I wanted to change the speed of the robot, where in the code would I do that?

PrimalEng (author)VVVBATMAN2016-04-06

Look in the follow_line() function. There are values of 55 and 200 you will see spread through that function. Try adjusting those or rewrite it to just slow down one wheel while the other runs at full.

Hi, I have made all the mechanical stuff, but am facing difficulty with software. Robot is running weirdly. RIGHT, LEFT, RIGHT, LEFT, LEFT, LEFT, LEFT. Not at all following the line. By the way, robot is running in a different way when connected to computer, and running differently when connected to Battery case for power supply.

Please help me.

HannahM56 (author)2016-02-11

Does your code have the same basic format for if I were doing the same project but on a mbed instead of arduino?

Apple OS X guide (author)2016-01-09

how to make it using the official motor shield and magician chassis.

bass.shuru (author)2014-12-14

hi! i want to use Motor Driver 1A Dual TB6612FNG instead of ardumoto sheild , my question is will it work fine with this robot chassis or there will be any compliacations ?? pleaseeeee reply!!!!!

ZRob314 (author)bass.shuru2014-12-15

You should be able to use any motor driver you want that will meet the motor requirements (6vdc, 250mA max). The example program will need to be modified to work with your motor driver.

hsingh1 (author)ZRob3142015-11-18

Can i use adafruit motor shield? Insted of adumoto?

pablo1211 (author)2015-11-14

Hey, I've got a problem. I did everything as it' s said in the tutorial but the robot is keep moving forward istead of following the line. Can someone help me?

roziea (author)2015-10-18

i had connect the arduino n 10A motor shield to power window motor with additional 6v battery, why my speed motor are different between Left and Right

roziea (author)roziea2015-10-18

please help me

roziea (author)roziea2015-10-18

please help me

fuqthislab (author)2015-04-25

I am having trouble with powering the ardumoto. Where does the Vin for the ardumoto go to??

jscottb (author)fuqthislab2015-04-30

Vin is to power the motors from the L298. It has a max input of I believe 18vdc.

Here is tutorial on the board. Ignore the part where it says Vin is optional! If you really want you bot to move, it's not. If you are powering the yellow gear motors or micro gear motors, I recommend using at least 5 rechargeable AA or a 6v NiMH or NiCAD battery pack.

EstefaniaC (author)2015-03-06

Hi I am trying to do your project. I cant add the library to arduino. some people said they had to update the file to arduino.h, no sure what you mean by that. It would be great if you couls help, thax.

Hey! Sometimes it's helpful to scroll through the comments ;)
Do you get an error like the one described by SoonK further down?
Then this should solve it:

The problem appears, beacuse the code is for an older version of the Arduino program.
have to go into the imported library and open the files "Button.h",
"engine.h" and and "navigation.h" and in each one replace the line
#include "WProgram.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#include "WProgram.h"

will solve the errors above, but a new error will occur saying theres
no "wiring.h". To solve that problem go into the file
"PololuQTRSensors.cpp" and replace
#include "wiring.h"
#include "wiring_private.h"

JackC4 (author)2015-03-03


I want to know to program a bot when it approaches a t section.for eg. if bot approaches 1st t section then it should go forward and when it approaches 2nd t section it should turn left or right

chico.gomez.12 (author)2015-01-11


I am trying to do this project and could quite download the entire code. I am using the exact same parts. I am also using ArduinoUno R3 IDE1.0.6. Every time I try to open the the code it gives me an error that says that the file QTTRC Line follow needs to be inside a sketch folder Qttrc_Line_follow --> how do you do this?

Just make folder named "QTRRC_Line_Follow" and put the file inside... then it will open.

osas201 (author)2013-05-14

Thanks for your help, finally got the robot working fine. check it out! lol

trupeshr (author)osas2012015-02-01

can you give me code?

u3016798 (author)2014-10-27

Hi, Nice project!

I have replaced all wprogram.h and wiring.h references with arduino.h to bring the sketch up to spec with IDE 1.0.6, it is all verified and OK.

I am at the stage where I have uploaded the code and the robot seems to not be able to detect my line, it simply keeps driving forward (which i think is no-line detected behaviour). I have the sensor mounted about 3mm from the surface.

Any help would be greatly appreciated.


ZRob314 (author)u30167982014-10-30

Did you go through the line sensor calibration procedure?

u3016798 (author)ZRob3142014-10-30

Thanks for the response,

Yes I did, I found that the corrections it was making were too small to detect when it was travelling as fast as it was. I reigned it back in to a slower speed and saw better results.

ricardo64 (author)u30167982015-01-30

Hey u3016798,

I also have my robot working but not following the line. How did you slow it down? Which program and what exactly did you change? Thanks in advance.


nkarlson (author)u30167982014-12-16


when you changed it to arduino.h, is this library included in initial download or do you have to also find the arduino.h library?

SoonK (author)2014-09-29

i keep getting this error. i have already downloaded the library, and I am using the code that was in the QTRRCExamples folder.

In file included from QTRRC_Line_Follow.pde:1:

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/Button.h:23:22: error: WProgram.h: No such file or directory

In file included from QTRRC_Line_Follow.pde:1:

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/Button.h:34: error: expected `)' before 'buttonPin'

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/Button.h:42: error: 'uint8_t' does not name a type

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/Button.h:43: error: 'uint8_t' does not name a type

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/Button.h:44: error: 'uint8_t' does not name a type

In file included from QTRRC_Line_Follow.pde:3:

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/engine.h: In function 'void engineSetup()':

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/engine.h:23: error: 'OUTPUT' was not declared in this scope

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/engine.h:23: error: 'pinMode' was not declared in this scope

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/engine.h: In function 'void motor(int, int)':

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/engine.h:33: error: 'HIGH' was not declared in this scope

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/engine.h:41: error: 'LOW' was not declared in this scope

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/engine.h:45: error: 'analogWrite' was not declared in this scope

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/engine.h:46: error: 'digitalWrite' was not declared in this scope

In file included from QTRRC_Line_Follow.pde:4:

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/navigation.h: In function 'void calibrate(PololuQTRSensorsRC*)':

C:\Users\Student\Documents\Arduino\libraries\PololuQTRSensors/navigation.h:21: error: 'delay' was not declared in this scope

plz help

re.becca.58910 (author)SoonK2015-01-26

You probably solved it meanwhile, but I'll answer anyway for anybody who has the same problem (like me):

The problem appears, beacuse the code is for an older version of the Arduino program.
You have to go into the imported library and open the files "Button.h", "engine.h" and and "navigation.h" and in each one replace the line
#include "WProgram.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#include "WProgram.h"

This will solve the errors above, but a new error will occur saying theres no "wiring.h". To solve that problem go into the file "PololuQTRSensors.cpp" and replace
#include "wiring.h"
#include "wiring_private.h"

That should do it, worked for me!

trupesh.rupareliya (author)2015-01-24

if i chang th color of line for ex (white line and black background )what is chang in program?

chico.gomez.12 (author)2015-01-11


I am trying to do this project and could quite download the entire code. I am using the exact same parts. I am also using ArduinoUno R3 IDE1.0.6. Every time I try to open the the code it gives me an error that says that the file QTTRC Line follow needs to be inside a sketch folder Qttrc_Line_follow --> how do you do this?

NitishK2 (author)2015-01-10

for coding of line follower click on:

AnindyaB (author)2014-12-21

i want to ask you guys that can I use the same code which is for arduino in freeduino??please reply

ZRob314 (author)AnindyaB2014-12-22

We are unfamiliar with the Freeduino, but if it uses the Arduino development environment and has the same I/O it should work.

nkarlson (author)2014-12-16

Even though Button.h is included in the library you provided, it is not included in the sketch. Is there a reason you included it?

Praveen Kumar ReddyG (author)2014-11-16

tell me the components used in making the line following robot

About This Instructable




More by ZRob314:Turn on an LED With Watson ConversationSt. Patrick's Day "Pinch Detector" With Circuit PlaygroundPiano Pi - Play a Duet With Your Pi!
Add instructable to: