Raspberry Pi Pico: Line Following Robot – MicroPython

A Line Following robot is a popular beginner-friendly tutorial for all microcontrollers. It is a simple and fun way to learn to code. In this blog, we will see how to build a line follower robot using Raspberry Pi Pico Microcontroller and Python.

Working Principle of IR Proximity Sensor:

Black is the color that absorbs light and all other colors reflect some light back based on their color (most reflecting color is pure white). Every color has a wavelength, our eyes can be visible only in a specific range of wavelengths only. One example of an invisible wavelength for our eyes is called Infrared Light. We can’t see infrared light with our naked eyes (but can by our mobile phone with poor IR Filters).

IR Proximity Sensor or Infrared Proximity Sensor is a kind of proximity sensor that has an IR Transmitter LED and an IR Receiver LED. The transmitter LED emits an Infrared Light and the receiver LED senses infrared light. So, there will be no reflection when there is no object (or) black object in front of an IR Proximity Sensor. However, if there is any other color, the infrared light gets reflected back by the surface to the IR Receiver LED.

By the above Infrared Principle, we can make a robot that can sense the black line and keep it away from the black line on both sides, so that it will stay on the line.

Components Required:

Useful Resources:

Circuit Diagram for Line Following Robot:

line following robot circuit

Note: If you are using 4 Wheel Drive, You can connect the motors in parallel on each side. As we are using an L298N driver (2Amps Max Current), it can handle 4 BO Motors (Yellow). When mounting on the chassis, the left two motors are from M1 and the Right two motors from M2. This way both wheels run in the same direction. You can check the video for a reference of rotation.

Line Follower Robot Code (Python – Raspberry Pi Pico):

from machine import Pin,PWM #importing required PIN and PWM from modules
import time #importing time module
import utime

#Define and map Pico pins with L298N Motor Driver Pins as OUTPUTS
IN1=Pin(9,Pin.OUT)//IN1 Motor Driver
IN2=Pin(8,Pin.OUT)//IN2 Motor Driver
IN3=Pin(7,Pin.OUT)//IN3 Motor Driver
IN4=Pin(6,Pin.OUT)//IN4 Motor Driver

# Defining enable pins and PWM object
EN1=PWM(Pin(10))// Enable Pins used for PWM Speed Control
EN2=PWM(Pin(5))//Enable Pins used for PWM Speed Control

#Set Pins for IR Sensors as INPUTS
right_ir = Pin(3, Pin.IN)//Right IR Sensor
left_ir = Pin(2, Pin.IN)//Left IR Sensor

#Set PWM Switching Speed in Hertz for EN Pins

#PWM Duty Cycle
EN1.duty_u16(65025)//Maximum for Pico 65025
EN2.duty_u16(65025)//Maximum Pico 65025

# Move Forward
def move_forward():
# Move Backward
def move_backward():
#Turn Right
def turn_right():
#Turn Left
def turn_left():
def stop():
while True:
    right_val=right_ir.value() #Read Sensor 1 Value
    left_val=left_ir.value() #Read Sensor 2 Value
    #Set Conditions for Obstacle Avoidance
    if right_val==0 and left_val==0:
    elif right_val==1 and left_val==0:
    elif right_val==0 and left_val==1:

Upload and AutoRun the Code on Pico:

You can save and run this file locally by any name. But if you want to auto-run your code on Raspberry Pi Pico, the name should be main.py only. You can save the file by clicking File> Save As > then select Raspberry Pi Pico on the popup (Pico should be connected). Then save the file as main.py

We have to draw a black line using black chart paper or black tape (avoid reflecting/shiny tapes). We will be placing the sensors away from this line. One on the left and another on the right side of the line.

Note: Most digital sensors will be in HIGH State(1) at normal conditions and LOW State(0) when something is detected. This is because electrical noises in the surrounding causes false trigger. To avoid this, Pull-Up resistors are used in most of the sensors.

The function of the code and Robot:

Forward Sensing: By default, the white surface on both sides, both sensors detected (LOW, LOW), and the motors run in the forward direction.

Right Sensing: When the Right Sensor is detected the black line (LOW, HIGH), the motor rotates in the Right direction.

Left Sensing: When Left Sensor is detected the black line (HIGH, LOW), the motor rotates in the Left direction.

Stop: When Both Sensors detected the black line (HIGH, HIGH), the motors stopped.

Sensor Logic:

FunctionLeft SensorRight Sensor

Motor Logic:


By this logic, the robot follows the line until the line ends.


If your motor runs in the opposite direction, there are multiple ways to correct it. You can change the pin numbers on the code, Interchange Terminals of the Motor. Below is how to change via interchanging terminals.

  • You can interchange motor terminals on M1 (or) M2 in the motor driver. Important! You must interchange terminals on the same side only. Do not connect one terminal to M1 and another terminal to M2. Otherwise, it will create a short circuit and can damage the L298N Driver.
  • If your sensor detects the opposite direction, the simplest way is to interchange the mounting of sensors without any changes in wiring or code.

Recommended Batteries and Chargers for robots:

Motors consume more power, so it is recommended to use Lithium Ion Batteries for the best performance, recharging, and long shelf-life. The BO Motors we use is rated at 3-12Volt DC, 40-180mA. For our robot, 7-12V is recommended for better performance. You can use a cheap 9v battery, but it will not last long and is not rechargeable. So go with rechargeable lithium-ion batteries.

Lithium batteries connected in series add up the voltage, for example, 1 lithium cell’s nominal voltage is 3.7 Volt, and for the 7.4 Volt requirement, you need a 2 Cell 18650 Holder. For our robot, this is enough. So you can use a 2Cell or 3 Cell Holder. Below are the recommended battery and its accessories,

All Products Pricelist from Various Shops:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.