Arduino HC-SR04
18 May 2013HC-SR04是一个超声波测距模块
接线
table(table table-bordered). |_.HC-SR04|_.Arduino| |VCC|5v| |GND|GND| |Trig|PIN8| |Echo|PIN9|程序
const int TP = 8; const int EP = 9; void setup() { pinMode(TP, OUTPUT); pinMode(EP, INPUT); Serial.begin(9600); } void loop() { digitalWrite(TP, LOW); delayMicroseconds(2); digitalWrite(TP, HIGH); delayMicroseconds(10); digitalWrite(TP, LOW); long microseconds = pulseIn(EP, HIGH); Serial.print("ret="); Serial.println(microseconds); long distacne = microseconds / 2 / 29.1; // 计算距离公式 Serial.print("distacne="); Serial.print(distacne); Serial.println("cm"); delay(1000); }
参考: http://www.elecfreaks.com/244.html
素材: https://github.com/rngadam/ART/tree/master/ele/fritzing/HC-SR04