How Bats Inspired One Of The Most Famous Distance Sensor : Everything about HC-SR04

Vishal -Vishal -
5 min read

If you are into IoT or Embedded Systems, you must have come across the HC-SR04, commonly known as the ultrasonic distance sensor, which is used to measure the distance of objects. This sensor is included in most Arduino kits, and it was also the first sensor I used because it came with my kit. It is widely used in robotics for obstacle detection and navigation, and it also finds applications in security systems to detect movement.

Now the question is, are you aware of how this ultrasonic sensor actually works? Most IoT hobbyists simply connect the Trig and Echo pins to an Arduino, copy-paste the code, and get the distance. But its working principle is fascinating, as it beautifully shows the connection between science and nature. The sensor is a great example of biomimicry. In this blog, we are going to explore this biomimicry, the working of the sensor, its code, and other tiny details.

First, let’s talk about bats. They usually travel in darkness because darkness helps them avoid predators and save energy due to less dehydration. With these benefits of darkness, the obvious con is that navigating/seeing anything in darkness is not easy. That’s where the concept of echolocation comes into the picture. What bats do is emit high-frequency sounds (ultrasound, usually 20 kHz to 200 kHz), which are inaudible to humans. These sound waves hit the object, then bounce back (echo) to the bats. The bats process these echo sounds and get an idea about the position, movement, and size of the object in front of them. This concept of detecting the location of objects using echo sound is called echolocation. Engineers took inspiration from this concept and integrated it into engineering applications like sonar, ultrasound medical imaging, distance sensors, etc.

The working of the HC-SR04 is inspired by echolocation. It also sends high-frequency ultrasonic waves of 40 kHz, and when these sound waves hit an object, they bounce back and are received by the sensor. That’s why we see two eye-like little cylinders on the sensor, they are called Tx and Rx. Tx is the transmitter, which converts electrical signals into sound waves, similar to speaker. Rx is the receiver, which converts the reflected sound waves back into electrical signals, similar to a microphone.

>
// Basic HC-SR04 Ultrasonic Sensor Code with Arduino

#define TRIG_PIN 9 // Trig pin connected to Arduino pin 9
#define ECHO_PIN 10 // Echo pin connected to Arduino pin 10

long duration; // Variable to store echo time
int distance; // Variable to store calculated distance

void setup() {
    pinMode(TRIG_PIN, OUTPUT); // Trig pin is OUTPUT (we send trigger signal)
    pinMode(ECHO_PIN, INPUT); // Echo pin is INPUT (we receive pulse duration)
    Serial.begin(9600); // Start Serial Monitor at 9600 baud
}

void loop() {
    // Step 1: Clear the Trig pin
    digitalWrite(TRIG_PIN, LOW);
    delayMicroseconds(2);

    // Step 2: Send a 10µs HIGH pulse to Trig pin
    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);

    // Step 3: Measure the duration Echo pin stays HIGH
    duration = pulseIn(ECHO_PIN, HIGH);

    // Step 4: Calculate distance
    // Speed of sound = 343 m/s = 0.034 cm/µs
    // Distance = (duration * 0.034) / 2 (divide by 2 for to-and-fro travel)
    distance = duration * 0.034 / 2;

    // Step 5: Print distance on Serial Monitor
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

    delay(500); // Wait half a second before next reading
}

The HC-SR04 has four pins: VCC, GND, Trig, and Echo. VCC and GND are self-explanatory. We usually provide around 5V to VCC. The Trig and Echo pins are connected to the GPIOs of a microcontroller. We configure the Trig pin as an output and the Echo pin as an input. To trigger the sensor, we set the Trig pin HIGH for 10 µs, which makes the Tx send 40 kHz ultrasonic sound waves. These sound waves travel, hit an object, and bounce back to the Rx, which receives them. The main IC on the sensor keeps track of the sending and receiving of the sound wave. Suppose at t = 0 the sound was sent and at t = T it was received back. The main IC holds the Echo pin HIGH for T seconds. That’s why we use pulseIn(echo, HIGH) in Arduino code. This function gives us the exact duration for which the Echo pin stayed HIGH. Since we already know the speed of sound, we can use this time to calculate the distance using the speed–distance formula. We divide this by 2 because to get to-and-fro distance.

Most blogs about the HC-SR04 stop the explanation here, but let’s go deeper. The question is: how are ultrasonic waves generated? There is a crystal oscillator mounted on the sensor that provides a reference clock to the onboard main IC. This clock is usually 8 MHz or 12 MHz. The IC divides this clock down to 40 kHz and uses it to drive the Tx transducer. To do this, the IC toggles the pin at 40 kHz (HIGH–LOW–HIGH–LOW repeatedly), generating an AC square wave. The Tx transducer contains piezoelectric crystals that vibrate mechanically when an AC signal is applied. This phenomenon is known as the reverse piezoelectric effect. These vibrations produce ultrasonic sound waves at 40 kHz, which are inaudible to human ears.

The Rx transducer also has piezoelectric material, but here the direct piezoelectric effect comes into play. When ultrasonic sound waves strike the material, it generates a tiny voltage. This weak signal is then amplified by an op-amp-based IC on the sensor. This IC also acts as a comparator to determine whether a valid echo exists or not.

There is also a driver IC on the module that boosts the signal power so the Tx can “shout” loudly, because ultrasonic waves being high-frequency require strong driving signals to be emitted effectively. In addition to these ICs, the module also has resistors and capacitors to handle filtering, biasing, and stable operation.

That’s all about the HC-SR04 from my side. I really love how beautifully nature inspires technology, and how engineers take such concepts to build fascinating inventions. Hats off to the engineers and manufacturers who designed this sensor and connected all these components into such a simple yet powerful module.

0
Subscribe to my newsletter

Read articles from Vishal - directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vishal -
Vishal -