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.

Distance = Speed x Time

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).

Distance = (0.03432 cm/us x 700uS) / 2 = 12.012 cm

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)

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.

#include <SPI.h>#include <Wire.h>#include <Adafruit_GFX.h>#include <Adafruit_SSD1306.h>#include "NewPing.h"#define TRIGGER_PIN 14#define ECHO_PIN 27// Maximum distance we want to ping for (in centimeters).#define MAX_DISTANCE 400#define SCREEN_WIDTH 128 // OLED display width, in pixels#define SCREEN_HEIGHT 64 // OLED display height, in pixels// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)#define OLED_RESET     -1 //NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);void setup() {  Serial.begin(115200);  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {    Serial.println(F("SSD1306 allocation failed"));    for (;;); // Don't proceed, loop forever  }}void loop() {  Serial.print("Distance = ");  Serial.print(sonar.ping_cm());  Serial.println(" cm");  delay(300);  update_display();}void update_display(){  display.clearDisplay();  display.setTextSize(2);  delay(1);  display.setTextColor(WHITE);  delay(1);  display.println(F("Distance"));  delay(1);  display.println(sonar.ping_cm());  delay(1);  display.println("cm");  delay(1);  display.display();  delay(500);}

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.

Post a Comment

Previous Post Next Post