Welcome to 9G Servo PTZ repository
For Arduino
The purpose of the experiment
In this course we mainly study the use of Ultrasonic sensor.
Introduction of Dual axis XY rocker module:
The ultrasonic module is a sensor that uses ultrasonic characteristics to detect the distance. It has two ultrasonic probes for transmitting and receiving ultrasonic waves. The range of measurement is 3-450 cm.
3-1 Ultrasonic emission and reception schematic
(1) You need to input a high level signal of at least 10us to the Trig pin to trigger the ranging function of the ultrasonic module.

3-2 Ultrasonic module sends trigger signal
(2) After the ranging function is triggered, the module will automatically send out 8 ultrasonic pulses with 40 kHz and automatically detect whether there is a signal return. This step is done internally by the module.
![]()
(3) When the module detects an echo signal, the ECHO pin will output a high level. The high level duration is the time from when the ultrasonic wave is sent to when it returns. You can calculate the distance by using the time function to calculate the high level duration. Formula: Distance = High level duration * Speed of sound(340M/S)/2.
The actual object is shown below.
List of components required for the experiment
Arduino UNO board *1
USB cable *1
Ultrasonic sensor *1
Dupont line *1 bunch
Actual object connection diagram:
We need to connect the circuit as shown in the figure below.
Experimental code analysis:
int Echo = A5;
int Trig =A4;
int Distance = 0;
void setup()
{
Serial.begin(9600); //Initialize the serial ports
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
}
void Distance_test()
{
digitalWrite(Trig, LOW); //Give the trigger pin 2μs low level
delayMicroseconds(2);
digitalWrite(Trig, HIGH); //Give the trigger pin 10μs high level,at least 10μs
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH); //(unit:us)
Fdistance= Fdistance/58;
// X second=( 2 * Y meter)/344 ==》X second = 0.0058 * Y meter ==》cm = us / 58
Distance = Fdistance;
}
void loop()
{
Distance_test();
if((2
Experimental steps
1. We need to open the program for this experiment: code_Ultrasonic_sensor.ino, click “√”under the menu bar,compile the program, and wait for the words of Done compiling in the lower left corner, as shown in the following figure.
2. In the menu bar of Arduino IDE,you need to select the 【Tools】---【Port】--- select the port that the serial number displayed by the device manager just now.for example:COM6,as shown in the following figure.
3. After the selection is completed, you need to click “→”under the menu bar,and upload the program to the Arduino UNO board, when appears to Done uploading on the lower left corner , that means that the program has been successfully uploaded to the Arduino UNO board, as shown in the following figure.
4. After the program upload is completed. You need to open the serial monitor of the Arduino IDE and you can see the current distance measured by the ultrasonic sensor. If the measured distance is larger than the range of the sensor, it will output “!!! Out of range”. As shown in the following figure.

