標(biāo)題: Arduino Uno+超聲波傳感器測距源碼與接線 [打印本頁]

作者: 18401669124    時間: 2018-4-16 18:33
標(biāo)題: Arduino Uno+超聲波傳感器測距源碼與接線
接線圖:


Arduino Uno超聲波傳感器測距源程序如下:
  1. const int TrigPin =2;
  2. const int EchoPin =3;
  3. float cm;
  4. void setup(){
  5.   Serial.begin(9600);
  6.   pinMode(TrigPin, OUTPUT);
  7.   pinMode(EchoPin, INPUT);
  8.   }
  9. void loop(){
  10.   digitalWrite(TrigPin, LOW); //低高低電平發(fā)一個短時間脈沖去TrigPin
  11.   delayMicroseconds(2);
  12.   digitalWrite(TrigPin, HIGH);
  13.   delayMicroseconds(10);
  14.   digitalWrite(TrigPin, LOW);
  15. cm = pulseIn(EchoPin, HIGH) / 58; //將回波時間換算成cm
  16. cm = (int(cm * 100)) / 100; //保留兩位小數(shù)
  17. Serial.print(cm);
  18. Serial.print("cm");//串口輸出
  19. Serial.println(); delay(1000);
  20. }
復(fù)制代碼

包括接線圖片和代碼?梢灾苯邮褂谩

作者: 逐飛    時間: 2018-4-16 22:06
謝謝分享
作者: 813547577    時間: 2018-4-18 14:20
只接一個超聲波傳感器有什么用?
作者: antking    時間: 2021-12-22 13:25
感謝分享,我記得有個示列呀
作者: nhtwins    時間: 2021-12-29 11:49

感謝分享,我記得有個示列呀




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1