How does the Ultrasonic Sensor work?

An ultrasonic sensor uses sound waves to detect objects in its surrounding environment. They're used in self-driving cars, manufacturing technology, automated lighting, liquid level monitoring, and more!

First, how are the sound waves being used? Some animals, such as bats and dolphins, also use sound to sense objects around them, through echolocation. This natural phenomena has inspired humans to create technologies such as sonar on submarines to explore the depths of the ocean.

Sound is what you hear when a medium, such as air, water, glass, vibrates. Sound waves are a result of molecules in this medium vibrating in a particular pattern, similar to how water ripples.

A very popular ultrasonic sensor model used in DIY electronic projects is HC-SR04. The rest of this article will cover the specifics of how this sensor works.

This sensor works best with objects about 1 inch to 6.5 feet away. The four pins on the sensor functions as follows:

  •  the VCC pin takes 5V to power the ultrasonic sensor

  •  the Trig, or Trigger, pin triggers the sound wave to be sent

  •  the Echo receives the echoing sound wave that bounces off an object

  •  The Ground pin grounds the current that’s powering this sensor

Let’s look at what’s happening between the ultrasonic sensor and an object from an overhead view.

The sensor’s transmitter, when triggered, sends a sound wave to the object. Then the sensor’s receiver receives the echoing sound wave that bounces off the object. This information is turned into an electrical signal that can be used by a computing device to calculate the distance between the sensor and the object.

TRIG and ECHO pins

Inside the ultrasonic sensor is a piezoelectric crystal. These crystals produce a voltage when physically stressed, and the opposite is true as well: a voltage applied to the crystal will cause the crystal to vibrate. The crystal causes the air around it to vibrate and produces sound waves as a result.

With the ultrasonic sensor, the voltage is applied through the Trig pin. The voltage causes the piezoelectric crystal inside the sensor to vibrate, producing a sound wave. When the reflected sound wave comes back to the sensor, The echo pin produces a voltage. Using a computing device, you can determine the distance of that object based on the time elapsed between the Trig and Echo. 

Below is a visual diagram of this process in sequential order:

Connecting the ultrasonic sensor with the Raspberry Pi

You can use the data from an ultrasonic sensor to build interesting automation projects. This will require it to be hooked up to a computing device, such as a Raspberry Pi. So how would the Raspberry Pi and ultrasonic sensor work together?

The ultrasonic sensor sends out sound waves and receives an echo. It then sends this data to the Raspberry Pi. The Raspberry Pi uses the data sent by the sensor and calculates the distance between the sensor and the object, which can then be used as a variable in your code!


This article is a short excerpt from How To Make A Robot, a beginner course in Python and electronics. This ultrasonic sensor model (HC-SR04) comes with the course and is used to allow the robot you build to move around autonomously. If you're interested learning hands-on how this sensor and other electronic components work together, check out the course!

Leave a comment

Please note, comments must be approved before they are published