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. If you want to use C/C++ to do this project, here is the C programming link: https://osoyoo.com/2021/05/14/osoyoo-raspberry-pi-v2-0-car-lesson-3-obstacle-avoidance-c-language/

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 ):

1)Make sure you have installed rpi.gpio, pca9685 and servokit  library in lesson 1 . If not, please run following two commands:

sudo pip3 install adafruit-circuitpython-pca9685
sudo pip3 install adafruit-circuitpython-servokit

2)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 by typing following command,

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  python3 picar3.py  and run the program again.

Now the car will automatically make obstacle avoidance auto driving.