In the previous article, we built an Obstacle Avoidance Robot using Arduino. We have planned to make the same Obstacle Avoidance Robot using a Raspberry Pi Pico microcontroller. In this project, I mainly used an HC-05 ultrasonic distance sensor to get the distance sensed immediately and drive the motors separately by the L298N motor driver module. Also, I used a robot car chassis for this project. But you can use a DIY chassis according to your budget.
You can learn how to Interface the ultrasonic sensor with the Raspberry Pi Pico board, please visit this link below.
- Raspberry Pi Pico
- L298N Motor Driver
- HC-SR04 Ultrasonic Sensor
- Servo Motor
- DC Gear Motor (x4)
- Wheels (x4)
- Robot Car Chassis
- Connection Wires
- Lithium-Ion Battery (x4)
- 4-Channel Lithium-Ion Battery Holder
About Parts of the Obstacle Avoidance Robot
You have to connect all the required components as per the circuit diagram. The circuit is very simple, you can directly give AC supply to the DC circuit. Before designing the PCB I also checked the Obstacle Avoidance Robot circuit on a breadboard. After testing the circuit, I designed a PCB layout by using EasyEDA software to make the circuit compact and look professional.
Now come to the main part, where we need to order our PCB prototype. I always prefer PCBWay for its quality assurance, fastest delivery, and 24/7 customer support.
PCBWay is a Chinese-based PCB manufacturing company which is providing prototypes, PCB assembly, SMT Stencil and Flexible PCBs. I am in collaboration with them for about recent 1.5 years, and I feel like PCBWay gives an excellent price and customer service factor in one single serving. What is also spectacular about PCBWay to me, as an electronics hobbyist and customer, is their on-time service. In this review, I will explain my personal opinion regarding the quality and processing aspects of PCBWay.
When I first approached PCBWay, I found their PCB prototyping and customisation options capture huge areas with every aspect of you can customize the PCB as per your wish. You can choose different types of solder masks, silkscreen, surface finish, materials and much more. Below, I have attached some solder mask quality provided by PCBWay.
Addressing their SMT stencil service, it is often ordered with your standard PCB boards if your design includes surface mount components being soldered on. Really it is too professional and accurate also checked by their engineers. It is like a template which makes it easy for your project as surface mount soldering isn’t the easiest skill to grasp. Besides the prototype customization, PCBWay also offers different interchangeable options such as the stencil type, thickness, existing fiducials, whether you like electropolishing or not etc.
Now come to the PCB Assembly service, where components can be assembled by them, and your PCB board will come ready to be used. Also here you could choose the option to have PCBWay supply all the components on your design or have the option for your own where you need to supply all the components. Alternatively, you could choose the combo option where PCBWay supplies some parts, along with you will have to supply the rest of the components. I strongly recommend going for the combo option if you have some of the parts you need for that project.
The best part of PCBWay in my opinion, their engineers check your Gerber file and if there is a correction needed, they might contact you or correct it with their PCB expertise. Pointing to that, I think this is the most valuable service that they are providing you than other companies.
Now, once you have done all your customisations and made your final choices, you can receive a quote along with a rough price estimate directly from their website. The price of your product will be based on the options you choose, with the higher, more expensive options contributing to your price more heavily. Shipping is also included separately, DHL is the expensive method and standard airmail is the cheapest method of shipment. The nice thing about PCBWay’s website is that you can see how the final price is broken down by certain costs. Furthermore, there is a choice for you to select your build time and shipping method so that when you change it, you can instantly see the price difference.
Setup Robot Chassis for Obstacle Avoidance Robot
Let’s create the robot car chassis first. For that, see the below pictures for detail.
After that, install the DC motors as shown in the pictures below. You can arrange them as you like.
Next, attach the wheels with the DC motors. For that, I used robot wheels.
Now, mount the L298N motor driver module on the robot chassis and connect all four DC motors to the motor driver.
Next, place the Raspberry Pi Pico on the breadboard.
After that, attach the servo motor to the front of the chassis using hot glue or a screw. And then, install the ultrasonic sensor on top of the servo motor.
Install a lithium-ion battery on the battery holder and breadboard at the top of the robot car chassis.
Finally, connect the servo motor, ultrasonic sensor, motor driver module, and lithium-ion battery holder to the Raspberry Pi Pico.
Connect the Raspberry Pi Pico to the computer and upload the Python script to the board.
How to Prepare Thonny for Uploading Code to Raspberry Pi Pico Board
First, you have to install Thonny Python IDE on your PC. After installing it, hold and press the “BOOTSEL” button on your Raspberry Pi Pico board and plug it into your PC. Release the button when your computer detects your Raspberry Pi board.
Now you can see an extra storage device appeared in your my computer section. Open it and click the “INDEX” file and you will redirect to the Raspberry Pi website. From there, you have to download the “MicroPython UF2 file“. After that simply drag and drop to that extra storage and return to the Thonny software.
Now you need to click on Tools > Options > Interpreter > select microPython (Raspberry Pi Pico) > select the correct port and hit OK.
Now copy and paste the below code to the Thonny IDE, and hit the green-coloured run button. Then you have to save the code as “main.py” (please save it as it is, otherwise the board will not work after USB is removed from the PC) on the Raspberry Pi. If you select My Computer then it will not work after you remove the board from the PC.
Raspberry Pi Pico Code
#Include the library files
from machine import Pin,PWM
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))
speed = 30000 #Speed of this robot
#Get the distance
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
dis = distance()
leftDis = distance()
rightDis = distance()
if(leftDis > rightDis):
elif(leftDis < rightDis):
leftDis = 0
rightDis = 0