roboattic Lab
Published © GPL3+

How to Make Target Chasing Robot Car Using Raspberry Pi Pico

Build a target-chasing robot car with Raspberry Pi Pico, L298N motor driver, and sensors! Perfect for DIY robotics fun and learning.

BeginnerFull instructions provided5 hours759
How to Make Target Chasing Robot Car Using Raspberry Pi Pico

Things used in this project

Hardware components

Raspberry Pi Pico
Raspberry Pi Pico
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1

Software apps and online services

Thonny IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Custom parts and enclosures

2D Design for CNC Cutting of Acrylic Sheet

Car Chassis

Sketchfab still processing.

Wheel Couple

Sketchfab still processing.

Motor Clips

Sketchfab still processing.

Schematics

Target Chasing Robot Car Circuit Diagram

Code

Target Chasing Robot Car Code

MicroPython
# 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  # Initialize to prevent NameError
    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)

Credits

roboattic Lab
13 projects • 9 followers
YouTube Content Creator Robotics Enthusiast

Comments