Line Follower Robot using AVR Microcontroller ATmega16

Published  February 28, 2019   0
Line Follower Robot Using AVR Microcontroller ATmega16

Here we will be building yet another project with AVR Atmega16 Microcontroller. If you are new to AVR microcontrollers then you can refer to previous AVR projects and tutorials.

Building a robot is always a thrill for all electronics hobbyists. And this thrill can be maximized if the robot can automatically do some things without any external instructions. One of the most commonly built robot by electronics beginners is Line Follower Robot. As the name suggests robot will follow line drawn on the surface. The line need not be a straight line. Also, lines can be of any colours.

We previously built Line Follower Robot projects using different Controllers:

  1. Line Follower Robot using 8051 Microcontroller
  2. Line Follower Robot using PIC Microcontroller
  3. Line Follower Robot using Arduino
  4. Line Follower Robot using Raspberry Pi
  5. Line Follower Robot Using MSP430 Launchpad


Today we will use AVR microcontroller to build a Line follower Robot.

Mostly IR sensors are used to detect lines. The IR sensors are good in detecting white or black surfaces. Although you can use other sophisticated sensors which will be able to detect all colours and can make a robot which can follow all colour lines. The robot should be able to detect line even if the path is altered by changing the angle and keep following the line. Also, it should stop wherever the stop zone comes in the line.              

Line follower robots are used extensively now a days in manufacturing industries, medical, domestic applications and goods warehouse. The robots are not limited to only these applications and can extend its applications in many future applications.


Concept of Line Follower Robot

The IR sensors are key player in this project. IR sensors are placed in front side of the robot to track the drawn black line and the surface. The robot is placed in between the line and with the help of IR sensors the robot keeps track of the line. IR sensors feedback the reading to microcontroller and with the help of IR reading, the microcontroller move motors left side or right side and again brings the robot to the path.

Line Follower Robot is able to track a line with the help of an IR sensor. This sensor has a IR Transmitter and IR receiver. The IR transmitter (IR LED) transmits the light and the Receiver (Photodiode) waits for the transmitted light to return back. An IR light will return back only if it is reflect by a surface. Whereas, all surfaces do not reflect an IR light, only white the colour surface can completely reflect them and black colour surface will completely observe them as shown in the figure below. Learn more about IR sensor module here.

Concept of White Line Follower Robot

Concept of Black Line Follower Robot

IR Sensor Module


Now we will use two IR sensors to check if the robot is in track with the line and two motors to correct the robot if its moves out of the track. These motors require high current and should be bi-directional; hence we use a motor driver module like L293D. We will also need a Microcontroller like ATmega16 to instruct the motors based on the values from the IR sensor. A simplified block diagram of the same is shown below.


These two IR sensors will be placed one on either side of the line. If none of the sensors are detecting a black line then AVR microcontroller instructs the motors to move forward as shown below

Working of Raspberry Pi Line Follower Robot

If left sensor comes on black line then the microcontroller instructs the robot to turn left by rotating the right wheel alone.

Turning left to line follower robot using Raspberry Pi

If right sensor comes on black line then the microcontroller instructs the robot to turn right by rotating the left wheel alone.

Turning right to line follower robot


If both sensors comes on black line, robot stops.

Stopping Arduino Line Following Robot


This way the Robot will be able to follow the line without getting outside the track. Now let us see how the circuit and Code looks like.


Components Required

  1. DC Gear Motors(2 units)
  2. IR sensor module(2 units)
  3. L293D Module
  4. Wireless Power Source(e.g. Battery, Power bank)
  5. Robot Chaises
  6. Atmega16 Microcontroller IC
  7. 16Mhz Crystal Oscillator
  8. Two 100nF Capacitors
  9. Two 22pF Capacitors
  10. Push Button
  11. Jumper Wires
  12. Breadboard
  13. USBASP v2.0(To program Atmega16)


Circuit Diagram

Connect all the components as shown below in the Circuit Diagram

Circuit Diagram for Line Follower Robot Using AVR Microcontroller ATmega16


Programming Atmega16 for Line Follower Robot

Here the Atmega16 is programmed using USBASP and Atmel Studio7.0. If you don’t know that how Atmega16 can be programmed using USBASP then visit the link. Complete Program is given at the end of the project, just upload the program in Atmega16 using JTAG programmer and Atmel Studio 7.0 as explained in previous tutorial.


We have tried to keep the code as short and simple as we can. So we are using Macros and Special function registers in this code.

if(bit_is_clear(PINA,leftSen)){        // check if left sensor is OFF


This statement checks if the PA0 where left sensor is connected is LOW or HIGH. 

If you want to learn more about this statement and other macros then go to this link

There are four conditions that we are following to read trough IR sensors. IR sensors are named according to their placement in frontside of the Robot. The left IR is Left sensor and right IR is right sensor. The following conditions will decide the movement of robot.

IR Sensor Setup for Line Follower using Atmega16


We have written the code according to above conditions. Only change is that we are using only two input pins of the L293D to drive both motors.

Complete code with a demonstration Video is given below.

/* Line Follower Robot Using Atmega16
   Circuit Digest( */
#define leftSen PA0                //Connect Left Sensor At PA0
#define rightSen PA1               //Connect Right Sensor At PA1
int main(void)
DDRA=0xFC;                // make PA0,PA1 as input for both sensors 0x0b11111100
DDRC=0xFF;      // make Port  as output to connect motor pins 
 PINA=0x03;             //initialize PA0 and PA1
 if(bit_is_clear(PINA,leftSen)){        // check if left sensor is OFF 
   if(bit_is_clear(PINA,rightSen)) {    // check if right sensor is OFF
    PORTC=0b00000000;       // if both sensor zero 
    }       // then stop the robot
    PORTC=0b00000001;                  // if right is ON then take left
 else                                  // check if left sensor in ON
   if(bit_is_clear(PINA,rightSen)) {   // check if right sensor is OFF
    PORTC=0b00000010;      // it means left sensor is ON
}      // so take right
    PORTC=0b00000011;            // if both sensor is ON 
       }     // then keep moving the robot