

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. Send a 10 µs HIGH 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 power supply. |
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; // Trig 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 HIGH pulse to the Trig pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the HIGH 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 measure through transparent materials like glass?
A: No, ultrasonic sensors cannot measure through transparent materials like glass, as ultrasonic waves are reflected by the surface.
Q: What is the maximum distance the sensor can measure?
A: The maximum distance is typically 400 cm, but this may vary slightly depending on the specific sensor model.
Q: Can I use the ultrasonic sensor outdoors?
A: While the sensor can be used outdoors, environmental factors like wind, temperature, and rain may affect its performance. Consider using a weatherproof enclosure for protection.
Q: How do I reduce noise in the sensor readings?
A: Use a capacitor across the power pins, ensure proper grounding, and average multiple readings in your code to reduce noise.
This concludes the documentation for the ultrasonic sensor.