How to make an obstacle avoidance robot using Raspberry Pi Pico board

How to make an obstacle avoidance robot using Raspberry Pi Pico board

Hello and welcome back. In this project, we will learn how to make an obstacle-avoidance robot using the Raspberry Pi Pico board. Also, I have described many projects like this with the Arduino Platform. You can visit them on our website. In this project, I mainly used an ultrasonic sensor to get the distance and used the L298N motor driver board to move the robot. And I used DIY chassis for this robot. But you can use any robot chassis available in the market.

If you want to learn how to use the ultrasonic sensor with the Raspberry Pi Pico board, please use this link.

Ok, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, let’s create the robot car chassis. For that, I used a piece of rigifoam. But you can use any other material or robot car chassis.

Step 3

Thirdly, install the gear motors as follows. You can arrange them as you like.

Step 4

Next, install the rear wheel as follows. For that, I used a robot caster wheel. And then, connect the wheels for the gear motors.

Step 5

Now, attach the servo motor to the front of this robot. And then, install the ultrasonic sensor on top of the servo motor. Follow the pictures below for that.

Step 6

After, mount the motor driver board on top of the robot chassis and connect the gear motors to it.

Step 7

Next, place the Raspberry Pi Pico board on the breadboard. Then, install the li-ion battery holder and breadboard on the top of the robot car chassis.

Step 8

OK, now connect the servo motor, ultrasonic sensor, motor driver board, and li-ion battery holder to the Raspberry Pi Pico board. Use the circuit diagram below for that.

How to make an obstacle avoidance robot using Raspberry Pi Pico board

Step 9

Next, connect this robot to the computer and upload the python script to the board.

How to make an obstacle avoidance robot using Raspberry Pi Pico board

  • Full details of this project — Download

#Include the library files
from machine import Pin,PWM
import time

servo = PWM(Pin(0))#Include the servo motor pin
servo.freq(50)#Set the frequency

Trig = Pin(2,Pin.OUT)#Include the Trig pin
Echo = Pin(3,Pin.IN)#Include the Echo pin

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

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

speed = 30000 #Speed of this robot

def forward():
    ENA.duty_u16(speed)
    IN1.value(0)
    IN2.value(1)
    ENB.duty_u16(speed)
    IN3.value(1)
    IN4.value(0)
    
def backward():
    ENA.duty_u16(speed)
    IN1.value(1)
    IN2.value(0)
    ENB.duty_u16(speed)
    IN3.value(0)
    IN4.value(1)
    
def left():
    ENA.duty_u16(speed)
    IN1.value(1)
    IN2.value(0)
    ENB.duty_u16(speed)
    IN3.value(1)
    IN4.value(0)
    
def right():
    ENA.duty_u16(speed)
    IN1.value(0)
    IN2.value(1)
    ENB.duty_u16(speed)
    IN3.value(0)
    IN4.value(1)

def stop():
    ENA.duty_u16(0)
    IN1.value(0)
    IN2.value(0)
    ENB.duty_u16(0)
    IN3.value(0)
    IN4.value(0)
    
#Get the distance
def distance():
    Trig.value(0)
    time.sleep_us(4)
    Trig.value(1)
    time.sleep_us(10)
    Trig.value(0)
      
    while Echo.value() == 0:
       low = time.ticks_us()
       
    while Echo.value() == 1:
       high = time.ticks_us()
       
    t = high - low
    cm = t/29/2#Time convert to the cm
#     time.sleep(0.1)
    return cm

def servoLeft():
    servo.duty_u16(7000) #1500-8500
    
def servoRight():
    servo.duty_u16(3000) #1500-8500
    
def servoStart():
    servo.duty_u16(5400) #1500-8500

while True:
    dis = distance()
    if(dis<10):
        stop()
        time.sleep(1)
        servoLeft()
        time.sleep(1)
        leftDis = distance()
        time.sleep(0.5)
        print(leftDis)
        servoStart()
        time.sleep(1)
        servoRight()
        time.sleep(1)
        rightDis = distance()
        time.sleep(0.5)
        print(rightDis)
        servoStart()
        time.sleep(1)
        if(leftDis > rightDis):
            print("Turn Left")
            left()            
            time.sleep(0.5)
            stop()
            time.sleep(1)
        elif(leftDis < rightDis):
            print("Turn Right")
            right()
            time.sleep(0.5)
            stop()
            time.sleep(1)
    else:
        leftDis = 0
        rightDis = 0
        forward()       
       

  • Now, save this script as “main.py” on the Raspberry Pi Pico board. Then, click the Run button.

Step 10

Finally, remove the USB cable and put the batteries to the battery holder.

Now, you can test your obstacle avoidance robot car. If the motors do not rotate in the correct direction, please switch and connect the motor wires. Ok, enjoy this project. The full video guide is below. So, see you in the next project or tutorial.

How to make an obstacle avoidance robot using Raspberry Pi Pico board

How to make an obstacle avoidance robot using Raspberry Pi Pico board

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *