

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.








Below are the general technical specifications for a commonly used 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 |
| Pin Name | Pin Number | Description |
|---|---|---|
| VCC | 1 | Power supply pin (5V DC) |
| Trig | 2 | Trigger pin: Sends the ultrasonic pulse |
| Echo | 3 | Echo pin: Receives the reflected pulse |
| GND | 4 | Ground pin |
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 the trigPin as an output and echoPin as an input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Send a 10 µs HIGH pulse to the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // Ensure a clean LOW pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Send the 10 µs HIGH pulse
digitalWrite(trigPin, LOW);
// Measure the duration of the HIGH pulse on the echoPin
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 for a short period before the next measurement
delay(500);
}
No Output or Incorrect Readings:
Unstable or Fluctuating Measurements:
Sensor Not Detecting Objects:
Q1: Can the ultrasonic sensor measure through transparent materials like glass?
A1: No, ultrasonic sensors typically cannot measure through transparent materials like glass, as sound waves are either absorbed or refracted.
Q2: What is the maximum angle of detection for the sensor?
A2: The HC-SR04 has a detection angle of approximately 15 degrees. Ensure objects are within this cone for accurate readings.
Q3: Can I use the sensor with a 3.3V microcontroller?
A3: The HC-SR04 is designed for 5V operation. If using a 3.3V microcontroller, a level shifter is recommended for safe operation.
Q4: Why is the distance always showing as zero?
A4: This could happen if the Echo pin is not receiving a reflected signal. Check the alignment and ensure the object is within range.