W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
HC-SR04超聲波傳感器使用聲納來確定物體的距離,就像蝙蝠一樣。它提供了非常好的非接觸范圍檢測,準確度高,讀數(shù)穩(wěn)定,易于使用,尺寸從2厘米到400厘米或1英寸到13英尺不等。
其操作不受陽光或黑色材料的影響,盡管在聲學(xué)上,柔軟的材料(如布料等)可能難以檢測到。它配有超聲波發(fā)射器和接收器模塊。
你將需要以下組件:
按照電路圖進行連接,如下圖所示。
在計算機上打開Arduino IDE軟件。使用Arduino語言進行編碼控制你的電路。通過單擊“New”打開一個新的草圖文件。
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,Trigger,Echo和GND,連接如下:
在我們的程序中,我們通過串口顯示了傳感器測量的距離,單位為英寸和厘米。
你將在Arduino串口監(jiān)視器上看到傳感器測量的距離,單位為英寸和厘米。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: