[转载] [Arduino模块]SRF04/05 超声波传感器
2013-12-181.3万
AI 快速预览详细 收起
本文介绍了如何使用SRF04/05超声波传感器进行距离测量。文章提供了详细的电路图和代码示例,帮助用户快速实现这一功能。通过连接传感器到Arduino,用户可以轻松测量物体的距离。
[Arduino模块]
SRF04/05 超声波传感器
电路示意:
![]()
代码示例:
- #define ECHOPIN 2 // Pin to receive echo pulse
- #define TRIGPIN 3 // Pin to send trigger pulse
-
- void setup(){
- Serial.begin(9600);
- pinMode(ECHOPIN, INPUT);
- pinMode(TRIGPIN, OUTPUT);
- }
-
- void loop(){
- digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
- delayMicroseconds(2);
- digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
- delayMicroseconds(10);
- digitalWrite(TRIGPIN, LOW); // Send pin low again
- int distance = pulseIn(ECHOPIN, HIGH); // Read in times pulse
- distance= distance/58; // Calculate distance from time of pulse
- Serial.println(distance);
- delay(50); // Wait 50mS before next ranging
- }
复制代码
|