Raspberry Pi Line Following Robot with Maker Line

Let's try to build line following robot using Raspberry Pi.

Introduction

Maker Line sensor is designed for line following robot application. It works at 3.3V and 5V make it suitable to be used with Arduino boards, Raspberry Pi, ESP32 and micro:bit. This tutorial is using Raspberry Pi board and python code. Before we start, I recommend you to understand the basic of line following robot. You can refer to this tutorial:

Video

This video is about Raspberry Pi line following robot with Maker Line sensor.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is the python code for Raspberry Pi line following robot with Maker Line. You may need to adjust the speed (try and error) based on your hardware.

from gpiozero import Button, Buzzer, LineSensor
from time import sleep
import pigpio
from os import system
pi = pigpio.pi()
sw1 = Button(21)
sw2 = Button(16)
sw3 = Button(20)
buzzer = Buzzer(26)
sensorD1 = LineSensor(27)
sensorD2 = LineSensor(22)
sensorD3 = LineSensor(25)
sensorD4 = LineSensor(12)
sensorD5 = LineSensor(13)
M1A = 18
M1B = 17
M2A = 3
M2B = 2
pi.set_mode(M1A, pigpio.OUTPUT)
pi.set_mode(M1B, pigpio.OUTPUT)
pi.set_mode(M2A, pigpio.OUTPUT)
pi.set_mode(M2B, pigpio.OUTPUT)
def readMakerLine():
global sensorReading
sensorString = ""
sensorReading = 1*(sensorD5.value) + \
2*(sensorD4.value) + \
4*(sensorD3.value) + \
8*(sensorD2.value) + \
16*(sensorD1.value)
if sensorReading < 16:
sensorString = "0"
if sensorReading < 8:
sensorString = sensorString + "0"
if sensorReading < 4:
sensorString = sensorString + "0"
if sensorReading < 2:
sensorString = sensorString + "0"
sensorString = sensorString + "{0:b}".format(sensorReading)
print("Maker Line: " + sensorString)
def robotMove(speedLeft, speedRight):
if speedLeft > 0:
pi.set_PWM_dutycycle(M1A, 0)
pi.set_PWM_dutycycle(M1B, speedLeft)
else:
pi.set_PWM_dutycycle(M1A, abs(speedLeft))
pi.set_PWM_dutycycle(M1B, 0)
if speedRight > 0:
pi.set_PWM_dutycycle(M2A, 0)
pi.set_PWM_dutycycle(M2B, speedRight)
else:
pi.set_PWM_dutycycle(M2A, abs(speedRight))
pi.set_PWM_dutycycle(M2B, 0)
def sw1Pressed():
global robotGo
robotGo = True
buzzer.beep(0.1, 0.1, 1)
def sw2Pressed():
global robotGo
robotGo = False
buzzer.beep(0.1, 0.1, 1)
sw1.when_pressed = sw1Pressed
sw2.when_pressed = sw2Pressed
sensorReading = 0
robotGo = False
buzzer.beep(0.1, 0.1, 2)
try:
while True:
if robotGo == False:
robotMove(0, 0)
else:
readMakerLine()
# You can adjust motor speed based on your hardware
if sensorReading == 0b00100 or sensorReading == 0b01110:
robotMove(40, 40)
elif sensorReading == 0b01100:
robotMove(30, 40)
elif sensorReading == 0b00110:
robotMove(40, 30)
elif sensorReading == 0b11100:
robotMove(20, 40)
elif sensorReading == 0b00111:
robotMove(40, 20)
elif sensorReading == 0b11000:
robotMove(10, 40)
elif sensorReading == 0b00011:
robotMove(40, 10)
elif sensorReading == 0b10000:
robotMove(0, 40)
elif sensorReading == 0b00001:
robotMove(40, 0)
elif sensorReading == 0b11111:
robotMove(30, 30)
if sw3.is_pressed:
robotMove(0, 0)
sleep(1)
buzzer.beep(0.2, 0.2, 3)
sleep(1)
system('sudo shutdown -h now')
except KeyboardInterrupt:
buzzer.off()
robotMove(0, 0)

Thank You

References:

  1. Python GPIO library – pigpio
  2. Library to control GPIO on Raspberry Pi – gpiozero
  3. Maker Line Datasheet

Thanks for reading this tutorial. If you have any technical inquiry, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Read and Log Internal Temperature Data in Raspberry Pi Pico to .txt file.
3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C
Read and Display Environment Sensor Data Using Raspberry Pi Pico and CircuitPython
Control LED Brightness Using PWM On Raspberry Pi Pico
Overclock Raspberry Pi 4 Model B to 2.0GHz for Raspberry Pi OS
Scroll to Top