The HC-SR04 is a proximity sensor that’s very popular amongst makers. Here, we explain how the module works, what you need to know to use one in your projects safely, and discuss the operating basics. Links to the full datasheets are available at the end of the article.
About the HC-SR04
The HC-SR04 is an ultrasonic proximity sensor that tells you whether an object is in front of it, and also provides the distance between the sensor and the object. These sensing abilities make it particularly useful for robots that need to know how far they are away from an object or obstacle, for example, a wall or a piece of furniture that they shouldn’t hit.
The module is readily available as a finished breakout board that connects to your existing projects. You can get it from many different manufacturers and suppliers, like Adafruit and Geekreit, and they are usually fully compatible. The only difference is the price and shipping time.
- Length: 4.5 cm (1 ¾ in)
- Width: 2.0 cm (¾ in)
- Height: 1.4 cm (½ in)
- Typical price: Around $4
- Supply voltage: 5V
- Operating voltages: 3V or 5V (trigger), 5V all other I/O ports
- Working current: 15mA
- Operating range: 2 cm to 400 cm (1 in - 13 ft)
- Claimed precision: 0.3cm, more realistically: 1cm
- Measuring angle: 15 degrees
Principle of Operation
As described above, the main job of the HC-SR04 is adding ultrasonic distance measuring capabilities to projects. But how does that work? The principle, as well as the usage of the module, are both surprisingly simple.
The HC-SR04 gets triggered by a ten microsecond high signal on the trigger pin. Once it gets pulled low again, the module sends out eight 40kHz sound pulses. If an object is present in the detection range, the sound pulses get reflected by that object, and the module receives the echo. The time between sending the eight pulses and receiving the echo can be used to calculate the distance to the object that reflected the sound.
Checking how long the output pin of the module (usually labeled echo) is high calculates the measured distance. If it is high for longer than 30mS, no object was detected. Please refer to the datasheet of your module for the exact formulas. The recommended time between trigger pulses is 60mS.
There are many HC-SR04 datasheets available on the internet. However, I found these to be the most helpful and the ones that contained the most information:
- HC-SR04 Datasheet Osepp
- Elecfreaks HC - SR04 (PDF)
- Dual ultrasonic module - with code examples and wiring diagram
Projects That Use the HC-SR04
- How to Connect an Ultrasonic Sensor to an Arduino
- How to Make an Obstacle-Avoiding Robot With Raspberry Pi
- How To Build an Object Detector With Arduino Using an HC-SR04 and an SG90 Servo
- How to Make an Arduino Door Alarm Using an Ultrasonic Sensor
- Build a Single Cubic Meter with Arduino
The HC-SR04 is a readily available, cheap, and easy to use ultrasonic proximity sensor module that can be used for a wide variety of projects. Due to its small physical size, it’s also useful for mobile applications, like robots.
Cover image courtesy of Banggood.com