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 Lesson1 and Lesson 2

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

待添加安装SG90舵机图片: RPi_Robot_Car/servo_steer_car/lesson4/4-1.png

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

待添加安装SG90舵机图片: RPi_Robot_Car/servo_steer_car/lesson4/4-3.png

Connect ultrasonic sensor holder SG90 servo motor to PWM 14 port of OSOYOO PWM HAT board, and connect GND and VCC of ultrasonic module to GND and 5V of OSOYOO PWM HAT board, 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 ):

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

wget http://osoyoo.com/driver/servo-steer-car/pi-obstacle.py 

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

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

python pi-obstacle.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 pi-obstacle.py  and run the program again.

待添加调整舵机方向图片

Now the car will automatically make obstacle avoidance auto driving.