Project 11: Ultrasonic Sound-following Tank
(1)Description:
In the previous lesson, we learned about the light-following smart car. And in this lesson, we can combine the knowledge to make an ultrasonic sound-following car. In the project, we use ultrasonic sensors to detect the distance between the car and the obstacle in front, and then control the rotation of the two motors based on this data so as to control the movements of the smart car.
The specific logic of the ultrasonic sound- following smart car is shown in the table blow:
Detection |
Setting |
|---|---|
The distance(cm) between the car and the obstacle front |
Set the angle of the servo to 90° |
Condition |
Movement |
distance≥20 and distance≤50 |
Go front |
10<distance<20 |
Stop |
distance≤10 |
go back |
(2)Flow chart:

(3)Connection Diagram:

Note: The wiring of the ultrasonic sensor, the servo and the motor is the same as the previous project experiment. The GND, VCC, SDA, and SCL of the 8x16 LED panel are respectively connected to G (GND), V (VCC), A4, and A5 on the expansion board.
(4)Test Code:
You can also drag blocks to edit your code, as shown below.
(1)
(2)
(3)
(4)
(5)
(6)
(7) 
(8) 
(9) 
(10) 
Complete Test Code
(Note: Do not connect the Bluetooth module before uploading the code, because uploading the code also uses serial communication, and there may be conflicts with the Bluetooth serial communication, which can cause the upload to fail.)

(5)Test Result:
Upload the code, power up, and turn the DIP switch to ON. The servo will rotate 90°, the 8X16 LED panel will show
, and the car will follow the obstacle to move.
