超音波距離感測器是利用聲波量測距離,其工作原理跟海底聲納、車輛倒車感測等相同。
感測器依結構又分會收發一體式與收發分體式,而在智能小車大多採用收發分體式,操作上較簡單。
收發分體式由發射端發射脈衝訊號,經由碰撞物體反射至接收端,因此從發射到接收到訊號的傳遞時間就以可計算出距離。
另外因收發分體式會受角度。
Arduino Libary: NewPing
範例程式: NewPingExample
函式說明
#define TRIGGER_PIN 11 訊號發射Pin
#define ECHO_PIN 12 訊號接收Pin
黃色為Tigger pin,發送1個脈衝的觸發訊號給感測器,超音波感測器發出訊號後會將ECHO訊號拉至High準位,當接收端偵測到回傳的訊號會將ECHO拉至Low,
因此ECHO從High至Low的時間為傳遞時間(藍色波形),經換算為 (0.780us/2)*340.29m=13.3cm。
程式碼
#include#define TRIGGER_PIN 11 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. void setup() { Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. } void loop() { delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. Serial.print("Ping: "); Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range) Serial.println("cm"); }
全站熱搜
留言列表