

An ultrasonic sensor is a device that uses ultrasonic waves to measure distance or detect objects. It emits a sound wave at a frequency higher than the audible range and measures the time it takes for the echo to return, allowing it to calculate the distance to the object.
Ultrasonic sensors are widely used in various applications, including:








Below are the key technical details for a typical ultrasonic sensor, such as the HC-SR04:
| Parameter | Value |
|---|---|
| Operating Voltage | 5V DC |
| Operating Current | 15 mA |
| Operating Frequency | 40 kHz |
| Measuring Range | 2 cm to 400 cm |
| Accuracy | ±3 mm |
| Trigger Input Signal | 10 µs TTL pulse |
| Echo Output Signal | TTL pulse proportional to distance |
| Dimensions | 45 mm x 20 mm x 15 mm |
The ultrasonic sensor typically has four pins, as described below:
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power supply pin (5V DC) |
| 2 | Trig | Trigger pin: Sends a 10 µs pulse to initiate measurement |
| 3 | Echo | Echo pin: Outputs a pulse width proportional to distance |
| 4 | GND | Ground connection |
Below is an example of how to use the HC-SR04 ultrasonic sensor with an Arduino UNO:
// Define pins for the ultrasonic sensor
const int trigPin = 9; // Trigger pin connected to digital pin 9
const int echoPin = 10; // Echo pin connected to digital pin 10
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Set pin modes
pinMode(trigPin, OUTPUT); // Trig pin as output
pinMode(echoPin, INPUT); // Echo pin as input
}
void loop() {
// Send a 10 µs pulse to the Trig pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse on the Echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
float distance = duration * 0.034 / 2; // Speed of sound is ~0.034 cm/µs
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Wait before the next measurement
delay(500);
}
No Output or Incorrect Readings
Unstable or Fluctuating Measurements
Sensor Not Detecting Objects
Echo Pin Always HIGH or LOW
Q: Can the ultrasonic sensor measure distance through glass?
A: Ultrasonic waves may not pass through glass effectively, as they are often reflected or absorbed. For such applications, consider alternative sensors like infrared.
Q: What is the maximum angle of detection for the sensor?
A: The HC-SR04 has a detection angle of approximately 15 degrees. Ensure objects are within this cone for accurate measurements.
Q: Can I use the sensor with a 3.3V microcontroller?
A: While the sensor operates at 5V, you can use a level shifter to safely interface it with a 3.3V microcontroller.