An ultrasonic sensor is a device that uses sound waves to measure the distance to an object. It works by sending out a sound wave at a high frequency (typically 40 kHz) and then listening for the echo of the wave when it bounces off of the object. The time it takes for the sound wave to travel to the object and back is used to calculate the distance.
Working of Ultrasonic Sensor
An ultrasonic sensor has four pins: VCC, GND, Trig, and Echo. The VCC pin connects to the power supply, the GND pin connects to ground, the Trig pin sends out the sound wave, and the Echo pin receives the echo.
To measure the distance to an object, the Arduino sends a high-frequency pulse to the Trig pin. This pulse causes the ultrasonic sensor to emit a sound wave. The sound wave travels through the air until it hits an object, where it is reflected back to the sensor. The Echo pin then receives the echo and measures the time it took for the sound wave to travel to the object and back.
The distance to the object can then be calculated using the following formula:
Use code with caution. Learn morecontent_copy
The speed of sound in air is approximately 340 meters per second.
Applications of ultrasonic sensors
Ultrasonic sensors have a wide variety of applications, including:
- Object detection
- Distance measurement
- Level sensing
- Robotics
- Security
- Automotive
- Medical
- Industrial
How to use an ultrasonic sensor
To use an ultrasonic sensor, you will need to connect it to an Arduino board and write some code. The following code will read the distance from the ultrasonic sensor and print it to the Serial Monitor:

Code
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
long duration;
int distance;
// Send a trigger pulse to the ultrasonic sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the echo pulse
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.println(distance);
delay(500);
}
Use code with caution. Learn morecontent_copy
This code will first set the Trig and Echo pins as outputs and inputs, respectively. It will then initialize the Serial Monitor at 9600 baud rate.
In the loop, the code will send a trigger pulse to the Trig pin, wait for 10 microseconds, and then send a low signal to the Trig pin. It will then measure the echo pulse from the Echo pin. The duration of the echo pulse is used to calculate the distance to the object.
The distance is then printed to the Serial Monitor. The code will then wait for 500 milliseconds before repeating the process.
Interfacing Arduino with ultrasonic sensor
To interface an ultrasonic sensor with an Arduino board, you will need to connect the four pins of the ultrasonic sensor to the corresponding pins on the Arduino board. The following table shows the connections:
Ultrasonic sensor pin | Arduino pin |
---|---|
VCC | 5V |
GND | GND |
Trig | 13 |
Echo | 12 |
Once the connections are made, you can upload the code to the Arduino board and start using the ultrasonic sensor.