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

Hardware Installation:

You must complete Lesson 1 basic frame work installation as per following link:

OSOYOO Raspberry Pi V2.0 car lesson 1: Basic framework installation and L298N board

You also need connect   Ultrasonic sensor  to following GPIO port:

Ultrasonic Sensor location GPIO# Physical Pin#
VCC 5V in PCA9685
TRIG 20 38
ECHO 21 40
GND GND in PCA9685

SERVO  黄/红/咖啡线 接PCA9685 PWM 15 插口

you can check the GPIO pin location in following graph:

电机ENA ,ENB,IN1,IN2,IN3,IN4,PCA9685连线同第一课 https://osoyoo.com/2020/03/19/how-to-use-osoyoo-model-pi-l298n-motor-driver-board-in-raspberry-pi-robot-car/

Software Installation:
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

python 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.