Robotics and Raspberry Pi are a match made in maker heaven. If you’ve ever wanted to build a robot that can think for itself, this project is the perfect place to start. In this guide, we’ll show you how to create a line-following robot using Raspberry Pi. By the end, your bot will be gliding along a track like a pro.


What You’ll Need:

  1. Raspberry Pi: Model 3B or later (Wi-Fi for updates is a bonus).
  2. Chassis Kit: Wheels, motors, and a baseplate.
  3. IR Sensors: At least two.
  4. Motor Driver: L298N or similar.
  5. Battery Pack: At least 7.4V to power everything.
  6. Jumper Wires: Male-to-female and male-to-male.
  7. Black Electrical Tape: To create the track.
  8. MicroSD Card: Preloaded with Raspberry Pi OS.

P.S. You can find all these items here: Shop – Lab404 | Robotics & Electronics Hub


Step 1: Set Up the Raspberry Pi

Before we dive into the robot, let’s get your Raspberry Pi ready.

  1. Flash Raspberry Pi OS: If you haven’t already, use the Raspberry Pi Imager to install Raspberry Pi OS on your MicroSD card.
  2. Enable GPIO Pins: Boot the Pi and enable GPIO in the Raspberry Pi Configuration menu under Preferences.
  3. Install Python Libraries: Open a terminal and run: sudo apt update sudo apt install python3-rpi.gpio This lets your Pi control the GPIO pins to talk to sensors and motors.

Step 2: Assemble the Chassis

Just like with an Arduino robot, start by building the base of your bot.

  1. Attach the motors to the chassis.
  2. Mount the wheels and any optional caster wheel.
  3. Secure the Raspberry Pi to the base with screws or Velcro.

Pro Tip: Leave enough space for the motor driver and battery pack.


Step 3: Wire the Components

Here’s how to connect everything:

Motor Driver:

  • Connect the motor terminals to the L298N motor outputs (OUT1, OUT2, OUT3, OUT4).
  • Attach the L298N input pins (IN1, IN2, IN3, IN4) to GPIO pins on the Raspberry Pi (e.g., GPIO 17, 18, 22, 23).
  • Connect the motor driver’s power input to your battery pack.

IR Sensors:

  • Mount two IR sensors at the front of the chassis.
  • Connect the VCC and GND pins to the Raspberry Pi’s 5V and GND pins.
  • Connect the signal pins to GPIO pins (e.g., GPIO 24 and 25).

Step 4: Write the Code

Open a Python editor on your Raspberry Pi (like Thonny) and load this script:

pythonCopy codeimport RPi.GPIO as GPIO
import time

# GPIO Pin Definitions
LEFT_SENSOR = 24
RIGHT_SENSOR = 25
LEFT_MOTOR_FORWARD = 17
LEFT_MOTOR_BACKWARD = 18
RIGHT_MOTOR_FORWARD = 22
RIGHT_MOTOR_BACKWARD = 23

# Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEFT_SENSOR, GPIO.IN)
GPIO.setup(RIGHT_SENSOR, GPIO.IN)
GPIO.setup(LEFT_MOTOR_FORWARD, GPIO.OUT)
GPIO.setup(LEFT_MOTOR_BACKWARD, GPIO.OUT)
GPIO.setup(RIGHT_MOTOR_FORWARD, GPIO.OUT)
GPIO.setup(RIGHT_MOTOR_BACKWARD, GPIO.OUT)

def forward():
    GPIO.output(LEFT_MOTOR_FORWARD, GPIO.HIGH)
    GPIO.output(LEFT_MOTOR_BACKWARD, GPIO.LOW)
    GPIO.output(RIGHT_MOTOR_FORWARD, GPIO.HIGH)
    GPIO.output(RIGHT_MOTOR_BACKWARD, GPIO.LOW)

def turn_left():
    GPIO.output(LEFT_MOTOR_FORWARD, GPIO.LOW)
    GPIO.output(LEFT_MOTOR_BACKWARD, GPIO.LOW)
    GPIO.output(RIGHT_MOTOR_FORWARD, GPIO.HIGH)
    GPIO.output(RIGHT_MOTOR_BACKWARD, GPIO.LOW)

def turn_right():
    GPIO.output(LEFT_MOTOR_FORWARD, GPIO.HIGH)
    GPIO.output(LEFT_MOTOR_BACKWARD, GPIO.LOW)
    GPIO.output(RIGHT_MOTOR_FORWARD, GPIO.LOW)
    GPIO.output(RIGHT_MOTOR_BACKWARD, GPIO.LOW)

def stop():
    GPIO.output(LEFT_MOTOR_FORWARD, GPIO.LOW)
    GPIO.output(LEFT_MOTOR_BACKWARD, GPIO.LOW)
    GPIO.output(RIGHT_MOTOR_FORWARD, GPIO.LOW)
    GPIO.output(RIGHT_MOTOR_BACKWARD, GPIO.LOW)

try:
    while True:
        left = GPIO.input(LEFT_SENSOR)
        right = GPIO.input(RIGHT_SENSOR)

        if left == 0 and right == 0:  # Both sensors on the line
            forward()
        elif left == 0 and right == 1:  # Left sensor on the line
            turn_left()
        elif left == 1 and right == 0:  # Right sensor on the line
            turn_right()
        else:  # Both sensors off the line
            stop()
        time.sleep(0.1)

except KeyboardInterrupt:
    GPIO.cleanup()

Save it as line_follower.py.


Step 5: Create Your Track

Lay down black electrical tape on a white surface. Start with simple shapes like ovals or zigzags. Test the robot with gradual curves before attempting tighter turns.


Step 6: Run Your Robot

  1. Place your robot at the start of the track.
  2. Power up the Raspberry Pi and motor driver.
  3. Run the script:bashCopy codepython3 line_follower.py
  4. Watch as your bot follows the line!

Troubleshooting Tips

  • Robot veers off track? Check sensor alignment or adjust the sensor height.
  • Motors not moving? Revisit your wiring and ensure the GPIO pins match the code.
  • No line detection? Test each IR sensor individually using a simple script.

What’s Next?

Your line-following bot is alive! Now it’s time to level up. Add more sensors for edge detection or implement speed control for smoother turns. In our next guide, we’ll explore How to Automate Your Home Using Raspberry Pi.


Need Parts?

From Raspberry Pis to sensors and motor drivers, we stock everything you need to build and upgrade your robot. Check out our store for quality components at competitive prices.

Shop – Lab404 | Robotics & Electronics Hub


Leave a Reply

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