Raspberry Pi Pico based Line Following Robot
366 5
Arijit Ghosh

Raspberry Pi Pico based Line Following Robot

This Robot can follow any line and can reach its destination automatically, by tracking any...


This Robot can follow any line and can reach its destination automatically, by tracking any line. This can be used to move to places in factories and industries to move anything without any control of a user or computer. This moves based on the sensor data that the IR Sensor gives as output. The sensor works on the principle of reflection and refraction of light. Infrared light bends more as compared to visible light (i.e. VIBGYOR). And we know that black colour absorbs most of the light which is falling on it (Basically black colour is anything that we cannot see, there is no colour, called black). Similarly here also when the IR Led is perpendicular to the line, it absorbs and does not reflect the light black. Whereas when the IR Led is perpendicular to any other colour, it reflects back the Infrared light. When the left sensor reflects the Infrared light, and the right does not, it means that the robot must move towards the right and vice versa. Again when both the sensor reflects the Infrared light, the robot must move straight and when both the sensor does not reflect any Infrared light, the robot stops moving. By this principle the robot works.

Line Follower

Hardware Used : 

  • Raspberry Pi Pico
  • L298N Motor Driver
  • 2x IR Sensor
  • 12V Battery
  • 4x Motors
  • 4x Wheels


Software Used:

Thonny IDE

Hardware and Software Selection :

For this project I have selected the Raspberry Pi Pico because it is a newly launched board with much more advanced features. Before the launch of this new microcontroller board, I was experimenting with various arduinos for building a line following robot. Then finally I realized it will be better to use a small arduino board or ony microcontroller IC such as Atmega328P for this project, since it will reduce the size and weight of the robot. This will also consume less current compared to others.
But after the launch of this new advanced board, in the past January, 2021 I planned to make the robot with Raspberry Pi Pico. The first and the foremost reason behind this is that this board will consume very low current, besides the most amazing fact is that this board has a on-board buck-boost converter that enables us to use this board in just a very limited amount of voltage, as low as 1.5 V.
Now for the software in case, we can use either Thonny IDE or Arduino IDE. But I have chosen Thonny IDE because this IDE enables us to use the Micro Python and Circuit Python for programming. Since Python requires very less coding compared to Arduino and C++, I have chosen this platform for programming.


Circuit Diagram Explanation

Line Follower Circuit

Here the two IR Sensors are connected to the Gpio pin 2 and 3 of the Pi Pico. The Motor Driver pins are connected to the Gpio Pins 10,11,12 and 13 of the Pi Pico. The enable pins are connected to the Gpio pins 6 and 7 of the Pi Pico. These Pins exchange PWM signals between the Pi Pico and the Motor Driver.  

Hardware Explanation :

I have chosen the Raspberry Pi Pico because it is way better as it has a clock frequency of 133 MHz compared to the Arduino Nano which has a clock frequency of 16 MHz. Also it has many more features like the built-in Temperature Sensor(although it has no use in this project).
So in comparison with Arduino Nano, the Raspberry Pi Pico is a way better option to choose. The IR Sensor can be replaced with other kinds of Lidar Sensors.


# Raspberry Pi Pico based Line Following Robot

from machine import Pin,PWM #importing PIN and PWM

import time #importing time

import utime

# Defining motor pins





# Defining enable pins and PWM object




# Defining  right and left IR digital pins as input

right_ir = Pin(2, Pin.IN)

left_ir = Pin(3, Pin.IN)


# Defining frequency for enable pins




# Setting maximum duty cycle for maximum speed




# Forward

def move_forward():






# Backward

def move_backward():






#Turn Right

def turn_right():






#Turn Left

def turn_left():







def stop():






while True:


    right_val=right_ir.value() #Getting right IR value(0 or 1)

    left_val=left_ir.value() #Getting left IR value(0 or 1)




    # Controlling robot direction based on IR value

    if right_val==0 and left_val==0:


    elif right_val==1 and left_val==0:


    elif right_val==0 and left_val==1: