Arduino 超声波实验程序

该博客介绍了如何使用超声波传感器进行测距实验。通过计算室内28摄氏度下声音的速度,将其转换为每微秒的厘米数,进而确定声音往返所需的时间。在Arduino环境中,利用Trig和Echo引脚发送和接收超声波信号,根据计算的距离触发蜂鸣器报警。实验代码展示了如何在距离小于等于2cm时激活报警功能。
摘要由CSDN通过智能技术生成

在这里插入图片描述
1.首先要求测算声音的速度。可以根据温度进行计算,这样可以更精确。
例如:室内温度为28摄氏度。
声速为:331+28*0.6=347.8 m/s.
2.将声音速度转换成每微秒多少厘米。
34780 cm/S == 34.78cm /毫秒 == 0.03478 cm/微秒 == 28.752微秒/cm==287.52微秒/10cm
3.声音往返距离 28.752微秒 * 2 / cm =57.504 微秒/cm ~58 微秒/cm
.

--------------------------------------程序--------------------------------------

/超声探测实验/
const int TrigPin=3;
const int EchoPin=2;
const int buzzerPin=8;
int distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
pinMode(buzzerPin,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TrigPin,LOW);
delayMicroseconds(2);
digitalWrite(TrigPin,HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin,LOW);

distance=pulseIn(EchoPin,HIGH)/57.504;
Serial.print(distance);
Serial.println(“cm”);
if(distance<=2){
alarm();
}
delay(100);
}
void alarm(){
for(int i=1;i<=3;i++){
digitalWrite(buzzerPin,HIGH);
delay(1000);
digitalWrite(buzzerPin,LOW);
delay(1000);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值