Hello and welcome back! In this project, we’ll learn how to make a Line Tracking Robot using the Raspberry Pi Pico board. For that, I used the 3-way IR infrared line tracking sensor module, but you can use any other IR infrared sensor module as well. The process remains the same. We’ll be programming the robot using the Python language, which is beginner-friendly and easy to learn. It’s a great project for those who are just starting out. If you need help setting up the Raspberry Pi Pico board with Thonney IDE, please use this tutorial. Also, I used the L298N motor driver module for controlling the gear motors. Let’s get started and have some fun building our Line Tracking Robot!
3-way Ir infrared sensor module
The 3-way IR infrared sensor module has three IR transmitter and receiver LEDs. This helps the module detect lines and gives us three digital input values to work with. Also, we can use three jumper wires to get the values to the Raspberry PI Pico board.
- You can buy this sensor using this link – Click on me
- How to make a line-tracking robot with Arduino – Click on me
Ok, let’s do this project step by step. The required components are given below.
- Raspberry Pi Pico board x 1 — Amazon / Our store
- 3-way IR infrared sensor x 1 — Amazon / Our store
- L298N motor driver x 1 — Amazon / Our store
- Gear motor x 2 — Amazon / Our store
- Caster wheel x 1 — Amazon / Our store
- Breadboard x 1 — Amazon / Our store
- Li-ion battery holder x 1 — Amazon / Our store
- Li-ion battery x 2 — Amazon
- Jumper wires — Amazon / Our store
- Foam board / Rigifoam
Firstly, identify these components.
Secondly, cut a piece of foam board in the following sizes. But, you can use Rigifoam or other suitable materials.
Thirdly, cut the car chassis using the following sizes.
And then, connect the robot wheels for the gear motors and glue them on the chassis.
Next drill a hole near the gear motors and put the motor wire through this hole. After that, install the L298N motor driver board on the top of the robot chassis.
Now, connect the gear motors to the motor driver board. And then, attach the caster wheel to the back side of the robot car chassis.
Next, place the Raspberry Pi Pico board on the breadboard and install it on the top of the robot chassis.
And then, connect the motor driver to the Raspberry PI Pico board. For that, use the circuit diagram below.
Now, attach the 3-way IR infrared sensor module to the front of the robot car chassis. And then, connect this module to the Raspberry Pi Pico board. For that, use the circuit diagram above. (I used 10 male-to-female jumper wires. Because the jumper wires are short. )
Next, connect the battery holder wires to the motor driver board. Now, install it on the top of the chassis.
Finally, connect this robot to the computer. And then, copy and paste the following script on the Thonny IDE.
- Script and circuit diagram — Download
from machine import Pin, PWM from time import sleep ENA = PWM(Pin(1)) IN1 = Pin(2,Pin.OUT) IN2 = Pin(3,Pin.OUT) IN3 = Pin(4,Pin.OUT) IN4 = Pin(5,Pin.OUT) ENB = PWM(Pin(6)) right = Pin(7, Pin.IN) middle = Pin(8, Pin.IN) left = Pin(9, Pin.IN) # speed of this car speed = 45000 # 0 - 65025 ENA.duty_u16(speed) ENB.duty_u16(speed) def lineTracking(): sleep(0.05) return[left.value(),middle.value(),right.value()] def robot(): sensorValues = lineTracking() if sensorValues == [1,1,1]: stop() elif sensorValues == [0,0,0]: stop() elif sensorValues == [1,0,1]: forward() elif sensorValues == [0,1,1]: turnLeft() elif sensorValues == [0,0,1]: turnLeft() elif sensorValues == [1,1,0]: turnRight() elif sensorValues == [1,0,0]: turnRight() def forward(): IN1.on() IN2.off() IN3.on() IN4.off() def turnRight(): IN1.on() IN2.off() IN3.off() IN4.on() def turnLeft(): IN1.off() IN2.on() IN3.on() IN4.off() def stop(): IN1.off() IN2.off() IN3.off() IN4.off() if __name__ == '__main__': try: while True: robot() except KeyboardInterrupt: stop()
- Now, save this script on the Raspberry Pi Pico board. You have to save it as “main.py”. And then, click the run button.
Now, remove the USB cable and put the batteries into the battery holder. And then, check the motor directions and change the motor wires if they are not rotating in the correct direction.
OK, now place this robot car on the black color track. You can design it as you like. Please get a width of 1 inch.
OK, enjoy this project. The full video guide is below. We hope to see you in the next project or tutorial.