HC-SR04 Ultrasonic Sensor with ESP32 DOIT Kit v1

MuhammadMuhammadESP322 months ago23 Views

Introduction

The HC-SR04 Ultrasonic Sensor is widely used for distance measurement in robotics, automation, and obstacle detection projects. It works by sending out ultrasonic waves and measuring the time it takes for the waves to bounce back after hitting an object.

The ESP32 makes an excellent choice for working with the HC-SR04 because it offers fast processing, multiple GPIO pins, and precise timing functions that help in measuring distance accurately.

In this tutorial, we will:

  • Learn how the HC-SR04 sensor works.
  • Connect the sensor with the ESP32 DOIT Kit v1.
  • Write Arduino code to measure distance.
  • Display the measured distance in the Serial Monitor.

How HC-SR04 Works

  • Trigger Pin: Sends out an ultrasonic pulse (40kHz).
  • Echo Pin: Receives the reflected pulse.
  • Time Measurement: The duration between sending and receiving is proportional to distance.

📏 Formula for distance:

  • 0.0343 cm/µs = speed of sound.
  • Divided by 2 because sound travels to the object and back.

Components Required

  • ESP32 DOIT Kit v1
  • HC-SR04 Ultrasonic Sensor
  • Jumper wires
  • Breadboard

ESP32 & HC-SR04 Pin Connections

HC-SR04 PinESP32 DOIT Kit Pin
VCC5V
GNDGND
TRIGGPIO 15
ECHOGPIO 2

Arduino Code

#define TRIG_PIN 15
#define ECHO_PIN 2

long duration;
float distance;

void setup() {
  Serial.begin(115200);   // Start serial communication
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  // Clear the trigger pin
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  // Send a 10µs pulse to trigger
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Measure the duration of echo pulse
  duration = pulseIn(ECHO_PIN, HIGH);

  // Calculate distance in cm
  distance = (duration * 0.0343) / 2;

  // Print distance on Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(1000);  // Wait for 1 second
}

Step-by-Step Code Explanation

Pin Definitions

#define TRIG_PIN 15
#define ECHO_PIN 2

Assigns ESP32 pins 15 and 2 for TRIG and ECHO.

Setup Function

pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

TRIG is set as output (to send signal), ECHO as input (to receive).

Sending Trigger Pulse

digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

Sends a short 10µs pulse to start measurement.

Measuring Echo

duration = pulseIn(ECHO_PIN, HIGH);

Measures time (in microseconds) that ECHO pin stays HIGH.

Calculating Distance

distance = (duration * 0.0343) / 2;

Converts time into distance in centimeters.

Output Result

Serial.println("Distance: ...");

Displays measured distance in the Serial Monitor.

Real-Life Applications of HC-SR04 with ESP32

  • 🚗 Obstacle avoidance robots
  • 🚪 Automatic doors (detects people approaching)
  • 📦 Smart parking systems (detects free parking spots)
  • 🌊 Water level monitoring in tanks
  • 🏠 Home security systems (detects motion/entry)

Troubleshooting

  • No readings / always 0 → Check wiring (TRIG must be OUTPUT, ECHO as INPUT).
  • Always constant distance → Ensure nothing is blocking the sensor.
  • Random values → Use stable power supply (prefer 5V).
  • Error due to range → HC-SR04 works best between 2cm to 400cm.

What is the operating voltage of the HC-SR04 sensor?

The HC-SR04 typically works with 5V, but it can be interfaced with ESP32 using a voltage divider on the Echo pin.

Which ESP32 pins are used for HC-SR04 in this tutorial?

We connected Trigger → GPIO15 and Echo → GPIO2.

What is the maximum range of the HC-SR04 ultrasonic sensor?

It can measure distances from 2 cm to 400 cm with an accuracy of about ±3 mm.

Why is my sensor giving unstable readings?

Unstable readings may occur due to electrical noise, reflective surfaces, or poor wiring. Use averaging in code to smooth values.

Can the HC-SR04 be used outdoors with ESP32?

Yes, but performance may reduce in direct sunlight or windy conditions. For reliable outdoor use, a waterproof ultrasonic sensor (like JSN-SR04T) is recommended.

Leave a reply

Loading Next Post...
Follow
Search Trending
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...