ocrobot:kit:arduino_star_kit:tutorial016
差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版 | |||
ocrobot:kit:arduino_star_kit:tutorial016 [2017/10/04 07:55] – 弘毅 | ocrobot:kit:arduino_star_kit:tutorial016 [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ======超声波传感器测距====== | ||
+ | <WRAP center round info 100%> | ||
+ | 这个例程展示如何使用SR04超声波模块来进行距离测量。 | ||
+ | </ | ||
+ | 科学家们将每秒钟振动的次数称为声音的频率,它的单位是赫兹(Hz)。我们人类耳朵能听到的声波频率为20Hz~20000Hz。当声波的振动频率小于20Hz或大于 20000Hz时,我们便听不见了。因此,我们把频率高于20000赫兹的声波称为“超声波”。由于超声波指向性强,能量消耗缓慢,在介质中传播的距离较远,因而超声波经常用于距离的测量。 | ||
+ | |||
+ | 超声波测距原理: | ||
+ | 超声波发射器向某一方向发射超声波,在发射时刻的同时开始计时,超声波在空气中传播,途中碰到障碍物就立即返回来,超声波接收器收到反射波就立即停止计时。超声波在空气中的传播速度为340m/ | ||
+ | 超声波测距的原理是利用超声波在空气中的传播速度为已知,测量声波在发射后遇到障碍物反射回来的时间,根据发射和接收的时间差计算出发射点到障碍物的实际距离。由此可见,超声波测距原理与雷达原理是一样的。 | ||
+ | |||
+ | 测距的公式表示为:L=C×T | ||
+ | |||
+ | 式中L为测量的距离长度;C为超声波在空气中的传播速度;T为测量距离传播的时间差(T为发射到接收时间数值的一半)。 | ||
+ | |||
+ | {{: | ||
+ | |||
+ | =====搭建电路===== | ||
+ | {{: | ||
+ | =====代码===== | ||
+ | <code cpp> | ||
+ | const int TrigPin = 8; | ||
+ | const int EchoPin = 9; | ||
+ | |||
+ | float cm; | ||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | pinMode(TrigPin, | ||
+ | pinMode(EchoPin, | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | digitalWrite(TrigPin, | ||
+ | delayMicroseconds(2); | ||
+ | digitalWrite(TrigPin, | ||
+ | delayMicroseconds(10); | ||
+ | digitalWrite(TrigPin, | ||
+ | |||
+ | cm = pulseIn(EchoPin, | ||
+ | Serial.print(cm); | ||
+ | Serial.print(" | ||
+ | Serial.println(); | ||
+ | delay(1000); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
+ | {{youku> | ||
+ | |||
+ | [[ocrobot: |