Building Line Following Robot Using Maker Pi Pico, Maker Drive and Maker Line

OVERVIEW

Raspberry Pi Pico is the 1st Microcontroller Development Board from Raspberry Pi Foundation. It is also based on the 1st Microcontroller IC/Silicon – RP2040, designed and produced by Engineers from the Raspberry Pi team too.

Maker Pi Pico is Raspberry Pi Pico’s extension board developed by Cytron Technologies. Maker Pi Pico gives you access to all Raspberry Pi Pico’s pins on two 20 ways pin headers, with clear labels. Each GPIO is coupled with an LED indicator for convenient code testing and troubleshooting. The bottom layer of this board even comes with a comprehensive pinout diagram showing the function of each pin. 

Maker Drive is a easy and beginner-friendly motor driver.  Maker Drive is designed for simplicity and with the beginner in mind.  Maker Drive is designed to be compact, roughly the size of a passport photo. Maker Drive has 4 Test Buttons (2 for each channel), 4 Indicator LEDs (2 for each channel). 

Maker line is a easy and beginner-friendly line sensor. Maker Line also supports dual outputs: 5 x digital outputs for the state of each sensor independently which is similar to conventional IR sensor, but you get the benefit of easy calibration; One analog output where its voltage represents the line position. Analog output also offers higher resolution compared to individual digital outputs. 

HARDWARE SETUP

Parts

Wiring & Assembly

Solder the wires to the motors.

This slideshow requires JavaScript.

Mount motors to robot chassis.

This slideshow requires JavaScript.

Pass the wires through the chasis hole as shown below.

This slideshow requires JavaScript.

Tighten a 5mm PCB Stand on the chasis with the screw to mount Maker Drive.

This slideshow requires JavaScript.

Tighten a 5mm PCB Stand to Maker Drive.

This slideshow requires JavaScript.

Mount Maker Drive on the chasis. Only one PCB Stand is screwed to chasis.

This slideshow requires JavaScript.

Connect the wires to Maker Drive and mount the wheel to the motors. 

This slideshow requires JavaScript.

Tighten a screw on the chasis as shown below to mount Maker Pi Pico later.

This slideshow requires JavaScript.

Stick a double-sided tape on the chasis and place battery holder (with battery) on it.

This slideshow requires JavaScript.

Connect the battery wires to Maker Drive and check the motors are running correctly.

Img 20210310 161128

Turn on the switch on battery holder and  press button M1A & M2A on Maker Drive, the motors should be moving forward. Swap the wires if the motors are moving in wrong directions.

Img 20210310 161128 768x1024

 

Place 15mm and 10mm PCB Stand together (or use 25mm PCB Stand) and tighten it to chasis.

Img 20210310 161659

Screw Maker Pi Pico with the PCB Stand.

Img 20210315 115239

Add PCB Stand to the remaining 3 sides of the Maker Pi Pico.

This slideshow requires JavaScript.

Connect the wires as shown below to power up Maker Pi Pico.

This slideshow requires JavaScript.

Connect wires from Maker Pi Pico to Maker Drive.

This slideshow requires JavaScript.

Tighten two 5mm PCB Stand at the bottom of chasis.

This slideshow requires JavaScript.

Install Maker Line with 10mm PCB Stand.

This slideshow requires JavaScript.

Connect wires from Maker Pi Pico to Maker Line.

This slideshow requires JavaScript.

Install ultrasonic sensor into the ultrasonic bracket.

This slideshow requires JavaScript.

Install ultrasonic sensor with bracket into the chasis.

This slideshow requires JavaScript.

Connect wires from Maker Pi Pico to ultrasonic sensor.

This slideshow requires JavaScript.

SOFTWARE SETUP

Installing MicroPython

Step 1: Hold down the ‘BOOTSEL’ button on the top of your Pico; then, while still holding it down connect a micro USB cable into the micro USB port on your Pico and computer. Count to three, then let go of the ‘BOOTSEL’ button.

