SR04 Ultrasonic Distance Sensor
Module Source
Purchase Link: https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-19589090137.10.33c336b4KtAN1t&id=608943401463
Baidu Netdisk Download Link:
https://pan.baidu.com/s/1sSah9PvLBrmbA7So-6YcSw
Password:qq35
Specifications
Working Voltage: 5-5.5V
Working Current: 5.3mA
Sensing Angle: Less than 15 degrees
Detection Range: 2CM-600CM
Detection Accuracy: 0.1CM + 1%
Output Method: GPIO
Pin Count: 4 Pin
Hardware Connection
- VCC: Connect to the 5V output of the development board.
- Trig (Trigger): Connect to the digital output pin 9 of the development board.
- Echo: Connect to the digital input pin 10 of the development board.
- GND: Connect to the ground (GND) of the development board.
Usage Method
This code snippet first generates a short (10 microsecond) high pulse on the trigPin
to activate the HC-SR04 to start the distance measurement. Then, it measures the duration of the high pulse received on the echoPin
(the time taken for the sound to travel from transmission to the echo's return). Using this time and the speed of sound (approximately 340 m/s or converted to cm/µs), the distance is calculated and then output through the development board's serial port.
/******************************************************************************
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 8, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
// 定义连接到传感器的针脚
const int trigPin = 9;
const int echoPin = 10;
// 定义变量来存储测量的持续时间和计算的距离
long duration;
int distance;
void setup() {
// 初始化串口通信
Serial.begin(9600);
// 设置触发针脚为输出,回声针脚为输入
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// 清除触发针脚
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// 设置触发针脚高电平状态持续10微秒
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// 读取回声针脚,返回声波往返时间(以微秒为单位)
duration = pulseIn(echoPin, HIGH);
// 计算距离:使用声速(34000厘米/秒)计算距离(往返所以除以2)
distance = duration * 0.034 / 2;
// 将距离发送到串口
Serial.print("Distance: ");
Serial.println(distance);
// 短暂的延迟避免过快刷新
delay(500);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Usage Testing
Place an object 20CM away from the distance sensor, and use the development board to measure the distance between the sensor and the object, then output the result through the serial port.