How to Make an Autonomous Line-Following Robot | Arduino


In this post, I’ll show you how to make an autonomous line-following robot using Arduino and a reflectance sensor.

Shout out to the late Gordon McComb for this project idea. He is the author of an excellent book that I recommend buying if you’re getting started with robotics: How to Make a Robot.

Real-World Applications

Here is a real-world application of a line-following robot. Elon Musk is inside the Tesla factory and describes how one of their robots follows a line in order to navigate across the factory floor.


Here are the requirements:

  • Make an autonomous line-following robot using Arduino and a reflectance sensor.

You Will Need

The following components are used in this project. You will need:


First grab the 0.25 inch thick foam board. Cut it into two 3/8 inch by 3/4 inch pieces.


Glue both pads so that one is on top of the other.

Glue the stack of two pads to the underside of the board so that the middle of the stack is exactly in the center of the lower board.

The stack should be right in between both snap action switches. The front of the stack should be slightly behind the front edge of the board.


Grab the header pins that came with the reflectance sensors. Cut the pin headers so that you have two sets of three pins. One set of three will be for one of the sensors and the other set of three will be for the other sensor.


Insert the short end of the header pins into the reflectance sensors.


Solder the short end of the header pins so that the header pins remain in place. Be careful not to solder big blobs that connect one short pin to another. It is a difficult soldering job because the pins are so close together. Just take your time. No need to hurry.


Now grab the following 6 male-to-female jumper wires and hook up the female part as follows:

  • Black, red, yellow (Right sensor)
  • Green, orange, white (Left sensor)

Get a piece of double-sided tape and attach the sensor to the underside of the board on top of small foam board stack.

The front of the sensor should be aligned with the front of the robot.


Connect the 6 male-to-female jumper wires to the breadboard as follows:

  • White wire connects to c23
  • Orange wire connects to h12
  • Green wire connects to GND via h5.
  • Yellow wire connects to c25
  • Red wire connect to g12
  • Black wire connects to GND via h29
  • Connect a wire from a23 to analog pin 4 on the Arduino
  • Connect a wire from a25 to analog pin 5 on the Arduino

If you have issues with the servos not moving, try connecting the ground on the reflectance sensor (the green and black wires) to their own ground on the Arduino.

Upload the following code to the Arduino board to test the reflectance sensors.

 * This is a test of the reflectance
 * sensor of the line following robot.
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-15

#define line_left A4 // The IO pins connected to sensor
#define line_right A5

int irleft_reflect = 0; // Readings stored here
int irright_reflect = 0;

 *  This setup code is run only once, when 
 *  Arudino is supplied with power.
void setup() { 
   Serial.begin (9600); // Set the baud rate

 * This is the main code that runs again and again while
 * the Arduino is connected to power.
void loop() { 

  // Read the reflectance sensors
  // Values range from 0 to 1023, representing
  // analog voltage from 0 to 5 volts
  // 0 = solid white; 1023 = solid black
  irleft_reflect = analogRead(line_left);
  irright_reflect = analogRead(line_right);

  Serial.print ("Left:");      
  Serial.print ("\t");        
  Serial.print (irleft_reflect);
  Serial.print ("\t");
  Serial.print ("Right:");
  Serial.println (irright_reflect);

Open the Serial Monitor to verify that the values are changing when you wave an object in front of the sensors.

Feel free to close down the Serial Monitor at this stage. We will now make the line course that the robot will follow.

Grab the white poster (22 inches by 28 inches), and also get the 0.75 inch black electrical tape.

Create a course that looks something like the image below. Be sure to keep at least a three-inch margin between the side of the poster board and the line course. Make sure the turns and the course are smooth, otherwise you will have problems.


The course is ready, so upload the following sketch to the Arduino.

#include <Servo.h> // We are using servos, so add library

 * This robot follows a line made with black electric
 * tape on white poster board.
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-15

// Each servo is an object with its own data and behavior
Servo left_servo;
Servo right_servo;

// Sensors connected to analog pins 4 (left sensor) and 5 (right sensor)
const int left_line = A4;
const int right_line = A5;

// Store sensor readings here
int irleft_reflect = 0;
int irright_reflect = 0;

// Try values between 400 and 800. 
// Helps determine if robot is over the line
int threshold = 800;           

 *  This setup code is run only once, when 
 *  Arudino is supplied with power.
void setup() { 
  // Assign each servo to its own digital pin on the Arduino

void loop() { 

  // Read the reflectance sensors
  irleft_reflect = analogRead(left_line);  
  irright_reflect = analogRead(right_line);

  // robot is right over the line
  if (irleft_reflect >= threshold && irright_reflect >= threshold) {

  // robot is veering off to the right
  if (irleft_reflect >= threshold && irright_reflect <= threshold) {

  // robot is veering off to the left
  if (irleft_reflect <= threshold && irright_reflect >= threshold) {

  // If robot has lost the line, go find it
  if (irleft_reflect < threshold && irright_reflect < threshold) {

// On the continuous rotation servo, the write() 
// method sets the speed of the servo.
// 0 is full speed in one direction.
// 180 is full speed in the other direction.
// ~90 is no movement (You will have to tweak to
//   get no movement).
void line_forward() {
void line_right_slip() {
void line_left_slip() {
void line_right_spin() {
void line_left_spin() {

Disconnect the USB cable from the Arduino, and place the Arduino on the floor.

Turn on the servos:

  • a13 to e3 = left servo ON
  • e13 to e28 = right servo ON

Plug in the Arduino’s power.

Press the Reset button on the Arduino as you position the robot over the black electrical tape on the course. The Reset button is like unplugging the Arduino and plugging it in again.

Watch the robot in action!