Step 2: Pico appear as a removable drive, as though you’d connected a USB flash drive or external hard drive. You’ll see two files on your Pico – INDEX.HTM and INFO_UF2.TXT. Point the mouse cursor at the first file – INDEX.HTM and double-click it to open it in your browser.

1

 

Step 3: When the browser opens, click on the MicroPython tab to load the MicroPython-specific section of the page. Click on the ‘Download UF2 file’ button to download the MicroPython firmware.

2

 

Step 4: Copy the downloaded file to Pico’s removable storage drive.

3

 

Step 5: After a few seconds you’ll see your Pico disappear from File Manager.

Installing Thonny

Step 1: Open your web browser, visit thonny.org, and click the download link at the top of the page to download the Thonny installer.

Step 2: Open the Thonny software, with your Pico connected to your computer, click on the word ‘Python’ followed by a version number at the bottom-right of the Thonny window. In the list that appears, look for MicroPython (Raspberry Pi Pico) and click on it. If you can’t see it in the list, double-check that your Pico is properly plugged in to the micro USB cable and that the micro USB cable is properly plugged in to your computer.

4

 

CODING

Move Around

In this part, we will be learning on how to code the robot to move forward, reverse, turn left and turn right.

Add Library

Step 1: Download the library file (motor_driver.py) from here

Step 2: Launch Thonny application and open the library file

11

 

Step 3: Save the library file into the Raspberry Pi Pico

12

13

 

Step 4: Save the library with the same name you downloaded with (motor_driver.py) Must add .py at the back

14

 

Step 5: Click OK and the library is added to your Pi Pico

 Get the library documentation here

Move Forward

Step 1: Open a new empty file.

Step 2: Copy & paste these code into your Thonny IDE.

from motor_driver import * # import motor_driver library
motor = motor_driver(4,5,2,3) # M1A = 4, M1B = 5, M2A = 2, M2B = 3
# Move Forward
motor.speed(50,50) # move forward at speed 50
view raw Move Forward.py hosted with ❤ by GitHub

Step 3: Save the file in Raspberry Pi Pico.

1 2

Step 3: Save the file as ‘main.py’ and click OK. Must add .py at the back.

3

Turn on the robot by using the switch at battery holder. The robot will move forward.

Move Backward

Follow the steps from “Move Forward” and add the code below.

from motor_driver import * # import motor_driver library
motor = motor_driver(4,5,2,3) # M1A = 4, M1B = 5, M2A = 2, M2B = 3
# Move Backward
motor.speed(50,50) # move backward at speed 50
view raw Move Backward.py hosted with ❤ by GitHub

Turn Left

Follow the steps from “Move Forward” and add the code below.

from motor_driver import * # import motor_driver library
motor = motor_driver(4,5,2,3) # M1A = 4, M1B = 5, M2A = 2, M2B = 3
# Turn Left
motor.speed(0,50) # turn left at speed 50
view raw Turn Left.py hosted with ❤ by GitHub

Turn Right

Follow the steps from “Move Forward” and add the code below.

from motor_driver import * # import motor_driver library
motor = motor_driver(4,5,2,3) # M1A = 4, M1B = 5, M2A = 2, M2B = 3
# Turn Right
motor.speed(50,0) # turn right at speed 50
view raw Turn Right.py hosted with ❤ by GitHub

Aadgif

Brake

Follow the steps from “Move Forward” and add the code below.

from motor_driver import * # import motor_driver library
import utime
motor = motor_driver(4,5,2,3) # M1A = 4, M1B = 5, M2A = 2, M2B = 3
# Move Forward then brake
motor.speed(50,50) # move forward at speed 50
utime.sleep(1) # sleep 1 second
motor.brake() # stop the motor
view raw Brake.py hosted with ❤ by GitHub

Example codes to drive the motors are here

Detect Object

In this part, we will be learning on how to code the robot to read ultrasonic sensor.

Follow the steps from “Move Forward” and add the code below.

import machine
import utime
# read ultrasonic
Trig = machine.Pin(6,machine.Pin.OUT) # Connect Trig pin to pin 6 and set to OUTPUT
Echo = machine.Pin(7,machine.Pin.IN) # Connect Echo pin to pin 7 and set to INPUT
def read_ultrasonic():
Trig.value(0)
utime.sleep_us(2)
Trig.value(1)
utime.sleep_us(10)
Trig.value(0)
while Echo.value() == 0:
pulse_start = utime.ticks_us()
while Echo.value() == 1:
pulse_end = utime.ticks_us()
pulse_duration = pulse_end pulse_start
distance = pulse_duration//58
return distance
while True:
print(read_ultrasonic()) # print ultrasonic distance
utime.sleep(1) # sleep 1 second
view raw read ultrasonic.py hosted with ❤ by GitHub

This slideshow requires JavaScript.

This program will print ultrasonic distance in the unit of cm on the Thonny shell.
Example code to read ultrasonic is here

Line Following

In this part, we will be learning on how to code the robot to read line sensor and follow the line.

Follow the steps from “Move Forward” and add the code below.

import machine
import utime
line = machine.ADC(26) # set pin 26 as analog INPUT
def read_line_sensor: # read line sensor function
return line.read_u16()
while True:
print(read_line_sensor()) # print line sensor value
utime.sleep(1) # sleep 1 second
view raw read line sensor.py hosted with ❤ by GitHub

This program will print line sensor analog value on the Thonny shell.


Follow the steps from “Move Forward” and add the code below.

import machine
import utime
from motor_driver import * # import motor_driver library
import _thread
Trig = machine.Pin(6,machine.Pin.OUT) # Connect Trig pin to pin 6 and set to OUTPUT
Echo = machine.Pin(7,machine.Pin.IN) # Connect Echo pin to pin 7 and set to INPUT
line = machine.ADC(26) # Set pin 26 as analog INPUT
motor = motor_driver(4,5,2,3) # M1A = 4, M1B = 5, M2A = 2, M2B = 3
global us_trig # Declare global variable
us_trig = False # set false as default value
def read_ultrasonic(): # read ultrasonic function
Trig.value(0)
utime.sleep_us(2)
Trig.value(1)
utime.sleep_us(10)
Trig.value(0)
while Echo.value() == 0:
pulse_start = utime.ticks_us()
while Echo.value() == 1:
pulse_end = utime.ticks_us()
pulse_duration = pulse_end pulse_start
distance = pulse_duration//58
return distance
def read_line_sensor(): # read line sensor function
return line.read_u16()
def us_reader_thread(): # read ultrasonic thread
global us_trig
while True:
if read_ultrasonic() < 10: # if ultrasonic distance less than 10
us_trig = True # set global variable to True
_thread.start_new_thread(us_reader_thread, ()) # start read ultrasonic thread
while True:
line_value = read_line_sensor()
if us_trig == True: # if ultrasonic triggered
motor.brake() # brake the motor
utime.sleep_ms(500) # sleep 500ms
global us_trig
us_trig = False # set global variable to False
else:
if line_value > 60000: # All line
motor.speed(60,60)
elif line_value > 40000: # S5,S4+S5
motor.speed(60,10)
elif line_value > 34000: # S4,S3+S4
motor.speed(60,30)
elif line_value > 30000: # S3
motor.speed(60,60)
elif line_value > 22000: # S2,S3,+S2
motor.speed(30,60)
elif line_value > 10000: # S1,S2+S1
motor.speed(10,60)
elif line_value > 200: # No line
motor.speed(30,30)
view raw line follow.py hosted with ❤ by GitHub

 

.

This line follow example is the combination of the three program above .

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

Getting Started with Robotic Operating System (ROS) and JetRacer AI Kit
Barrier Gate with micro:bit and MakeCode
Play MP3 File on Maker Pi Pico Using CircuitPython
Remote Jetson Nano using SSH with Visual Studio Code
3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C
Tutorials of Cytron Technologies Scroll to Top