HC-SR04超声波传感器像蝙蝠一样,使用SONAR来确定物体的距离。它采用2厘米至400厘米或1英寸至13英尺的易于使用的包装,提供了卓越的非接触范围检测,具有高精度和稳定的读数。
该操作不受阳光或黑色Materials的影响,尽管在听觉上,柔软的Materials(如布)可能很难检测到,它带有超声波发射器和接收模块。
您将需要以下组件-
按照电路图进行连接,如下图所示。
const int pingPin=7; //超声波传感器触发引脚 const int echoPin=6; //超声波传感器的回波引脚 void setup() { Serial.begin(9600); //启动串行终端 } void loop() { long duration, inches, cm; pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW); pinMode(echoPin, INPUT); duration=pulseIn(echoPin, HIGH); inches=microsecondsToInches(duration); cm=microsecondsToCentimeters(duration); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); delay(100); } long microsecondsToInches(long microseconds) { return microseconds/74/2; } long microsecondsToCentimeters(long microseconds) { return microseconds/29/2; }
超声波传感器具有如下连接的四个端子-+ 5V,触发器,回波和GND-
在我们的程序中,我们已经通过串行端口显示了传感器测量的距离,以英寸和厘米为单位。
您将在Arduino串行监视器上看到以英寸和厘米为单位的传感器测得的距离。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)