Introduction: How to Make a Target Chasing Robot Car Using Raspberry Pi Pico

About: YouTube Content Creator 📽️ Robotics Enthusiast / Maker 🤖 Learn to make robots that are beyond your imagination 🚀

Greetings everyone, and welcome to my Instructables tutorial. Today, I'll guide you through the process of creating an Target Chasing Robot Car using Raspberry Pi Pico.

Project Overview:

This project showcases a target chasing robot car built with a Raspberry Pi Pico and an L298N motor driver, combined with mecanum wheels that allow for agile and multidirectional movement. The design integrates an ultrasonic sensor for precise distance measurement and IR sensors for accurate path detection, making it an ideal DIY robotics project for hobbyists and tech enthusiasts alike. The attached circuit diagram and detailed robot photo offer clear guidance on wiring and assembly, ensuring that even beginners can follow along to build their own autonomous robot vehicle.

Leveraging a robust microcontroller platform and efficient sensor integration, this project not only demonstrates smooth forward motion and responsive turning but also provides a practical application of real-time obstacle detection. The code example included in the project manages motor control and sensor data seamlessly, resulting in a robot that can effectively chase and navigate towards its target. With its blend of modern components and step-by-step instructions, this Raspberry Pi Pico robot car project is a must-try for anyone interested in robotics, automation, and innovative tech builds.

Before beginning a huge shoutout to JLCMC for sponsoring.

Now, let's get started with our project!

Supplies

Electronic Components Required:

  1. Ultrasonic Sensor
  2. IR Sensor
  3. Raspberry Pi Pico
  4. L298N Motor Driver
  5. Gear Motor
  6. Mecanum Wheel
  7. Breadboard
  8. 18650 Battery
  9. 18650 Battery Holder

Additional Tools:

  1. Soldering Iron
  2. Hot Glue
  3. Cutter

Step 1: 3D CAD

I used Tinkercad to plan and design my project. I designed this chassis with three things in mind: easy assembly, 3D printability, and affordability. After finalizing the design, I exported the file in STL format to ensure it was ready for 3D printing. The attached file includes all necessary components and dimensions for accurate printing and straightforward assembly. For even greater affordability, you can also consider CNC cutting the chassis frame from an acrylic sheet.

Car Chassis

Wheel Couple

Motor Clips

Step 2: Assembling the Chassis

Chassis Assembly Steps:

  1. Solder the wires to the gear motor.
  2. Affix the 3D-printed motor mount clips onto the gear motor.
  3. Secure the motor (with the attached mount clips) onto the robot body using hot glue.
  4. Mount the L298N motor driver onto the chassis and connect the motor wires to its terminals.
  5. Install the Mecanum wheel onto the gear motor.

Your chassis assembly is now complete and ready for the next steps.

Step 3: Elevate Your Electronic Projects - JLCMC

JLCMC is your one-stop shop for all electronic manufacturing needs, offering an extensive catalog of nearly 600,000 SKUs that cover hardware, mechanical, electronic, and automation components. Their commitment to guaranteeing genuine products, rapid shipping (with most in-stock items dispatched within 24 hours), and competitive pricing truly sets them apart. In addition, their exceptional customer service ensures you always get exactly what you need to bring your projects to life.

To show their support for our community, JLCMC is offering an exclusive $19 discount coupon. This is the perfect opportunity to save on high-quality components for your next project. Don’t miss out—visit https://jlcmc.com/?from=RBL to explore their amazing range of products and grab your discount coupon today!

Step 4: Circuit Between Raspberry Pi Pico & L298N Motor Driver

The L298N has six key pins that you’ll connect to the Raspberry Pi Pico: ENA, IN1, IN2, IN3, IN4, and ENB. These pins let your Pico tell the L298N what to do. Here’s how to wire them up:

  1. ENA (Enable Motor A)
  2. Connect ENA to GP7 on the Raspberry Pi Pico.
  3. This pin controls the speed of Motor A (the left-side motors) using PWM. Think of it as the gas pedal for the left side of your car.
  4. IN1 and IN2 (Motor A Direction Control)
  5. Connect IN1 to GP6 and IN2 to GP5 on the Pico.
  6. These pins control the direction of Motor A. By setting one HIGH and the other LOW, you can make the motors spin forward or backward. For example:
  7. IN1 = LOW, IN2 = HIGH: Motor A spins forward.
  8. IN1 = HIGH, IN2 = LOW: Motor A spins backward.
  9. IN3 and IN4 (Motor B Direction Control)
  10. Connect IN3 to GP4 and IN4 to GP3 on the Pico.
  11. These pins control the direction of Motor B (the right-side motors). They work the same way as IN1 and IN2:
  12. IN3 = HIGH, IN4 = LOW: Motor B spins forward.
  13. IN3 = LOW, IN4 = HIGH: Motor B spins backward.
  14. ENB (Enable Motor B)
  15. Connect ENB to GP2 on the Pico.
  16. This pin controls the speed of Motor B using PWM, just like ENA does for Motor A. It’s the gas pedal for the right side of your car.

Step 5: Circuit Between Raspberry Pi Pico, Ultrasonic Sensor & IR Sensor

With the motor driver connected, it’s time to wire up the sensors that will give your robot car its "senses." In this step, we’ll connect the Ultrasonic Sensor and the IR Sensors. These sensors will help your robot car navigate its environment and make smart decisions. Let’s dive in!

Connecting the Ultrasonic Sensor

The ultrasonic sensor has four pins: VCC, GND, Trigger, and Echo. Here’s how to connect them to the Raspberry Pi Pico:

  1. VCC: Connect to the 3.3V pin on the Pico. This provides power to the sensor.
  2. GND: Connect to the GND pin on the Pico. This completes the circuit.
  3. Trigger: Connect to GP0 on the Pico. This pin sends the ultrasonic pulse.
  4. Echo: Connect to GP1 on the Pico. This pin receives the reflected pulse and tells the Pico how far away an object is.

How the Ultrasonic Sensor Works

  1. The Pico sends a short pulse to the Trigger pin.
  2. The sensor sends out an ultrasonic wave and listens for the echo.
  3. The Echo pin goes HIGH for a duration proportional to the distance of the object.
  4. The Pico measures this duration and calculates the distance using the formula:
  5. Distance (cm) = (Time × Speed of Sound) / 2

Connecting the IR Sensors

Each IR sensor has three pins: VCC, GND, and OUT. We’ll connect two IR sensors—one for the right side and one for the left side of the robot car.

  1. Right IR Sensor:
  2. VCC: Connect to the 3.3V pin on the Pico.
  3. GND: Connect to the GND pin on the Pico.
  4. OUT: Connect to GP8 on the Pico. This pin sends a HIGH or LOW signal depending on whether the sensor detects an obstacle or line.
  5. Left IR Sensor:
  6. VCC: Connect to the 3.3V pin on the Pico.
  7. GND: Connect to the GND pin on the Pico.
  8. OUT: Connect to GP9 on the Pico. This pin works the same way as the right sensor.

How the IR Sensors Work

  1. When the IR sensor detects an obstacle or a reflective surface (like a white line), the OUT pin goes LOW.
  2. When there’s no obstacle or the surface is non-reflective (like a black line), the OUT pin goes HIGH.
  3. The Pico reads these signals and decides whether to turn left, turn right, or keep moving forward.

Step 6: Power Supply

The L298N motor driver has two power input pins: VCC and GND. Here’s how to connect the battery:

  1. Battery Positive (+): Connect to the VCC pin on the L298N. This provides power to the motors.
  2. Battery Negative (-): Connect to the GND pin on the L298N. This completes the circuit.

Important Note: Make sure the battery voltage matches the rated voltage of your motors. For example, if your motors are rated for 6V, don’t use a 12V battery, as it could damage them.

The L298N has a 5V output pin that can power the Raspberry Pi Pico. Here’s how to connect it:

  1. L298N 5V Pin: Connect to the VBUS pin on the Raspberry Pi Pico. This provides regulated 5V power to the Pico.
  2. L298N GND Pin: Connect to the GND pin on the Raspberry Pi Pico. This ensures a common ground between the L298N and the Pico.

Why This Works: The L298N has an onboard voltage regulator that steps down the battery voltage to 5V, which is safe for the Pico.

Pro Tips for Power Connections

  1. Use a switch between the battery and the L298N to easily turn the robot car on and off.
  2. Always disconnect the battery when making changes to the circuit to avoid accidental short circuits.

Step 7: Upload the Code Using Thonny IDE

Now that your robot car is fully wired up, it’s time to bring it to life with code! In this step, we’ll use Thonny IDE, a user-friendly Python editor, to upload the code to your Raspberry Pi Pico. This code will control the motors, read sensor data, and make your robot car chase its target. Let’s get started!

1) Install Thonny IDE

