📅  最后修改于: 2020-12-27 10:01:59             🧑  作者: Mango
在Arduino超声波距离传感器的上一主题中,我们使用了四端子超声波传感器。
在这里,我们将使用三端超声波传感器,如下所示:
它具有三个端子GND(接地),5V和SIG(信号)。该传感器的过程和函数类似于超声波距离传感器。
频率波在空中传播,并在其路径上撞击物体。波浪从物体反射回并到达模块。
超声波距离传感器使用超声波对障碍物或物体进行ping操作。
它检测3 cm或4m或400 cm的范围。
这种传感器的示例是:
SEN136B5B
它是SeedStudio的传感器。我们需要在HIGH和LOW之间切换状态以注意输出。
让我们开始这个项目。
下面列出了创建项目所需的组件:
该项目的结构如下所示:
下面列出了将超声波传感器连接到板上的步骤:
考虑下面的代码:
const int pinTOping = 7;
void setup() {
Serial.begin(9600); //Serial communication at 9600 bps rate
Serial.println("Test for the Ultrasonic range sensor");
}
void loop()
{
// initialzing the variables using long data type
long TIMEduration, in, cm;
// The PING))) is triggered by a HIGH pulse valu2 of 2 or more microseconds.
// We have specified short time to clean HIGH pulse:
pinMode(pinTOping, OUTPUT);
digitalWrite(pinTOping, LOW);
delayMicroseconds(4);
digitalWrite(pinTOping, HIGH);
delayMicroseconds(8);
digitalWrite(pinTOping, LOW);
// We have used the same pin to read the signal from the PING)), which is a HIGH pulse
// the time is measured in microseconds
pinMode(pinTOping, INPUT);
TIMEduration = pulseIn(pinTOping, HIGH);
// convert the time into a distance
in = microsecondsToIn(TIMEduration);
cm = microsecondsToCm(TIMEduration);
Serial.print(in);
Serial.print("inches, ");
Serial.print(cm);
Serial.print("centimeters");
Serial.println();
delay(200); //time delay of 200 microseconds
}
long microsecondsToIn(long microseconds) {
// there are 73.746 microseconds per inch according to PING datasheet
// we need to divide the distance by 2
//It is because the ping travels forward and bounces backward
return microseconds / 74 / 2;
}
long microsecondsToCm(long microseconds) {
// The speed of sound is 340 m/s
return microseconds / 29 / 2;
}
步骤如下:
连接图如下所示:
输出量
串行监视器上的输出将显示为: