搜索代码

ESP32 Tutorial 27/55 - Measuring Distanc with Ultrasonic Sensor | SunFounder's ESP32 IoT Learning kit

ESP32 Tutorial 27/55 - Measuring Distanc with Ultrasonic Sensor | SunFounder's ESP32 IoT Learning kit

In this tutorial, we will learn how to use an ultrasonic distance sensor with the ESP32 to measure distances and display the results on a screen. We will also explore how to activate a buzzer when an object is detected within a specific range. This project highlights the versatility of the ESP32 microcontroller, which integrates Wi-Fi and Bluetooth capabilities, making it suitable for various IoT applications.

ultrasonic_principle

Throughout this guide, you will find explanations of the hardware components, wiring instructions, and code snippets to help you implement the project successfully. For additional clarity, please refer to the video (in video at 00:00).

Hardware Explained

The primary components of this project include the ESP32 microcontroller, the ultrasonic sensor (HC-SR04), and a buzzer. The ultrasonic sensor consists of two main parts: a transmitter that emits ultrasonic waves and a receiver that listens for the reflected waves. By measuring the time it takes for the waves to return, we can calculate the distance to an object.

The ESP32 serves as the central controller, processing data from the ultrasonic sensor and controlling the buzzer based on the measured distance. The buzzer will sound when the detected distance is less than a defined threshold, indicating that an object is too close.

Datasheet Details

Manufacturer HC-SR04
Part number HC-SR04
Logic/IO voltage 5 V
Supply voltage 5 V
Output current (per channel) 16 mA
Peak current (per channel)
PWM frequency guidance
Input logic thresholds
Voltage drop / RDS(on) / saturation
Thermal limits
Package
Notes / variants Measuring range: 2 cm to 400 cm

 

  • Ensure proper power supply of 5 V for the sensor.
  • Maintain a clear path for ultrasonic waves to avoid interference.
  • Use short wires to minimize signal delay and noise.
  • Ensure correct pin connections to avoid miscommunication.
  • Test the sensor in various environments to check for accuracy.

Wiring Instructions

ESP32-27_ultrasonic_wiring

To wire the ultrasonic sensor to the ESP32, connect the VCC pin of the sensor to the 5V pin on the ESP32. Next, connect the GND pin of the sensor to one of the ground (GND) pins on the ESP32. The trigger pin (Trig) from the sensor should be connected to GPIO 26 on the ESP32, while the echo pin (Echo) should be connected to GPIO 25. This setup allows the ESP32 to send a signal to the sensor and receive the echo signal to calculate distance.

ESP32-27_ultrasonic_schematic

For the buzzer, connect the positive (long) leg to GPIO 12 on the ESP32 and the negative (short) leg to the GND. Ensure that all connections are secure to prevent any loose wiring during operation. If using a breadboard, align the buzzer connections properly to avoid miswiring. Make sure to refer to the video for wiring confirmation (in video at 05:12).

Code Examples & Walkthrough

The code initializes the pins for the ultrasonic sensor and sets up serial communication. The main function, readSensorData(), is responsible for sending a trigger signal and measuring the response time from the echo pin.

const int echoPin = 25;
const int trigPin = 26;

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

The code defines echoPin and trigPin as constants for the pins connected to the ultrasonic sensor. In the setup() function, we initialize serial communication and set the pin modes accordingly.

float readSensorData() {
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  unsigned long microsecond = pulseIn(echoPin, HIGH);
  float distance = microsecond / 29.00 / 2;
  return distance;
}

This excerpt shows the readSensorData() function, which sends a 10-microsecond pulse to the trigger pin. It then measures the time taken for the echo pin to receive the signal back, calculates the distance, and returns this value.

Finally, the loop function continuously reads the distance and prints it to the serial monitor. If the distance is below 20 cm, the buzzer will be activated.

Demonstration / What to Expect

When you run the program, the ESP32 will display the measured distance in centimeters on the serial monitor. If an object enters within 20 cm of the sensor, the buzzer will sound. This behavior can be tested by placing objects at various distances in front of the sensor. Be cautious of reversed polarity and ensure that the connections are made as specified to avoid any issues during operation (in video at 08:00).

Video Timestamps

  • 00:00 Start
  • 1:46 Introduction to Ultrasonic Sensor
  • 6:02 Wiring explained
  • 7:37 ESP32 Arduino code explained
  • 11:33 Selecting ESP32 board and COM port in Arduino IDE
  • 13:15 Demonstration of measuring distance
  • 16:43 Tacking action with distance: buzzer

图像

ESP32-27_ultrasonic_wiring
ESP32-27_ultrasonic_wiring
ESP32-27_ultrasonic_schematic
ESP32-27_ultrasonic_schematic
ultrasonic_principle
ultrasonic_principle
827-ESP32 Tutorial 27/55- Arduino code for ultrasonic sensor
语言: C++
/*
 * // 为超声波传感器定义引脚
 */
const int echoPin = 25;
const int trigPin = 26;

 // 读取传感器数据的函数原型
float readSensorData();

void setup() {
 // 以115200波特率开始串行通信
  Serial.begin(115200);
 // 将echoPin设置为输入,将trigPin设置为输出。
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);
 // 将传感器信息打印到串行监视器
  Serial.println("Ultrasonic sensor:");
}

void loop() {
 // 读取超声波传感器的距离
  float distance = readSensorData();
 // 将测量的距离打印到串行监视器上
  Serial.print(distance);
  Serial.println(" cm");
 // 读取之间的延迟
  delay(200);
}

 // 从超声波传感器读取数据的功能
float readSensorData() {
 // 在发送高信号之前触发低信号
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
 // 向 trigPin 发送一个10微秒的高电平信号
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
 // 返回低信号
  digitalWrite(trigPin, LOW);

 // 测量回声引脚上的高信号持续时间
  unsigned long microsecond = pulseIn(echoPin, HIGH);

 // 使用声速(每厘米29.00微秒)计算距离。
  float distance = microsecond / 29.00 / 2;

 // 返回计算出的距离
  return distance;
}

|||您可能需要的东西

文件📁

没有可用的文件。