- Obstacle detection in robotics projects.
- Parking sensors for vehicles.
- Liquid level measurement.
- Proximity sensing in home automation projects.
- VCC: +5V power supply.
- GND: Ground.
- Trig: Trigger pin (input). This pin initiates the ultrasonic pulse.
- Echo: Echo pin (output). This pin outputs a pulse whose width represents the time it took for the sound wave to return.
- ESP32 Development Board: This is the brain of our project. You can find them on Amazon or any electronics store.
- HC-SR04 Ultrasonic Sensor: This is the star of the show! Make sure you have one of these.
- Jumper Wires: You'll need male-to-male jumper wires to connect the sensor to the ESP32. Get a pack with various colors to keep things organized.
- Breadboard (optional): A breadboard is super handy for making temporary connections without soldering. It makes prototyping much easier.
- USB Cable: For uploading the code to your ESP32 board. It's the standard USB cable that comes with most development boards.
- Computer: You'll need a computer to write and upload the code to your ESP32.
- HC-SR04 VCC to ESP32 5V (or 3.3V, but 5V is generally recommended).
- HC-SR04 GND to ESP32 GND.
- HC-SR04 Trig to any digital pin on the ESP32 (e.g., GPIO 2, GPIO 4, or any pin you prefer). We'll use GPIO 2 in our example.
- HC-SR04 Echo to any digital pin on the ESP32 (e.g., GPIO 4, GPIO 5, or any pin you prefer). We'll use GPIO 4 in our example.
- Insert the HC-SR04 into the breadboard (optional): Place the sensor on your breadboard, ensuring that the pins are aligned with the holes. This keeps things organized and makes connecting the jumper wires easier.
- Connect VCC and GND: Use jumper wires to connect the VCC and GND pins of the HC-SR04 to the 5V and GND pins of the ESP32. If you don't have a breadboard, simply connect the wires directly.
- Connect Trig and Echo: Now, connect the Trig pin of the HC-SR04 to a digital pin on the ESP32. In our example, we're using GPIO 2. Next, connect the Echo pin of the HC-SR04 to another digital pin on the ESP32. We're using GPIO 4 in our example. Make sure your connections are secure and all the wires are properly in place.
Hey everyone! Today, we're diving into a super cool project: interfacing the HC-SR04 ultrasonic sensor with an ESP32! If you're into electronics, robotics, or just enjoy tinkering, this guide is perfect for you. We'll break down everything you need to know, from understanding how the sensor works to writing the code and getting it all connected. No prior experience is needed – we'll go through each step together. So, grab your ESP32, your HC-SR04 sensor, and let's get started!
What is the HC-SR04 Ultrasonic Sensor?
First things first, let's understand what the HC-SR04 ultrasonic sensor is all about. This little gadget is a distance measuring device that uses sound waves to detect objects. Think of it like a bat! The sensor sends out an ultrasonic pulse, and when it hits an object, it bounces back. The sensor then calculates the distance by measuring the time it takes for the sound wave to return. It's a fantastic and affordable way to measure distances in your projects. Its simplicity and ease of use make it a favorite among hobbyists and professionals alike. The HC-SR04 has two main parts: a transmitter that sends out the ultrasonic pulse and a receiver that picks up the echo. The distance is calculated using the following formula:
Distance = Speed of sound * Time / 2
The speed of sound is approximately 343 meters per second (or 0.0343 cm/µs). The division by 2 is because the time measured is for the round trip (from the sensor to the object and back). The HC-SR04 ultrasonic sensor is widely used in various applications, including:
It's a versatile component that can be integrated into many different projects! The sensor typically has four pins:
Understanding these pins is crucial for connecting the sensor to your ESP32.
Components You'll Need
Alright, before we get our hands dirty, let's gather all the necessary components. Here's a quick checklist to make sure you're ready to go:
With these components, you're all set to begin. Make sure you have all of these before you move forward. Now that you've got your components lined up, let's move on to the next step and get this sensor connected to your ESP32.
Connecting the HC-SR04 to Your ESP32
Now comes the fun part: connecting the HC-SR04 ultrasonic sensor to your ESP32! This step is straightforward, and we'll break it down into simple steps. First, let's look at the pin connections:
Using a breadboard will make this process a breeze, but it's not strictly necessary. Let's walk through how to connect the sensor to the ESP32 step by step:
Double-check all the connections to ensure they are secure and correctly matched. Incorrect connections can damage your components, so it's always worth double-checking. With your sensor connected, you're ready to move on to the code! This is the most important part of the tutorial since you are now making your sensor functional.
Programming the ESP32 for the HC-SR04
Okay, time to fire up your computer and write some code! We'll be using the Arduino IDE, a user-friendly platform that makes programming the ESP32 a piece of cake. If you don't have it installed already, download it from the Arduino website and install the ESP32 board support. Here's a breakdown of the code we'll use, along with explanations.
// Define the pins
#define TRIGGER_PIN 2
#define ECHO_PIN 4
// Define variables
long duration;
float distance;
void setup() {
Serial.begin(115200);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// Clear the trigger pin
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
// Set the trigger pin to HIGH for 10 microseconds
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// Measure the duration of the echo pulse
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500); // Wait for 500 milliseconds
}
Let's break down the code into sections to help you understand it step by step:
-
Pin Definitions:
#define TRIGGER_PIN 2 #define ECHO_PIN 4These lines define the GPIO pins of the ESP32 that we connected the Trig and Echo pins of the HC-SR04 to. This makes the code readable and easy to modify.
-
Variable Definitions:
long duration; float distance;We declare two variables:
durationto store the time it takes for the echo to return, anddistanceto store the calculated distance. We are going to need these variables to store values. -
Setup Function:
void setup() { Serial.begin(115200); pinMode(TRIGGER_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); }Inside the
setup()function:Serial.begin(115200);initializes the serial communication at a baud rate of 115200, allowing us to print data to the Serial Monitor.pinMode(TRIGGER_PIN, OUTPUT);sets the Trig pin as an output, so we can send the trigger signal.pinMode(ECHO_PIN, INPUT);sets the Echo pin as an input, so we can receive the echo signal.
-
Loop Function:
void loop() { // Clear the trigger pin digitalWrite(TRIGGER_PIN, LOW); delayMicroseconds(2); // Set the trigger pin to HIGH for 10 microseconds digitalWrite(TRIGGER_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIGGER_PIN, LOW); // Measure the duration of the echo pulse duration = pulseIn(ECHO_PIN, HIGH); // Calculate the distance distance = duration * 0.034 / 2; // Print the distance to the Serial Monitor Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); delay(500); // Wait for 500 milliseconds }Inside the
loop()function:digitalWrite(TRIGGER_PIN, LOW);anddelayMicroseconds(2);ensure the Trig pin is low before sending a pulse.digitalWrite(TRIGGER_PIN, HIGH);sets the Trig pin high for 10 microseconds to initiate the ultrasonic pulse.duration = pulseIn(ECHO_PIN, HIGH);measures the time the Echo pin is high (the echo duration).distance = duration * 0.034 / 2;calculates the distance in centimeters using the formula.- The
Serial.print()functions display the distance on the Serial Monitor. delay(500);adds a delay of 500 milliseconds (half a second) between measurements.
Make sure you understand the basics of the code and make sure to copy it correctly! Now it's time to upload the code.
Uploading the Code to Your ESP32
Alright, you've written the code, and now it's time to upload it to your ESP32 board. Here's how:
- Open the Arduino IDE: Make sure you've installed the Arduino IDE and have the ESP32 board support added.
- Select Your Board and Port: In the Arduino IDE, go to Tools > Board and select your ESP32 board. Then, go to Tools > Port and select the COM port that your ESP32 is connected to.
- Copy and Paste the Code: Copy the code provided earlier and paste it into the Arduino IDE.
- Verify the Code: Click the
Lastest News
-
-
Related News
IIoT, E-Scooters, Space & SEC/ESC Financing: Key Insights
Alex Braham - Nov 12, 2025 57 Views -
Related News
Top Electricity Companies In South Africa
Alex Braham - Nov 13, 2025 41 Views -
Related News
2025 Hyundai Santa Fe SEL AWD: First Look
Alex Braham - Nov 13, 2025 41 Views -
Related News
Master Free Fire 3D Intro Green Screen For Epic Edits
Alex Braham - Nov 13, 2025 53 Views -
Related News
PSEIJAZZGhostse Otaku Adventure: Episode 1
Alex Braham - Nov 9, 2025 42 Views