第五篇、基于Arduino uno,获取超声波(HC04)传感器的距离数据——结果导向

0、结果

说明:先来看看串口调试助手显示的结果,显示的是一个距离值,如果是你想要的,可以接着往下看。

1、外观

说明:虽然超声波传感器形态各异,但是原理和代码都是适用的。

2、连线

说明:只需要连接四根线。

  • uno————超声波传感器
  •     5V--------------VCC
  • GND--------------GND
  •       3--------------Trig
  •       4--------------Echo

3、源程序

说明:采用非阻塞方式编写,一定时间检测一次距离值,对距离值进行了简单的滤波,并将对应功能进行函数化,方便移植。

/*
  VCC------5V
  GND------GND
  Trig-----3
  Echo-----4
*/
/****************************************hc04 part****************************************/
#define hc04TimeOut 100                                  //Check once in 50 milliseconds       
#define filterNumber 3                                   //Number of filtration
const int Trig = 3;                                      //Connect the corresponding ultrasonic Angle
const int Echo = 4;                                      //Connect the corresponding ultrasonic Angle

unsigned long hc04Times = 0;                             //Record the device running time
int beforeVal = 0;                                       //The previous distance value
int presentVal = 0;                                      //Current distance value
int averageVal = 0;                                      //The average of the distance between one and the other
int diffval = 10;                                        //The difference between the previous distance and the next distance
int frequency = 0;                                       //Number of filtration
double distance, time ;                                  //Find the distance value
/****************************************set up and loop part*********************************/
void setup()
{
  Serial.begin(9600);                                    //Example Initializing serial port communication
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);                                  //Configure the working mode of the pin
  Serial.println("The distance is :");
}
void loop()
{
  getdata();                                             //Acquisition distance
}
/****************************************hc04 part****************************************/
/*Acquisition distance*/
void getdata() {
  if (millis() - hc04Times >= hc04TimeOut) {              //Check a distance at a certain time
    hc04Times = millis();
    digitalWrite(Trig, LOW);
    delayMicroseconds(2);
    digitalWrite(Trig, HIGH);
    delayMicroseconds(10);                               //Generate a high pulse of 10us to trigger SR04
    digitalWrite(Trig, LOW);
    time = pulseIn(Echo, HIGH);                          //Check the pulse width and note that the return value is microseconds us
    distance = time / 58 ;                               //Calculate the distance. The output distance is measured in centimeters
    filterData();                                          //Filter the distance value

    Serial.print("distance: ");
    Serial.print(averageVal);                            //Print distance value
    Serial.println(" cm");
  }
}
/*Filter the distance value*/
void filterData() {
  if (abs(averageVal - distance) > diffval) {
    if (++frequency >= filterNumber) {
      frequency = 0;
      beforeVal = averageVal;                                //The average is given to the previous value
      presentVal = distance;                                 //Distance value to the current value
      averageVal = beforeVal + (presentVal - beforeVal) / 2; //Find the current distance
    }
  } else {
    beforeVal = averageVal;                                  //The average is given to the previous value
    presentVal = distance;                                   //Distance value to the current value
    averageVal = beforeVal + (presentVal - beforeVal) / 2;   //Find the current distance
    if (averageVal <= 3) {                                   //When the distance is less than 3 centimeters, there's a lot of data, so we set it to 0
      averageVal = 0;
    }
    frequency = 0;                                           //Clears the number of times of the last filter
  }
}

4、注意事项

说明:超声波探测的区域是一个扇形区域。如果从一个短距离到一个比较远的距离,由于超声波会等待波返回,这个时间大概在几十毫秒,对实时性要求高的程序来说,在一定程度上会延迟执行运行。

5、基本原理

        超声波传感器是一种利用超声波来测量距离的传感器,其测量原理基于声学原理。当超声波传感器工作时,它会发出一个高频的声音信号,这个声音信号会转化为超声波,并向目标物体传播。当超声波遇到目标物体后,一部分能量被反射回传感器,传感器会接收到这一部分反射回来的超声波信号,并通过计算信号的往返时间,来确定目标物体与传感器之间的距离。

        通常情况下,超声波传感器需要由一个发射器和一个接收器构成,发射器用来产生超声波,接收器用来接收反射回来的超声波,并将其转换成电信号。此外,不同型号的超声波传感器对于距离和精度的测量范围有所区别,一般传感器的测量范围在几厘米到几米不等,其精度也随之而定。超声波传感器主要应用于避障、物体检测、距离测量等领域。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值