HC-SR04超聲波傳感器使用聲納來確定物體的距離,就像蝙蝠一樣。它提供了非常好的非接觸范圍檢測,準(zhǔn)確度高,讀數(shù)穩(wěn)定,易于使用,尺寸從2厘米到400厘米或1英寸到13英尺不等。
其操作不受陽光或黑色材料的影響,盡管在聲學(xué)上,柔軟的材料(如布料等)可能難以檢測到。它配有超聲波發(fā)射器和接收器模塊。
你將需要以下組件:
按照電路圖進(jìn)行連接,如下圖所示。
在計(jì)算機(jī)上打開Arduino IDE軟件。使用Arduino語言進(jìn)行編碼控制你的電路。通過單擊“New”打開一個(gè)新的草圖文件。
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; }
超聲波傳感器有四個(gè)端子:+5V,Trigger,Echo和GND,連接如下:
在我們的程序中,我們通過串口顯示了傳感器測量的距離,單位為英寸和厘米。
你將在Arduino串口監(jiān)視器上看到傳感器測量的距離,單位為英寸和厘米。
更多建議: