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.

The programming language in this project is C/C++. If you want to use Python to program this project, here is the tutorial link:

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

Step 1) If you have installed PCA9685 C library in Lesson 1, you can skip this step. Otherwise you need install the library by typing following command:

wget http://osoyoo.com/driver/p3-car/pca9685.tar.gz
tar -zxvf pca9685.tar.gz

Step 2) Download line tracking C code by typing following command:
wget http://osoyoo.com/driver/p3-car/lesson3.c

Step 3)Compile Lesson 3 code by typing following command:

gcc -o lesson3 lesson3.c pca9685/pca9685.c -lwiringPi

Step 4) Type following command to run the program:


Your servo will rotate the ultrasonic sensor to front position for 1 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  ./lesson3  and run the program again.

Now the car will automatically make obstacle avoidance auto driving.