极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 290462|回复: 109

Arduino学习笔记A2 - Arduino连接超声波传感器测距

  [复制链接]
发表于 2011-9-6 01:29:39 | 显示全部楼层 |阅读模式
本帖最后由 Ansifa 于 2013-6-3 12:25 编辑

Arduino连接超声波传感器测距


注:方法已经过时,新方法使用timer去计时,请看https://code.google.com/p/arduino-new-ping/

超声波传感器适用于对大幅的平面进行静止测距。普通的超声波传感器测距范围大概是2cm~450cm,分辨率3mm(淘宝卖家说的,笔者测试环境没那么好,个人实测比较稳定的距离10cm~2m左右,超过此距离就经常有偶然不准确的情况发生了,当然不排除笔者技术问题。)

测试对象是淘宝上面最便宜的SRF-04超声波传感器,有四个脚:5v电源脚(Vcc),触发控制端(Trig),接收端(Echo),地端(GND)
附:SRF系列超声波传感器参数比较
http://www.acroname.com/robotics/info/articles/devantech/srf.html


模块工作原理:
  • 采用IO触发测距,给至少10us的高电平信号;
  • 模块自动发送8个40KHz的方波,自动检测是否有信号返回;
  • 有信号返回,通过IO输出一高电平,高电平持续的时间就是超声波从发射到返回的时间.测试距离=(高电平时间*声速(340m/s))/2;
电路连接方法:


Arduino程序例子:
const int TrigPin = 2;
const int EchoPin = 3;
float cm;
void setup()
{
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

评分

参与人数 1 +1 收起 理由
弘毅 + 1 赞一个!

查看全部评分

回复

使用道具 举报

发表于 2013-5-27 20:57:40 | 显示全部楼层
做了这个实验,让测试结果在1602液晶屏上显示。用了SR04的库。
  1. #include "SR04.h"
  2. #define TRIG_PIN 8
  3. #define ECHO_PIN 9
  4. #include <LiquidCrystal.h>
  5. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  6. SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
  7. long a;

  8. void setup() {
  9.    lcd.begin(16, 2);
  10.      delay(1000);
  11. }

  12. void loop() {
  13.    a=sr04.Distance();
  14.   lcd.clear(); //清屏
  15.   lcd.print("Now Range is"); //使屏幕显示文字
  16.   lcd.setCursor(0, 1) ; //设置光标位置为第二行第一个位置
  17.   lcd.print(a);   //显示温度
  18.   lcd.print("CM"); //显示字母
  19.    delay(1000);
  20. }
复制代码

评分

参与人数 1 +24 收起 理由
Ansifa + 24 赞一个!

查看全部评分

回复 支持 4 反对 0

使用道具 举报

发表于 2013-5-21 20:54:09 | 显示全部楼层
好贴,支持一下。
回复 支持 1 反对 0

使用道具 举报

发表于 2011-9-16 19:20:12 | 显示全部楼层
是否可以加上电脑上返回的信号图片
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-9-17 00:05:19 | 显示全部楼层
piaozhiling 发表于 2011-9-16 19:20
是否可以加上电脑上返回的信号图片

这个超声波在公司,星期一才贴图片,大概结果就是
10cm
20cm
22cm
100cm
105cm
类似这样.有换行
回复 支持 反对

使用道具 举报

发表于 2011-11-23 19:11:54 | 显示全部楼层
  还成....再加上红外避障就完美了
回复 支持 反对

使用道具 举报

发表于 2011-11-24 09:20:33 | 显示全部楼层
Kinect上用的红外距离传感器实现有人了解么?
回复 支持 反对

使用道具 举报

发表于 2011-11-24 10:21:19 | 显示全部楼层
我看国外的视频有三个脚的超声波传感器,G V S 那我想要是把这trig和echo连载一个pin上,改下代码 能工作么
回复 支持 反对

使用道具 举报

发表于 2012-1-17 19:51:06 | 显示全部楼层
wali 发表于 2011-11-24 10:21
我看国外的视频有三个脚的超声波传感器,G V S 那我想要是把这trig和echo连载一个pin上,改下代码 能工作么

两个pin的模式不一样,应该不能接在一起的吧。不确定
回复 支持 反对

使用道具 举报

发表于 2012-3-27 22:08:11 | 显示全部楼层
very Good!!!!!!!!!
回复 支持 反对

使用道具 举报

发表于 2012-4-3 17:03:46 | 显示全部楼层
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);

