超声波模块实验

超声波模块实验

实验现象

串口输出超声波模块和格挡物体之间的距离,单位为cm

理论学习

超声波测距原理是在超声波发射装置发出超声波,它的根据是接收器接到超声波时的时间差,与雷达测距原理相似。 超声波发射器向某一方向发射超声波,在发射时刻的同时开始计时,超声波在空气中传播,途中碰到障碍物就立即返回来,超声波接收器收到反射波就立即停止计时。
在这里插入图片描述

原理图

在这里插入图片描述

代码编写

#define trigpin 2
#define echopin 3
float value_cm;
void setup() {
    
    
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);
}

void loop() {
    
    
  // put your main code here, to run repeatedly:
  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW);
  value_cm = float(pulseIn(echopin, HIGH) * 17) / 1000;
  Serial.print(value_cm);
  Serial.println("cm");
  delay(1000);
}

猜你喜欢

转载自blog.csdn.net/qq_45671732/article/details/109614588
今日推荐