Overview
In this project, we will make a Line Follower/Tracking Robot using Raspberry Pi 4 & Grayscale Sensor. For this, we will use a Robotic Car Kit called Picar-X Robot from Sunfounder.
The PiCar-X is an AI self-driving robot car that uses Raspberry Pi as its control center. The Robotic Kit is designed by SunFounder. The Kit comes with a Robot HAT for Raspberry Pi. The HAT integrates the motor driving, servo driving, and preset ADC, PWM, and digital pins to extend the functionality. The PiCar-X’s has a 2-axis camera module, ultrasonic module, and line tracking modules that can provide the functions of color/face/traffic signs detection, automatic obstacle avoidance, automatic line tracking, etc.
Line Follower Robot or Line Tracking Robot is a simple autonomously guided robot that follows a line drawn on the ground to either detect a dark line on a white surface or a white line on a dark. Today, these robots are used widely for logistics purposes in stores, warehouses, and industries. Even big companies like Amazon use robots for their warehouses, saving costs and creating safer working environments!
Bill of Materials
We need the following components along with the SunFounder Robot Car Kit to make a Line Follower Robot using Raspberry Pi. You can purchase all these components from Amazon links:
| S.N. | Components | Quantity | Purchase Links |
|---|---|---|---|
| 1 | Raspberry Pi Ai Car Kit PiCar-X | 1 | Amazon | SunFounder |
| 2 | Raspberry Pi 4 | 1 | Amazon | SunFounder |
| 3 | Samsung 18650 Battery | 2 | AliExpress |
| 4 | SD Card 16/32 GB | 1 | Amazon | SunFounder |
| 5 | SD Card Adapter | 1 | Amazon | AliExpress |
| 6 | 18650 Battery Charger | 1 | Amazon | AliExpress |
Robot HAT cannot charge the battery, so you need to buy a battery charger at the same time.
Robot Assembly
The Sunfounder Picar-X Robot Kit comes with all the necessary components required for the Robot which include the following components. You need to buy a Raspberry Pi 4 Board and a pair of Samsung 18650 Batteries separately.
The most important part of this Kit is Robot HAT. With the Robot HAT board, the PiCar-X integrates left/right driving motors, servo motors for steering, and the camera’s pan/tilt functions, and pre-sets the Robot HAT’s ADC, PWM, and Digital I2C pins to allow for extensions to the standard functionality of the Raspberry Pi.
Both a speaker and a Bluetooth chip have been engineered into the Robot HAT for remote control of Text-to-Speech, sound effects, or even background music functionality.
To assemble the components together and make a perfect Robotic Car you can follow the Assembly Guide. The PDF Document has all the images and a pictorial view for easy help with assembly.
The Grayscale Module is connected to the following pins of Robot HAT.
| Grayscale Sensor | RPI HAT |
|---|---|
| VCC | 3.3V |
| GND | GND |
| Output1 | A0 |
| Output2 | A1 |
| Output3 | A2 |
The Robotic Kit Part details, Assembly guide, HAT parts & functionality, calibration process, Robot Testing and Setting Up Rasbian OS has been explained in PiCar-X Getting Started guide.
Read the tutorial thoroughly to understand the Hardware & Software Setup Part.
Installing Python Modules & Libraries
The OS installation part and setup part can be followed on Raspberry Pi OS Setup. But for this robotic project part, it is recommended to install Raspberry Pi OS(Legacy).
You can either use the HDMI Screen or enable SSH and connect the Raspberry Pi to VNC Viewer using the local IP Address, username, and password. It is best to connect the Robot Wirelessly via a Remote Desktop as we need a remote control for it.
The Picar-X Robot should be turned ON and Raspberry Pi should be connected to the network. Now let’s install all the Python Modules and libraries.
Let’s first update the system using the following commands.
|
1 2 |
sudo apt update sudo apt upgrade |
Then install the Python3-related packages.
|
1 |
sudo apt install git python3-pip python3-setuptools python3-smbus |
Download & install the libraries for Robot-Hat.
|
1 2 3 4 |
cd /home/pi/ git clone https://github.com/sunfounder/robot-hat.git cd robot-hat sudo python3 setup.py install |
Running ‘setup.py‘ will download some necessary components. Your download may have failed due to network issues. You may need to download it again at this point. See the following interface, type ‘Y‘, and press Enter.
Now, download and install the ;vilib‘ module.
|
1 2 3 4 |
cd /home/pi/ git clone https://github.com/sunfounder/vilib.git cd vilib sudo python3 install.py |
Similarly download and install the ‘picar-x‘ module.
|
1 2 3 4 |
cd /home/pi/ git clone -b v2.0 https://github.com/sunfounder/picar-x.git cd picar-x sudo python3 setup.py install |
This will download a large number of files, therefore it will take time.
Once the download is complete, run the ‘i2samp.sh‘ script to install the components required by the i2s amplifier, otherwise the picar-x will have no sound.
|
1 2 |
cd /home/pi/picar-x sudo bash i2samp.sh |
Type ‘y‘ and press enter to continue running the script.
Type ‘y‘ and press enter to run ‘/dev/zero‘ in the background.
Type y and press enter to restart the Picar-X.
After restarting, the Raspberry Pi will connect to the Network and will play a sound.
If there is no sound after restarting, you may need to run the ‘i2samp.sh‘ script several times.
All the installation and setup part is complete here.
Python Code for Line Follower/Tracking Robot
Here is a complete Python Code for Line Follower/Tracking Robot using Raspberry Pi 4 & Grayscale Sensor.
You can directly run the code from the terminal using the following command.
|
1 2 |
cd /home/pi/picar-x/example sudo python3 minecart_plus.py |
The code is also located in the source code path ‘picar-x/example/minecart_plus.py‘. You can directly open the code from this location. You can Modify/Reset/Copy/Run/Stop the code below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from picarx import Picarx if __name__=='__main__': try: px = Picarx() # px = Picarx(grayscale_pins=['A0', 'A1', 'A2']) px_power = 10 while True: gm_val_list = px.get_grayscale_data() print("gm_val_list:",gm_val_list) gm_status = px.get_line_status(gm_val_list) print("gm_status:",gm_status) if gm_status == 'forward': print(1) px.forward(px_power) elif gm_status == 'left': px.set_dir_servo_angle(12) px.forward(px_power) elif gm_status == 'right': px.set_dir_servo_angle(-12) px.forward(px_power) else: px.set_dir_servo_angle(0) px.stop() finally: px.stop() |
Code Explanation
The grayscale sensor module grayscale_module is also imported in the picarx module, and we can use some of these methods to detect black lines.
The function to detect the black line looks like this:
get_grayscale_data(): This method directly outputs the readings of the three sensors, from right to left. The brighter the area, the larger the value obtained.get_line_status(): get_line_status(): This method will generate an action based on the values detected by the three probes. There are four types of actions: forward, left, right, and stop.
The trigger conditions for these actions are as follows: A value is assigned by default in the module as the threshold for detecting black or white. When the detection values of the three probes are all greater than the threshold, it means that the probes are sensing the color white, and no black line is detected, which makes the get_line_status() to generate a return value of stop.
- If the right (and the first) probe detects a black line,
rightis returned; - If the middle probe detects a black line, return
forward; - If the left probe detects a black line,
leftis returned.
Testing Line Follower/Tracking Robot using Raspberry Pi
This project will use the Grayscale module to make the PiCar-X move forward along a line. Use dark-colored tape to make a line as straight as possible, and not too curved. Some experimenting might be needed if the PiCar-X is derailed.
After running the code, PiCar-X will move forward along a line.
When the detection values of the three probes are all greater than the threshold, it means that the probes are sensing the color white, and no black line is detected. In this case, Robot stops.
This is how you can make an Line Follower Robot using Raspberry Pi 4 & Grayscale Sensor Module.













