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 ここでご購入を!

In this lesson, our Raspberry Pi robot car will use Ultrasonic sensor to detect obstacles and make automatic driving and avoid collision.
The sample codes in this tutorial are written in Python.

No. Picture Device Qty. Accessories Link
1 Ultrasonic Sensor 1 M1.5*8 Screw x 4
M1.5 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.5*8 Screw x 4
M1.5 nut x 4
M2*4 Self Tapping Screw x 1
Click here to buy
4 20Pin jumper wire female to female 20 cm some
5 Philips screwdriver 1 Click here to buy

Step 1: You must complete Lesson 1 basic framework. If you have completed hardware installation of lesson2, please keep it.

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.5*8 screw and M1.5 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 , and connect GND and VCC of ultrasonic module to GND and V+, then connect TRIG and ECHO of ultrasonic module to GPIO20 and GPIO21 as following graph (Remember : DO NOT remove any existing wires installed in Lesson 1) :

Step 1: Make sure you have installed rpi.gpio, adafruit-pca9685 library and enable I2C in lesson 1.

Step 2: Turn on the robot car and download the python code by typing the following command in your Raspberry Pi terminal:

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

Step 3: Put the robot car on the ground and run the program (ATTENTION: please select a correct command according to your Pi board):

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/3A+, type :

python3 picar-obstacle-avoid.py 

Step 4: After running the last command, the servo motor will make the ultrasonic sensor face left, then right, then middle and stay for a few seconds. If the ultrasonic sensor doesn’t face front, please turn off the robot car Immediately. Then remove ultrasonic sensor from servo and re-install it, make sure it faces front and fix it with M2*4 screw.

Step 5: Turn on the car again, type the same command python picar-obstacle-avoid.py and run the program again. Now the car will automatically make obstacle avoidance auto driving.