Monday , January 23 2017
Home / Robotics / Robot Theory / Programming a Line Follower Robot

Programming a Line Follower Robot

Programming a Line Follower Robot is the next step that you should do after building it. This is a follow-up on my previous post Line Follower Robot – Build it from scratch. If you haven’t already read it, I suggest you read it before going any further in this post.

When it comes to programming a line follower robot doesn’t mean you can just copy paste a bunch of code from somewhere and say you are done programming ūüėõ You have to calibrate the code to work properly with the hardware you have built. So the first step in programming is to make a demo track. This will help you test your code then and there. Here is the track that I built for my line follower.

Line Follower Track

The line is little over 3.5 cm in width and has almost all possible turns and bends. I used a black chart paper to make the track and stuck it over white one. You can come up with you own innovative ideas but this is the most straight forward method I can think of. I you have a better Idea leave a comment.

The next step is to sit with a pen and paper and make reading from the track that you just made. If you had used the circuit diagram that I posted or something similar, you should be having some indication LED’s at the output of the comparators like this one here,

Line Follower sensor

I placed the output LED’s¬†(green) directly above the line sensor to avoid confusion. This made my board look really bad. Again was a trade-off between the look and the feature, I choose the feature. It is really helpful if those LED’s are right on top of the sensors.

How is the line is sensed?

The IR emitter emits a constant IR beam. The white surface reflects most of the beam while the black surface absorbs most of the beam. This reflected beam is picked up by the IR detector and its conduction increases and hence a voltage variation in the output pin (0v for absence of IR rays and 5v for maximum intensity). This is given to the comparator to compare with a reference signal generated by the potentiometer. That is how the line is sensed by the sensors.

Back to the post, you have to place the bot over the line and see what it reads for various position of the line. Make sure that the line always detected at least by one sensor if is within the range of the three sensors. This is something you should take utmost care. I even mentioned in my previous post the maximum distance between the sensors should not exceed the width of the line.

With the calibration values at hand, you can start programming you robot. In your while(1) loop you should have a series of if statements which polls of these calibrated condition and make direction changes accordingly.

Let’s assume these are the only four possible conditions for the sake of understanding. The red dots are the sensors that are just above the ¬†black line and the green ones are on the white background.

line follower robot logic

Case 1 is when the robot has to move front as only the center sensor is low. Similarly Case 2 is right, Case 3 is left and case 4 is stop as all the sensors read high. This is how your code should look like (the terms, LEFT_SENSOR, RIGHT_SENSOR, CENTER_SENSOR, GO_FRONT, TURN_RIGHT, TURN_LEFT, STOP are all code macros for the corresponding pin in the microcontroller).

This code should be good for any linear track with normal bends. But this is not going to work if your track has right and acute angle turns. That is when we have to add some more logic to the existing one.

Say you are facing a right angle, what would your sensor data read? The center sensor along with either of the two side sensors will read LOW. This is when you have to tweak the direction logic a little.

line follower robot logic

Now you have to define two kinds of left turns. One of it will rotate your right motor forward and the left motor backward. Call this one “Power Left” as it turns left quite fast. The other one is to rotate the right motor forward while the left one¬†doesn’t rotate at all. Call this just “Left”. Do the same thing with the right turn logic.

For normal conditions use the “Left” or “Right” turn. When the bot encounters a right turn or an acute turn use the “Power Left” and “Power Right” this way you bot will respond faster to these cases and act accordingly. Now your code should have these lines too,

By adding this logic to your¬†controller¬†you are able to¬†tackle¬†all kinds of bends and turns in a linear track, that is you won’t be able to use it on tracks that have intersections and¬†discontinuities. Almost none of the event in today’s trend gives you such a simple track. Don’t get annoyed we will discuss some the most common track and how to solve them in my later posts.


Most of the time your robot will have “T” and “X” junctions in the track. You might have to survive a finite discontinuity in track or detect¬†patterns¬†in track to do some other tasks (such as glowing an LED or setting up an alarm in the buzzer). Some of them even have inversion in logic (you will be asked to follow white line on¬†black¬†background all of a sudden :-)).

This is just the beginning of line follower robot. There are a lot of possible difficulties that can be set up for you in a contest. All cannot be discussed here. But we will discuss some of the most common one (listed above) in the posts to come.

We will specially give importance to the “Shortest Path solving robot” in the near future. Your robot has to go from the source to the destination by travelling any distance but return to the source from the destination but the shortest path only. This is the kind of robots most people have difficulty in programming and there aren’t many tutorials online for its kind.

Don’t miss the exciting new posts that are yet to come. Subscribe to and get our latest post delivered to your inbox.

About Siddharth

Siddharth is a Firmware Engineer, techie, and a movie-buff. His interests include, Programming, Embedded Systems, Linux, Robotics, CV, Carpentry and a lot more. At times, you could see some of his sunday projects converge on release quality. You get to know him on the following social channels.

Check Also

robots chassis

How-to: Make a Chassis For Your Robot

Readymade chassis that are available in the market (like the one above)¬†are really good, cheap …

  • Ganapathi

    I am just a beginner in robotics..I need more clarity in how the sensor logic works..! U say green dots are on white and red dots are on black.. so it means for the bot to turn right there must be two red dots; one on the center line and other on the right line(center_sensor and right_sensor) and 1 green dot where there is no path for the robot to go..Am i right or i ve misunderstud sumthin.?

    • Hi Gnanpathi, don’t worry you’ve got the logic right ūüôā All the best!!

      • plz give me the complete code for that given project

  • gopi jaganath

    Hi I am Gopi Jaganath.i want the program for single sensored robot.plz reply me as it is urgent

  • varsha

    Please, send me the programme for track using Timer-Counter.
    Also send a programme for white line follower and black line follower.
    I m attaching the track with this mail.
    Please check and replay me.
    Thank You,
    varsha mali

    • Dear Varsha,

      What do you mean by “programme for track using Timer-Counter”? I don’t really get you. I have already give the code and its explanation for a black line follower try to modify that code and use it for the white line follower as well.

  • Shashank

    dude send me the full program….

    • Sashank, right now I’m travelling so cant send you the code. But why do you need it? I think I explained the logic really well so that you’ll be able to write your own code..

  • karan arora

    plz sir send me the c++ programming language for line follower bot.(atmega 16a).thank you.

  • Rajeev

    i need your help to operate my embedded system from a page(software) designed in dotnet…??

  • Manjeet

    Dear sir
    i need a 89xx programmer. can you provide?

    • Manjeet, by 89xx you mean the Philips 8051 controller p89v51? if so there is no need for any external programmer for it. It can be serially programmed with a tool called flash magic.

  • i am nikhil and i am happy with your explanations but the one doubt is that vat is better way to set up sensors i..e,to set them in a array form or in a v shape

    • Nikhil, each type of arrangement has different pros and cons. From my experience a linear array of sensors works just fine for all types of tracks. There are some drawbacks for the linear arrangement but they can be overcome with well written software.

  • harshal

    hello m from IT filed i want embeded c programming learning material..and please help me to build fast line follower robot

  • appkee

    give line follower program.

  • Pingback: inventorArtist » Little Rolly Robot()

  • rahul varma

    i find it useful.can you explain me how can we use PID algorithmic code ,so that it can move at fast speeds minimising the errors

  • devesh rai

    sir i have a development board of philips at89c51 i want the software of pl2303 serial to parellel converter for wiindow 8 plese mail me at


    Good work my friend but if the line is in 8 what will the robot do?? So I think that putting 4 sensors will be greater…

  • Hassan

    hi..i just made the hardware but having problem like how to connect my motor driver IC with the 18f controller..i am using port D as an output of the pic..kindly help me with it and if you can provide me with your will be helpful for me !! thank you in advance !!

    • I have posed a lot of contents on the l293D motor driver please refer to them for more clarity. You will have to connect the sensors to some of the port pins of your controller and define them as input. Then you will have to write some code macros that map the PORT pins to each sensor. After that you can just use the same code I have provided.

  • Livius

    Ehi, i have some problem, can i have the circuit schematic? Thanks
    Sorry for bad english

    • Hi Livius, I do not have circuit for the line follower robot. But I have posted enough materials in this blog to make more that just line follower robot. I suggest you read thorough them.

  • sabharie raam

    consider a node from which two left turns were there and i need to follow the second left track…how to use the logic….thank you

  • Nisha Singh

    Thanks. It was really helpful for me as a absolute beginner.

  • barńĪŇü

    plz give me the complete code for that given project

  • Aswin Ramadas

    i ve made a lfr usind arduino
    also i use a l293d driver for the ordinary dc hobby motors
    i also wrote the program for the bot
    now the problem is that the bot is running when lifted from the ground ,but it doesn’t when kept on the ground
    i use two lithium ion batteries in series having 3400mah
    can someone pls help me !!!!

  • Ankit Chopra

    sir i m using BDF 1000 5 tracking sensor for my line follower robot.
    can you please provide me code for that.
    my maid id is

  • Harish Raj. V

    hi, friends I need the possibility that come in line follower robot. And please friends send any one send me the coding for the possibility. please friends …..

Keep in touch with the current trends!
Did you like this article? Sign up and get our latest posts delivered to your inbox!
  We hate spam and never share your details.