

An ultrasonic sensor is a device that uses ultrasonic waves to measure distance or detect objects. It emits a sound wave at a frequency above 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. Connect to 5V DC. |
| 2 | Trig | Trigger pin. Sends a 10 µs pulse to initiate distance measurement. |
| 3 | Echo | Echo pin. Outputs a pulse whose duration corresponds to the measured distance. |
| 4 | GND | Ground pin. Connect to the ground of the circuit. |
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 cm
float distance = (duration * 0.034) / 2;
// 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:
Q: Can the ultrasonic sensor detect transparent objects?
A: Ultrasonic sensors may struggle to detect transparent objects like glass, as they may not reflect ultrasonic waves effectively.
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 readings.
Q: Can I use the sensor with a 3.3V microcontroller?
A: The HC-SR04 is designed for 5V operation. Use a logic level shifter to safely interface it with a 3.3V microcontroller.
By following this documentation, you can effectively integrate and troubleshoot an ultrasonic sensor in your projects.