这段代码看不懂 为什么要除以58呢 楼主给说明一下吧~
回复 支持 反对

使用道具 举报

发表于 2012-4-7 10:59:03 | 显示全部楼层
alextime 发表于 2012-4-3 17:03
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位 ...

我原来也有同问。
看了arduino cookbook就知道了。
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
声速是340m/s换算一下就是29ms/cm了。
回复 支持 反对

使用道具 举报

发表于 2012-4-10 16:49:00 | 显示全部楼层
咱家宝的大胖熊 发表于 2012-4-7 10:59
我原来也有同问。
看了arduino cookbook就知道了。
long microsecondsToCentimeters(long microseconds ...

谢了啊 呵呵
回复 支持 反对

使用道具 举报

发表于 2012-4-18 15:10:52 | 显示全部楼层
alextime 发表于 2012-4-10 16:49
谢了啊 呵呵

支持,正好找了一个传感器,用手水平转了一圈扫的数据~~

右上的平面是电脑的屏幕,旁边突出来的是热水壶,右下应该是本人(那个尖尖是啥玩意{:soso__15007980067068905509_1:}),左下的平面是座位的隔板,左边的突出是我的包。左上是空档,超过100cm的都按100算了。

这种方式可以用来做为智能车判断路径的依据哎,一边走一边分析数据,就像即时战略游戏的战争黑雾设计一样,我们的小车就是一个探路兵。{:soso__10846275550858121775_4:}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

评分

参与人数 1 +30 收起 理由
Ansifa + 30 好玩,用SD卡和陀螺仪自动记录就更好了

查看全部评分

回复 支持 反对

使用道具 举报

发表于 2012-5-27 11:58:08 | 显示全部楼层
这个是我用3个超声波传感器做的壁障探测小车,我的目标是做6个超声波传感器的,但是这个程序为什么在3个超声波的时候程序就失效了,只有2个超声波传感器有效
  1. int inputPin=2;
  2. int outputPin=3;
  3. //////////////
  4. int inputPin1=12;
  5. int outputPin1=13;

  6. ////////
  7. int inputPin2=0;
  8. int outputPin2=1;
  9. ///////
  10. int distance;
  11. int distance1;
  12. int distance2;
  13. void setup()
  14.      {
  15.      Serial.begin(9600);
  16.      //第一个超声波
  17.      pinMode(inputPin,INPUT);
  18.      pinMode(outputPin,OUTPUT);
  19.      //第2个超声波
  20.      
  21.       pinMode(inputPin1,INPUT);
  22.      pinMode(outputPin1,OUTPUT);
  23.      //
  24.       pinMode(inputPin2,INPUT);
  25.      pinMode(outputPin2,OUTPUT);
  26.      
  27.      //小车运动
  28.       pinMode(8,OUTPUT);
  29.       pinMode(9,OUTPUT);
  30.       pinMode(11,OUTPUT);
  31.       pinMode(5,OUTPUT);
  32.       pinMode(6,OUTPUT);
  33.       pinMode(7,OUTPUT);
  34.      }
  35. void loop()
  36.     {
  37.     //第一个超声波
  38.     digitalWrite(outputPin,LOW);
  39.     delayMicroseconds(2);
  40.     digitalWrite(outputPin,HIGH);
  41.     delayMicroseconds(10);
  42.     int distance=pulseIn(inputPin,HIGH);
  43.     distance=distance/58;
  44.     //第二个超声波
  45.     digitalWrite(outputPin1,LOW);
  46.     delayMicroseconds(2);
  47.     digitalWrite(outputPin1,HIGH);
  48.     delayMicroseconds(10);
  49.     distance1=pulseIn(inputPin1,HIGH);
  50.     distance1=distance1/58;
  51.    //第3个超声波
  52.     digitalWrite(outputPin2,LOW);
  53.     delayMicroseconds(2);
  54.     digitalWrite(outputPin2,HIGH);
  55.     delayMicroseconds(10);
  56.     distance2=pulseIn(inputPin2,HIGH);
  57.     distance2=distance2/58;
  58.    
  59.     //
  60.     //Serial.print("distance = ");
  61.     //Serial.println(distance);
  62.     //
  63.     Serial.print(",distance = ");
  64.   Serial.println(distance1);
  65.     //
  66.     Serial.print(",distance = ");
  67.     Serial.println(distance2);
  68.      
  69.      
  70.      Goahead();
  71.      delay(20);
  72.      if( distance >= 50)
  73.     {
  74.    // digitalWrite(ledpin,HIGH);
  75.     // delay(100);
  76.      Goahead();
  77.     }
  78.     else
  79.     {
  80.    // digitalWrite(ledpin,LOW);
  81.      //delay(100);
  82.     Turn_left();
  83.      }
  84.    
  85.    if( distance1 >= 50 )
  86.     {
  87.      // digitalWrite(ledpin,HIGH);
  88.     // delay(100);
  89.     // Goahead();
  90.     }
  91.     else
  92.     {
  93.    // digitalWrite(ledpin,LOW);
  94.      //delay(100);
  95.      //Stop();
  96.      Turn_right();
  97.     }  
  98.    
  99.    
  100.      if( distance2 >= 50)
  101.         {
  102.         // digitalWrite(ledpin,HIGH);
  103.         // delay(100);
  104.        Goahead();   
  105.         }
  106.     else
  107.        {
  108.          // digitalWrite(ledpin,LOW);
  109.         //delay(100);
  110.          Stop();
  111.       }
  112.    
  113.    
  114.     }
  115.    
  116.   ///////////////////////////////  
  117. void Goahead()
  118.      {
  119.      analogWrite(5,145);
  120.      digitalWrite(6,HIGH);
  121.      digitalWrite(7,LOW);
  122.      analogWrite(11,160);
  123.      digitalWrite(8,HIGH);
  124.      digitalWrite(9,LOW);
  125.     }


  126. void Stop()//停止程序
  127.      {
  128.     digitalWrite(6,HIGH);
  129.     digitalWrite(7,HIGH);
  130.     digitalWrite(8,HIGH);
  131.     digitalWrite(9,HIGH);
  132.     }  
  133.   
  134. void Turn_right()//右转
  135.     {
  136.     analogWrite(5,165);
  137.     digitalWrite(6,LOW);
  138.     digitalWrite(7,HIGH);
  139.     analogWrite(11,140);
  140.     digitalWrite(8,HIGH);
  141.    digitalWrite(9,LOW);
  142.    }

  143. void Turn_left()
  144.    {
  145.     analogWrite(5,165);
  146.     digitalWrite(6,HIGH);
  147.     digitalWrite(7,LOW);
  148.     analogWrite(11,160);
  149.     digitalWrite(8,LOW);
  150.     digitalWrite(9,HIGH);
  151.    
  152.    }
复制代码
回复 支持 反对

使用道具 举报

发表于 2012-5-31 15:16:50 | 显示全部楼层
饥渴的 发表于 2012-5-27 11:58
这个是我用3个超声波传感器做的壁障探测小车,我的目标是做6个超声波传感器的,但是这个程序为什么在3个超声 ...

谢谢问题我解决了··还有个问题那个怎么把超声波收到的数据用matlab连起来呢?
回复 支持 反对

使用道具 举报

发表于 2012-6-23 21:20:12 | 显示全部楼层
求教,用的超声波传感器和您的一样,程序用的也是您的。但是不知道为什么,输出的总是0.00cm。求指导,谢谢!!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 21:05 , Processed in 0.070107 second(s), 34 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表