Ultrasound Sensors

rover-robot-on-a-bench-with-a-kid-working-on-the back

Understanding the Ultrasound Sensor: How It Works and Where It's Used

Ultrasound sensors are fascinating devices that use sound waves to measure distance, detect objects, and enable various automated processes. From robotics to parking systems, these sensors have become essential components in modern electronics and engineering.

In this article, we'll explore how ultrasound sensors work, what they're made of, their key applications, and how you can use one in your own electronics projects.

1. What Is an Ultrasound Sensor?

An ultrasound sensor (also called an ultrasonic sensor) is a device that measures the distance to an object by emitting high-frequency sound waves (ultrasound) and detecting the echo that bounces back.

These sound waves are typically above 20 kHz — beyond the range of human hearing — and the time it takes for the echo to return is used to calculate distance.

using-ultrasound-echo-to-measure-distance-diagram

2. How Does It Work?

The basic working principle is called echolocation, similar to how bats and dolphins navigate.

  • The sensor sends a short ultrasonic pulse using a transmitter (usually a piezoelectric element).
  • The pulse travels through the air and reflects back when it hits an object.
  • A receiver detects the echo.
  • The time between sending and receiving is measured.
  • Using the speed of sound in air (~343 m/s), the distance is calculated with this formula:
Distance=Time×Speed of Sound2\text{Distance} = \frac{\text{Time} \times \text{Speed of Sound}}{2}

The division by 2 accounts for the round trip of the sound wave.

how-ultra-sonic-sensor-works-ideal-case

3. Key Components

Most ultrasound sensors come as small modules with two round transducers:

  • Transmitter (TX) – sends out the ultrasonic pulse
  • Receiver (RX) – listens for the reflected signal
  • Control circuitry – handles timing, triggering, and signal processing

Popular modules like the HC-SR04 are widely used in hobbyist and educational projects.

Ultrasonic HC-SR04 sensor

4. Common Applications

Ultrasound sensors are used in a wide range of fields:

  • Distance measurement (robot navigation, obstacle detection)
  • Level sensing (measuring liquid levels in tanks)
  • Parking assistance systems in vehicles
  • Presence detection in automation and smart devices
  • Medical imaging (e.g., prenatal ultrasound, though that uses more advanced transducers)
robot-rover-with-ultrasonic-sensor-and-waves-to-a-wall
Ultrasonic-sensor-based-water-level-measurement-principle
car parking sensor

5. Advantages and Limitations

Advantages:

  • Simple to use
  • Inexpensive and readily available
  • Non-contact measurement
  • Good for indoor use

Limitations:

  • Accuracy depends on temperature and air conditions
  • Limited range (typically 2–4 meters for hobby modules)
  • Can be affected by soft materials that absorb sound

6. How to Use One with Arduino

Here’s a quick example of using the HC-SR04 with Arduino:

Wiring:

  • Vcc → 5V
  • GND → Ground
  • Trig → Arduino pin (e.g., 9)
  • Echo → Arduino pin (e.g., 10)

Sample Code:

1#define TRIG_PIN 9
2#define ECHO_PIN 10
3
4void setup() {
5 Serial.begin(9600);
6 pinMode(TRIG_PIN, OUTPUT);
7 pinMode(ECHO_PIN, INPUT);
8}
9
10void loop() {
11 digitalWrite(TRIG_PIN, LOW);
12 delayMicroseconds(2);
13 digitalWrite(TRIG_PIN, HIGH);
14 delayMicroseconds(10);
15 digitalWrite(TRIG_PIN, LOW);
16
17 long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30 ms timeout
18
19 if (duration == 0) {
20 Serial.println("No object detected (timeout)");
21 } else {
22 float distance = duration * 0.0343 / 2;
23 Serial.print("Distance: ");
24 Serial.print(distance);
25 Serial.println(" cm");
26 }
27
28 delay(500);
29}
30
unltrasonic urduino basic setup

What Does pulseIn() Do?

The line:

1long duration = pulseIn(ECHO_PIN, HIGH);

measures the amount of time (in microseconds) that the Echo pin remains HIGH, which corresponds to the time taken by the ultrasonic pulse to travel to the object and back.

  • pulseIn(pin, HIGH) waits for the pin to go HIGH, then measures how long it stays HIGH.
  • This gives the round-trip time of the ultrasonic wave.
  • The distance is then calculated using distance = duration * 0.0343 / 2, where 0.0343 cm/µs is the speed of sound.

What if pulseIn() Returns 0?

This is an important case to consider. A return value of 0 means that no echo was detected within the specified timeout, not that an object is at zero distance. Even if an object is extremely close (e.g., 1 cm), the duration will still be non-zero (around 58 microseconds). Therefore, if you get a duration of 0, it almost certainly means the signal timed out and no object was detected within the sensor's range.

7. Tips for Better Accuracy

  • Use temperature compensation if precision is important
  • Avoid placing the sensor near soft or irregular surfaces
  • Mount it securely to avoid vibration
  • Apply simple averaging or filtering in software to smooth readings

8. Accuracy and Measurement Limits

While ultrasonic sensors can technically measure very fine time intervals (down to microseconds), practical accuracy is limited by environmental and hardware factors.

The resolution of sensors like the HC-SR04 is around 0.3 millimeters, but in most real-world situations, the practical accuracy is in the range of ±3 mm to ±5 mm.

Very close distances (below 2 centimeters) are often unreliable, and the sensor may not detect an object at all. At the upper end, the maximum range for typical low-cost modules is about 4 meters under ideal conditions. Reflections from soft, angled, or uneven surfaces may reduce accuracy.

In summary, ultrasonic sensors are best suited for general-purpose measurements where millimeter-level precision is not required but centimeter-level accuracy is acceptable.

9. What Happens if the Wall is at an Angle?

Ultrasound sensors depend on specular reflection, meaning sound reflects off surfaces like light reflects from a mirror. If the sensor is facing a flat wall perpendicularly, the ultrasonic pulse bounces straight back and is detected properly.

However, if the wall is at an angle, such as 45 degrees:

  • The pulse bounces away from the receiver, not back to it.
  • As a result, the echo may not return at all — the sensor sees no object and times out.
  • Alternatively, the pulse may reflect off another surface and come back later, giving a false or larger distance.

This issue is due to the narrow cone of detection (typically 15–30 degrees wide). Surfaces outside this cone often fail to return usable echoes.

To address this:

  • Mount sensors facing surfaces as squarely as possible.
  • For angled detection, use multiple sensors or different sensing technologies (like infrared or LIDAR).
  • Avoid smooth, angled surfaces that reflect ultrasonic waves off-path.
how-ultra-sonic-sensor-works-bad-angle

10. Effect of Object Material on Detection

The material that the object is made of has a significant impact on the sensor's ability to detect it. Hard, dense materials such as metal, hard plastic, or polished wood reflect ultrasonic waves very well and are therefore ideal targets. These produce strong and clean echoes that the sensor can easily interpret.

In contrast, soft or porous materials like foam, cotton, and thick fabric tend to absorb rather than reflect sound. This makes them unreliable for ultrasonic detection. A sensor may miss such targets entirely or return weak and fluctuating readings.

Water, if calm and perpendicular to the sensor, can reflect ultrasonic waves effectively and is commonly used in fluid level measurement applications. However, if the water surface is disturbed or covered with foam or bubbles, the reflections can become inconsistent.

Transparent or smooth plastic and glass surfaces may also reflect sound waves, but depending on their angle and surface characteristics, they might deflect the wave away from the receiver.

In summary, reliable detection requires that the object’s surface be relatively hard, flat, and properly oriented. When dealing with soft or irregular materials, consider using alternative sensing methods or enhancing the object’s surface reflectivity with added layers (e.g., taping a hard plate to a soft target).

how-ultra-sonic-sensor-works-scattered-on-round-objects

11. Using Multiple Ultrasonic Sensors

In some applications—like obstacle avoidance in robots or 3D environment mapping—a single ultrasonic sensor isn’t enough. Using multiple sensors can greatly enhance coverage and reliability, but it requires careful timing and positioning to avoid interference.

Why Use Multiple Sensors?

  • Wider coverage: One sensor only covers a narrow cone (~15–30 degrees). Multiple sensors can cover different angles or directions.
  • Redundancy: If one sensor misses a reading due to angle or surface, others may succeed.
  • Triangulation: With known sensor positions, you can estimate object position in 2D or 3D space.

The Challenge: Crosstalk

If two ultrasonic sensors trigger at the same time, their signals can interfere with each other. This is known as crosstalk and leads to false or unstable readings.

How to Avoid Interference

  • Stagger the trigger times: Only activate one sensor at a time, with a delay between them (e.g., 50–100 ms).
  • Use software scheduling: In the loop, trigger sensors in sequence rather than all at once.
  • Physically separate sensors: Place them far enough apart to reduce acoustic interference.

Sample Sequential Triggering Code

1// Assuming two sensors: TRIG1/ECHO1 and TRIG2/ECHO2
2measureSensor(TRIG1, ECHO1);
3delay(100); // Wait to avoid crosstalk
4measureSensor(TRIG2, ECHO2);
5
6void measureSensor(int trigPin, int echoPin) {
7 digitalWrite(trigPin, LOW);
8 delayMicroseconds(2);
9 digitalWrite(trigPin, HIGH);
10 delayMicroseconds(10);
11 digitalWrite(trigPin, LOW);
12
13 long duration = pulseIn(echoPin, HIGH, 30000);
14 if (duration > 0) {
15 float distance = duration * 0.0343 / 2;
16 Serial.print("Distance: ");
17 Serial.print(distance);
18 Serial.println(" cm");
19 } else {
20 Serial.println("No echo detected");
21 }
22}
23
robot-rover-with-multiple-ultrasonic-sensor-and-waves-to-a-wall

Or you can explore other categories

Arduino robot and rover

Electronics

A DIY made aircraft rc model with a transmitter

RC Flight Physics

Children looking into a microscop in turns

Microscopy