HC-SR04超声波传感器使用声纳来确定物体的距离,就像蝙蝠一样。 它提供了优异的非接触范围检测,具有高精度和稳定的读数,易于使用的包装,从2厘米到400厘米或1“到13英尺。
该操作不受阳光或黑色材料的影响,尽管在声学上,软的材料如布可能难以检测。 它配有超声波发射器和接收器模块。


技术规格
电源 - + 5V DC
静态电流 - <2mA
工作电流 - 15mA
有效角度 - <15°
测距距离 - 2厘米 - 400厘米/ 1“ - 13英尺
分辨率 - 0.3厘米
测量角度 - 30度
静态电流 - <2mA
工作电流 - 15mA
有效角度 - <15°
测距距离 - 2厘米 - 400厘米/ 1“ - 13英尺
分辨率 - 0.3厘米
测量角度 - 30度
必需的组件
您将需要以下组件 -
- 1 × Breadboard 面包板
- 1 × Arduino Uno R3
- 1 × ULTRASONIC传感器(HC-SR04)
程序
按照电路图进行连接,如下图所示。

草图
在计算机上打开Arduino IDE软件。 在Arduino语言编码将控制你的电路。 通过单击新建打开一个新的草图文件。

Arduino代码
- const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
- const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
- void setup() {
- Serial.begin(9600); // Starting Serial Terminal
- }
- 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连接如下 -
- 将+ 5V引脚连接到Arduino板上的+ 5v。
- 将触发器连接到Arduino板上的数字引脚7。
- 将Echo连接到Arduino板上的数字引脚6。
- 在Arduino上将GND连接到GND。
在我们的程序中,我们通过串口显示传感器测量的距离,单位为英寸和厘米。
结果
您将在Arduino串行监视器上看到传感器测量的距离,单位为英寸和厘米。
转载本站内容时,请务必注明来自W3xue,违者必究。