Product Introduction #
Ultrasonic sensor is a sensor used to measure distance, can be used to help the car to avoid obstacles or other related distance obstacle avoidance projects.
The ultrasonic transmitter emits ultrasonic waves in a certain direction, at the same time the timer starts timing, ultrasonic waves spread in the air, on the way to encounter obstacles will immediately return, ultrasonic receiver received reflected waves immediately stop timing. The propagation speed of sound wave in the air is 340m/s. According to the time t recorded by the timer, the distance s between the launching point and the obstacle can be calculated, that is, s=340*t/2.
Application reference: obstacle avoidance car, handheld range finder, water level monitoring, ultrasonic counter, etc.
Parameter Specification #
Parameter | Value/Description |
Operating voltage | 3.3V~5V |
Operating temperature | -40°C~+85°C |
Range of distance measurement | 3cm~500cm |
Detection accuracy | 0.1cm |
Blind area of measurement | <3cm |
Frequency of emission | 40KHz |
Size | 4.9cm*2.2cm |
Wiring Diagram #
Note:Expansion board is attached to esp32 controller board.
Ultrasonic Sensor | ESP32 |
VCC | 5V |
GND | GND |
Echo | Pin 17 |
Trig | Pin 16 |
Sample Code #
#include "SR04.h"
#define TRIG_PIN 16
#define ECHO_PIN 17
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);//Initializing the ultrasonic sensor
long a;//Create a long integer variable to store the distance detected by the ultrasound
void setup() {
Serial.begin(115200);//Initialization of Serial Port
delay(1000);
}
void loop() {
a=sr04.Distance();//Obtain the ultrasonic sensor detection value
Serial.print(a);//The value of the ultrasonic sensor is displayed on the serial port
Serial.println("cm");
delay(1000);
}
Note: If you are using an Arduino board, then change the Echo pin in the program to PIN11, change the TRIG pin to PIN12, set the baud rate to 9600, and then connect the hardware pin to PIN11, PIN12 and upload the program.
Test Result #
After uploading the code to the controller board, open the serial port monitor of the Arduino IDE and set the baud rate to 115200.
Move your hand or object in front of the sensor and you can see their distance value.
Related Resources #
Get One Now #
B2B Business: info@acebott.com
Official Website: Ultrasonic Sensor