NodeMCU ESP8266 สัปดาห์ที่ 6

 

 ESP8266 เซ็นเซอร์วัดระยะทาง HY-SRF05 Ultrasonic


Ultrasonic Module HY-SRF05 เป็นโมดูลวัดระยะ สามารถวัดได้ตั้งแต่  2cm - 4.5m 

โค๊ด
const int pingPin = 5; //D1
int inPin = 4; //D2
int led = 16; //D0
void setup() {
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop()
{
long duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (cm < 10) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

ลิ้งhttps://www.myarduino.net/article/289/%E0%B8%AA%E0%B8%AD%E0%B8%99%E0%B9%83%E0%B8%8A%E0%B9%89%E0%B8%87%E0%B8%B2%E0%B8%99-esp8266-%E0%B9%80%E0%B8%8B%E0%B9%87%E0%B8%99%E0%B9%80%E0%B8%8B%E0%B8%AD%E0%B8%A3%E0%B9%8C%E0%B8%A7%E0%B8%B1%E0%B8%94%E0%B8%A3%E0%B8%B0%E0%B8%A2%E0%B8%B0%E0%B8%97%E0%B8%B2%E0%B8%87-hy-srf05-ultrasonic

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

หน้าที่ของesp8266