[转载] [Arduino模块]SRF04/05 超声波传感器

2013-12-181.3万
AI 快速预览详细 收起
本文介绍了如何使用SRF04/05超声波传感器进行距离测量。文章提供了详细的电路图和代码示例,帮助用户快速实现这一功能。通过连接传感器到Arduino,用户可以轻松测量物体的距离。
[Arduino模块]
SRF04/05 超声波传感器

电路示意:



代码示例:
  1. #define ECHOPIN 2                            // Pin to receive echo pulse
  2. #define TRIGPIN 3                            // Pin to send trigger pulse
  3. void setup(){
  4.   Serial.begin(9600);
  5.   pinMode(ECHOPIN, INPUT);
  6.   pinMode(TRIGPIN, OUTPUT);
  7. }
  8. void loop(){
  9.   digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  10.   delayMicroseconds(2);
  11.   digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  12.   delayMicroseconds(10);
  13.   digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  14.   int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  15.   distance= distance/58;                        // Calculate distance from time of pulse
  16.   Serial.println(distance);                     
  17.   delay(50);                                    // Wait 50mS before next ranging
  18. }
复制代码





创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -