I.Objective
II.Parts and Devices
III.Hardware Installation
IV.Circuit Connection
V.Software Installation

Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP



In this lesson, our Raspberry Pi robot car will use Ultrasonic sensor to detect obstacles and make automatic driving and avoid collision.



No. Picture Device Qty. Accessories Link
1 Ultrosonic Sensor 1 M1.4*8 Screw x 4
M1.4 Nut x 4
Click here to buy
2 Servo Motor 1 M2.2*8 Self Tapping Screw x 2
M2*4 Self Tapping Screw x 1
Click here to buy
3 Mount Holder for Ultrasonic Sensor 1 M1.4*8 Screw x 4
M1.4 Nut x 4
M2*4 Self Tapping Screw x 1
Click here to buy
4 20Pin jumper wire female to female 20cm some
5 Philips screwdriver 1 Click here to buy


Step 1: You must complete Lesson 1 basic frame work

Step 2: Install servo motor at the front of upper car chassis with 2pcs M2.2*8 Self Tapping Screws

Step 3: Install Ultrasonic Module to mount holder with 4pcs M1.4*8 screw and M1.4 nuts

Step 4: Install mount holder for Ultrasonic Module on servo motor with M2*4 Self Tapping screw



Connect SG90 servo motor to PWM 15 of PCA9685 compatible module, and connect GND and VCC of ultrasonic module to GND and V+ of PCA9685 compatible module, then connect TRIG and ECHO of ultrasonic module to GPIO20 and GPIO21 of raspberry pi as following graph (Remember : DO NOT remove any existing wires installed in Lesson 1 ):



Make sure you have installed rpi.gpio and adafruit-pca9685 library in lesson 1 .

Download the python code by typing following command in your Raspberry Pi terminal:

wget http://osoyoo.com/driver/picar/picar-obstacle-avoid.py

Turn on the battery of your car and  your servo and run the program,

If you are using Python 2 in Raspberry Pi 3, type:

python picar-obstacle-avoid.py

or if you are using Python 3 in Pi 4, type :

python3 picar-obstacle-avoid.py 

Your servo will rotate the ultrasonic sensor to front position for 3 seconds. If your sensor is not facing front direction, please turn off the battery or press Ctrl-C key to stop the program. Then remove the sensor from servo and re-install it, make sure it faces front and fix the position with screw, now your can type same command  python picar-obstacle-avoid.py  and run the program again.

Now the car will automatically make obstacle avoidance auto driving.