Interface HC-SR04 Ultrasonic Sensor With ESP32

If you are an electronics engineer or a hobbyist and want to build some ultrasonic measurement devices like different types of robots, measurement scales, radar, and much more, you need to first learn how to interface HC-SR04 Ultrasonic Sensor. We are going to interface an HC-SR04 ultrasonic sensor module with ESP32 and also going to know every detail about this sensor, which is a crucial component of any ultrasonic detection system. The HC-SR04 ultrasonic sensor has a detection range of 13 feet with an angle of 15 degrees, which is great for detecting obstacles. Also, it is a deficient power consumption device which is another key feature for battery-powered applications.

In our previous projects, we have used this HC-SR04 sensor to build many interesting projects, some of which are Ultrasonic Distance Measurement Systems, Ultrasonic Radar, Obstacle Avoiding Robots, Water Level Controllers, etc.

HC-SR04 Ultrasonic Sensor Pinout

HC-SR04 Ultrasonic Sensor has 4 pins which are VCC, Trig, Echo, and GND. The input and output of this sensor are digital so we need to connect it to the digital pin of the microcontroller. The pinout of the HC-SR04 is shown below.

ultrasonic-sensor
Pin NameDescription
VCCThe +5V supply pin of the HC-SR04 sensor.
GNDThis is the ground pin of the sensor.
TriggerBy setting up this pin to HIGH for 10µs, the sensor initiates an ultrasonic burst.
EchoThis pin remains high until the sensor receives the echo back or a timeout occurs.

About HC-SR04 Ultrasonic Sensor

Before we go and understand how the ultrasonic distance sensor works, we will first learn what ultrasound is: ultrasound sound is a high-pitched wave, in which the frequency of the wave exceeds the audible hearing range of humans. The audible range for humans is 20Hz to 20Khz, and the sound emitted from the module is way higher than what human ears can process, so it’s inaudible for humans.

An ultrasonic sensor consists of two ultrasonic transducers. Here one acts as a transmitter (40KHz), and another one acts as a receiver and senses the reflected pulse frequency. The sensor produces an output pulse that is proportional to the distance of an object in front of the sensor. By executing the Analog pulses using a microcontroller, we can determine the distance of the object.

This sensor has an operating voltage of 5 volts and can have a range of detection between 2cm to 400cm. The ultrasonic sensor can be interfaced with any microcontroller. We can find all the interfacing of the ultrasonic sensor in this article.

Working Principle of HC-SR04 Ultrasonic Sensor

Before going to enter the working principle of the distance measurement system, first, we need to set the Trigger pin high for 10us. In that case, the sensor transmits an ultrasonic signal of the eighth pulse at 40KHz. The eighth pulse is designed to distinguish between the receiver and the transmitted pulse from the surroundings. When the eighth pulse transmission process is done, immediately the Echo pin goes high to indicate the returning signal. If the transmitted signal doesn’t return after 38ms, that means there is no object present in front of the sensor.

If those pulses are reflected back to the receiver, the Echo pin goes low as soon as the signal is received, and that generates a pulse on the Echo pin whose width varies from 150us to 25ms depending on the time taken to receive the signal.

Calculation of Distance Using Pulse Width

Come to the calculation part of the this distance measurement system, we can determine the distance from the reflected object by the received pulse width. We can do this with the distance-speed-time equation.

Let’s assume we have an unknown distance X, for that, we have received 700us pulse width on the Echo pin. Now to calculate the distance, we can use this formula to determine.

Also we know the speed of sound is 343.2 m/s. To calculate the distance, we need to convert the speed of sound to CGS scale (cm/us) which will be 0.03432 cm/us. We also know the time value received from the sensor (700us).

Now we know that the object is 12.012 cm away from the sensor.

Interfacing HC-SR04 Ultrasonic Sensor with ESP32

Project

Circuit Diagram

Ultrasonic Distance Sensor Interfacing With ESP32 Circuit Diagram

Components Required

  • ESP32 Development Board
  • HC-SR04 Ultrasonic Sensor
  • 128×64 OLED I2C Display
  • Prototype Board
  • Connection Wire
  • 2x Lithium-Ion Battery Holder
  • Lithium-Ion Battery (x2)

PCB Design

After designing the schematic diagram of the Ultrasonic Distance Sensor circuit, the assembled components and wiring are too clumsy and looked very unprofessional. In fact, the wiring also has a chance of loose connection. To give it a clean and professional look and also to compress the size, I decided to build its PCB prototype using EasyEDA software as it is so simple to use. Now come to the main part, where we need to order our PCB prototype. I always prefer PCBWay for their quality assurance, fastest delivery and also for 24/7 customer support.

You can view the Gerber file here Gerber File Viewer.

PCB View

Ultrasonic Distance Sensor Interfacing With ESP32 2D View
2D View
Ultrasonic Distance Sensor Interfacing With ESP32 3D View
3D View

Why I Choose PCBWay as My First Preference?

I’ve done several runs with PCBWay and am happy with the results, it’s good quality. The etching, solder mask, and hole sizes are all done well and that is what matters to me. It takes a few hours for a design to get approved and then a couple of days to finish and ship. With more than a decade in the field of PCB prototypes and fabrication, PCBWay has proved its assurance from time to time. They always look at the customer’s needs from different regions in terms of quality, on-time delivery, cost-effectiveness and any other demanding requests.

PCBWay Gold Finish
PCBWay Thickness

These PCBs were manufactured by PCBWay and the finish quality really impressive, especially with the gold finish path. If you want to finish your product faster you could also ask PCBWay to make a panelized order where you receive multiple PCBs in a single panel. With this, you will also receive SMT Stencil from PCBWay, saving you time and effort. All the orders are high quality and could select a lot of settings such as thickness, flexible PCB, the colour of the solder masks, the number of layers, material, surface finish and more.

PCBWay different types of solder masks

How to Order PCB from PCBWay?

ESP32 Code

Before we start uploading the code, we need to install the Adafruit_GFX library, Adafruit_SSD1306.h, and NewPing.h library from the library manager of Arduino to process the data from the sensor and display.

Troubleshooting of HC-SR04 Ultrasonic Sensor

  • If you are having trouble interfacing the HC-SR04 with ESP32, first need to check the power connection. The ultrasonic sensor module works with a 5V supply, and if you’re powering it with 3.3V, the module will be not working properly.
  • The module can give you inaccurate information if you are working with an unsupported library for your module, so you need to check the library that you have installed or you need to install the proper library for the module.
  • If the module is still not working, try comparing your module with another ones.

Leave a Comment