

The ultrasonic sensor is a versatile electronic component that uses high-frequency sound waves to measure distances or detect objects. It operates by emitting ultrasonic pulses and calculating the time it takes for the echo to return after bouncing off an object. This time-of-flight measurement allows for precise distance calculations, making the ultrasonic sensor a popular choice in a wide range of applications.








Below are the key technical details of a typical ultrasonic sensor (e.g., 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 | Pulse width 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: Outputs a pulse proportional to distance |
| GND | 4 | Ground pin |
Connect the Pins:
Trigger the Sensor:
Measure the Echo:
Calculate the Distance:
Below is an example of how to use the ultrasonic sensor with an Arduino UNO:
// Define the 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);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
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:
Echo Pin Always LOW:
Q: Can the ultrasonic sensor detect transparent objects?
A: No, the sensor may struggle to detect transparent or soft objects as they do not reflect ultrasonic waves effectively.
Q: Can I use the sensor with a 3.3V microcontroller?
A: Yes, but you may need a logic level shifter for the Echo pin to avoid damaging the microcontroller.
Q: What is the maximum angle of detection?
A: The sensor typically has a detection angle of about 15 degrees.
Q: Can the sensor work outdoors?
A: While it can work outdoors, environmental factors like wind, temperature, and rain may affect its performance.