Arduino : Reviewing the HC-SR04 ultrasonic sensor
- Pawit Kochakarn
- Mar 1, 2017
- 3 min read

In this chapter of the Arduino series, I will be deviating away from PID for a moment and focusing on reviews for a project that I am building right now. The main project is the create a binaural hearing device for blind navigation using two HC-SR04 ultrasonic sensors as range finders in order to create a spatial audio feel for the user so that they can judge distances from obstacles and orientation in the environment. You will have to wait another time for a whole blog post about this project but for now, I will be reviewing the basic HC-SR04 ultrasonic sensor that I have used and its basic functionalities.
An ultrasonic sensor triggers out ultrasound waves and receives back an echo signal when the waves reflect back from an object. You can visualise how it works with a basic diagram below:

We can see that pulse width modulation is used to measure the time between triggering the ultrasonic wave and receiving back the echo. This sensor can be integrated into a basic Arduino board (as I did with my project) and comes with 4 pins : GND (ground ), Echo, Trigger and Vcc (Power). The Echo and Trigger pins have to be connected to any of the Digital PWM pins on your Arduino board.
Here are some specifications below which can be found on the sensor's official data sheet.
Specifications
Power Supply :+5V DC
Quiescent Current : <2mA
Working Current: 15mA
Effectual Angle: <15°
Ranging Distance : 2cm – 400 cm/1" - 13ft
Resolution : 0.3 cm
Measuring Angle: 30 degree
Trigger Input Pulse width: 10uS
Dimension: 45mm x 20mm x 15mm
As we can see, what I found out quickly from experimenting and playing around with this sensor was that, it had a really hard time picking up echo signals from object when placed at an angle greater than around 15-20°. This maybe due to the fact that the trigger and echo parts of the sensor are kept separate and thus is only good at detecting objects directly in front. Luckily, I do have a solution for this problem. If you turn the sensor so that it is oriented vertically, larger angles are easier to detect. Furthermore, due to its function for DIY projects ( there are obviously better but way more expensive sensors out there, I will mention a few in a bit ...) its maximum distance is only 4 metres, which is not suitable if you want to be measuring large scale distances.
However, due to its simple to program functionalities (which I will be telling you later) and relatively small size, I feel like it is a good starting point for people wanting to play around with ultrasonic sensors. Please note that these sensors are pretty bad at measuring objects with smaller surface areas than around 0.5 metre squared.
Below is simple circuit diagram on how to hook your HC-SR04 up to an Arduino microcontroller.

And below is a simple code on how to use the sensor as a distance range finder in order to display distances on your serial monitor.
The maths behind converting distance in pulse width into centimetres is demonstrated in the code.
Cost
These ultrasonic sensors can be bought on eBay for as low at $1 and so this can be very economical for all you electronic hobbyists out there !!!
Comments