> [!TLDR] Using the HC-SR04 sensor with ESP32 > This guide explains how to use an HC-SR04 ultrasonic distance sensor with an ESP32 microcontroller. The code sends a trigger pulse to the sensor's TRIG pin and then measures the duration of the echo pulse received on the ECHO pin using the `pulseIn()` function. This duration is then converted to distance in centimeters and inches using the speed of sound, and the results are printed to the serial monitor. # Introduction Using the [[Sensor de Distância Ultrassônico HC-SR04|HC-SR04]] with an [[ESP32]] involves controlling GPIO pins (General Purpose Input/Output). Here's a breakdown of how to do it! ## Requisites - Hardware - [[ESP32]] - [ESP32](app://obsidian.md/ESP32) is a series of low cost and low power consumption microcontrollers. It is also a system-in-one-chip with integrated microcontroller, Wi-Fi and Bluetooth. - [[Sensor de Distância Ultrassônico HC-SR04|HC-SR04]] - Ultrasonic distance sensor. - Software - [[Arduino IDE]] or [[Wokwi]] for programming/simulating the code to run on the [[ESP32]]. - Make sure to [[Instalando o driver da ESP32 no Windows|install the ESP32 driver]] to be able to upload the code to the [[ESP32]]. # Hardware Connections * **[[Sensor de Distância Ultrassônico HC-SR04]]:** * **Pin VCC:** Connect to the VCC on the [[ESP32]] * **Pin TRIG:** Connect to a GPIO digital pin in the [[ESP32]] * **Pin ECHO:** Connect to a GPIO digital pin in the [[ESP32]] * **Pin GND:** Connect to GND of the [[ESP32]] ![[HC-SR04 with ESP32 image.png]] # Example Code 1 - Simple way to Measure Distance ```cpp #define PIN_TRIG 19 #define PIN_ECHO 18 void setup() { Serial.begin(115200); pinMode(PIN_TRIG, OUTPUT); pinMode(PIN_ECHO, INPUT); } void loop() { // Start a new measurement: digitalWrite(PIN_TRIG, HIGH); delayMicroseconds(10); digitalWrite(PIN_TRIG, LOW); // Read the result: int duration = pulseIn(PIN_ECHO, HIGH); Serial.print("Distance in CM: "); Serial.println(duration / 58); Serial.print("Distance in inches: "); Serial.println(duration / 148); delay(1000); } ``` ## Explanation This Arduino code uses an HC-SR04 ultrasonic distance sensor to measure distances and print them to the serial monitor. Let's break down the code step by step: * `#define PIN_TRIG 19`: This line defines a constant named `PIN_TRIG` and sets it to 19. This pin will be used to trigger the HC-SR04 to start a measurement. * `#define PIN_ECHO 18`: This line defines a constant named `PIN_ECHO` and sets it to 18. This pin will receive the echo pulse from the HC-SR04. * `pinMode(PIN_TRIG, OUTPUT)`: Configures the `PIN_TRIG` (pin 19) as an output. This is necessary because the Arduino will send a signal out through this pin to trigger the sensor. * `pinMode(PIN_ECHO, INPUT)`: Configures the `PIN_ECHO` (pin 18) as an input. The Arduino will listen for the echo signal on this pin. * `digitalWrite(PIN_TRIG, HIGH)`: Sets the `PIN_TRIG` HIGH for 10 microseconds. This short pulse triggers the HC-SR04 to send out an ultrasonic burst. * `delayMicroseconds(10)`: Pauses the program for 10 microseconds. This ensures the trigger pulse is long enough for the sensor to detect it. * `digitalWrite(PIN_TRIG, LOW)`: Sets the `PIN_TRIG` back to LOW. The trigger pulse needs to be short. * `int duration = pulseIn(PIN_ECHO, HIGH)`: This is the core of the distance measurement. `pulseIn()` measures the length of a pulse (in microseconds) on a given pin. In this case, it's measuring the time the `PIN_ECHO` is HIGH, which corresponds to the time it took for the ultrasonic burst to travel to the object and back. * `Serial.print("Distance in CM: ");`: Prints the text "Distance in CM: " to the serial monitor. * `Serial.println(duration / 58)`: Calculates the distance in centimeters and prints it to the serial monitor. The division by 58 is derived from the speed of sound in air and the conversion from microseconds to centimeters. (The speed of sound is approximately 343 meters/second, or 0.0343 cm/microsecond. Since the sound travels to the object and back, the calculation is: distance = (0.0343 cm/µs * duration µs) / 2, which simplifies to approximately duration / 58.2). * `Serial.print("Distance in inches: ");`: Prints the text "Distance in inches: " to the serial monitor. * `Serial.println(duration / 148)`: Calculates the distance in inches and prints it to the serial monitor. The division by 148 is a similar calculation but with the conversion to inches.