If you haven’t already installed Thonny IDE, follow these steps:

  1. Go to thonny.org.
  2. Download the version of Thonny that matches your operating system (Windows, macOS, or Linux).
  3. Install Thonny by following the on-screen instructions.

2) Connect the Raspberry Pi Pico to Your Computer

  1. Plug the Micro-USB cable into your Raspberry Pi Pico.
  2. Connect the other end of the cable to your computer.
  3. Hold down the BOOTSEL button on the Pico while plugging it in. This puts the Pico into bootloader mode, allowing it to be recognized by your computer.

3) Set Up Thonny IDE for Raspberry Pi Pico

  1. Open Thonny IDE on your computer.
  2. Go to Tools > Options.
  3. In the Interpreter tab, select MicroPython (Raspberry Pi Pico) as the interpreter.
  4. Select the correct port (usually something like /dev/ttyACM0 on Linux or COMX on Windows).
  5. Click OK to save the settings.

4) Upload the Code to the Raspberry Pi Pico

  1. Copy this code:
# Code written by: Shahbaz Hashmi Ansari
# How to Make a Target Chasing Robot Car Using Raspberry Pi Pico
# Include the library files
from machine import Pin, PWM
import time
import utime

trigger = Pin(0, Pin.OUT) # Trig pin
echo = Pin(1, Pin.IN) # Echo pin
right_sensor = Pin(8, Pin.IN) # Right IR Sensor
left_sensor = Pin(9, Pin.IN) # Left IR Sensor

# Motor driver pins
ENA = PWM(Pin(7))
IN1 = Pin(6, Pin.OUT)
IN2 = Pin(5, Pin.OUT)
IN3 = Pin(4, Pin.OUT)
IN4 = Pin(3, Pin.OUT)
ENB = PWM(Pin(2))

ENA.freq(1000)
ENB.freq(1000)

speed = 50000 # Adjusted Speed of the robot

def forward():
ENA.duty_u16(speed)
IN1.low()
IN2.high()
ENB.duty_u16(speed)
IN3.high()
IN4.low()

def turn_left():
ENA.duty_u16(speed)
IN1.high()
IN2.low()
ENB.duty_u16(speed)
IN3.high()
IN4.low()

def turn_right():
ENA.duty_u16(speed)
IN1.low()
IN2.high()
ENB.duty_u16(speed)
IN3.low()
IN4.high()

def stop():
ENA.duty_u16(0)
IN1.low()
IN2.low()
ENB.duty_u16(0)
IN3.low()
IN4.low()

# Get the distance
def get_distance():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()

timeout = utime.ticks_us() + 30000 # Timeout after 30ms to avoid infinite loop
signaloff = 0
signalon = 0

while echo.value() == 0:
signaloff = utime.ticks_us()
if utime.ticks_us() > timeout: # Break loop if sensor doesn't respond
return 100 # Return a large distance if sensor fails
while echo.value() == 1:
signalon = utime.ticks_us()
if utime.ticks_us() > timeout: # Break loop if stuck
return 100

timepassed = signalon - signaloff
dist = (timepassed * 0.0343) / 2
return dist

# Main loop
while True:
dis = get_distance()
right = right_sensor.value()
left = left_sensor.value()
print(f"{right},{left}")

if 1 < dis < 15:
forward()
print(f"Moving Forward \nDistance: {dis:.2f} cm")
elif right == 0 and left == 1:
print("Turning Left")
turn_left()
time.sleep(0.2)
elif right == 1 and left == 0:
print("Turning Right")
turn_right()
time.sleep(0.2)
else:
print("Stopping")
stop()

time.sleep(0.1)
  1. Click the Save button in Thonny IDE.
  2. When prompted, choose Raspberry Pi Pico as the location to save the file.
  3. Save the file as main.py. This ensures the code runs automatically when the Pico is powered on.
  4. Click the Run button (green arrow) to upload and execute the code.


Step 8: Working Video and Tutorial

Congratulations! You’ve successfully built your target-chasing robot car using the Raspberry Pi Pico, L298N motor driver, ultrasonic sensor, and IR sensors. Demonstration video of this project can be viewed here: Watch Now

Thank you for your interest in this project. If you have any questions or suggestions for future projects, please leave a comment and I will do my best to assist you.

For business or promotional inquiries, please contact me via email at Email.

I will continue to update this instructable with new information. Don’t forget to follow me for updates on new projects and subscribe to my YouTube channel (YouTube: roboattic Lab) for more content. Thank you for your support.