Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 23: Ultrasonic Sensor Module (HC-SR04)

In this lesson, you’ll learn how to use an ESP32 Development Board for measuring distance with an ultrasonic sensor (HC-SR04). We’ll cover setting up the sensor, reading data, and calculating the distance in centimeters. This project is ideal for beginners working with microcontrollers and sensors, offering hands-on experience in data acquisition and serial communication. You’ll develop practical skills in programming the ESP32 and grasp ultrasonic sensing technology.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

Ultrasonic Sensor Module (HC-SR04)

BUY

Breadboard

BUY

Wiring

../_images/Lesson_23_Ultrasonic_esp32_bb.png

Code

Code Analysis

  1. Pin declaration:

    Start by defining the pins for the ultrasonic sensor. echoPin and trigPin are declared as integers and their values are set to match the physical connection on the ESP32 Development Board.

    const int echoPin = 26;
    const int trigPin = 25;
    
  2. setup() function:

    The setup() function initializes the serial communication, sets the pin modes, and prints a message to indicate the ultrasonic sensor is ready.

    void setup() {
      Serial.begin(9600);
      pinMode(echoPin, INPUT);
      pinMode(trigPin, OUTPUT);
      Serial.println("Ultrasonic sensor:");
    }
    
  3. loop() function:

    The loop() function reads the distance from the sensor and prints it to the serial monitor, then delays for 400 milliseconds before repeating.

    void loop() {
      float distance = readDistance();
      Serial.print(distance);
      Serial.println(" cm");
      delay(400);
    }
    
  4. readDistance() function :

    The readDistance() function triggers the ultrasonic sensor and calculates the distance based on the time it takes for the signal to bounce back.

    For more details, please refer to the working principle of the ultrasonic sensor module.

    float readDistance() {
      digitalWrite(trigPin, LOW);   // Set trig pin to low to ensure a clean pulse
      delayMicroseconds(2);         // Delay for 2 microseconds
      digitalWrite(trigPin, HIGH);  // Send a 10 microsecond pulse by setting trig pin to high
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);  // Set trig pin back to low
      float distance = pulseIn(echoPin, HIGH) / 58.00;  // Formula: (340m/s * 1us) / 2
      return distance;
    }