

>> ตัวอย่าง CODE :
int irmotionPin = 4; // Pin of IR Motion Sensor
int relayPin = 8; // Pin of Relay Module
void setup()
{
Serial.begin(9600);
pinMode(relayPin, OUTPUT); // Set Pin connected to Relay as an OUTPUT
digitalWrite(relayPin, LOW); // Set Pin to LOW to turn Relay OFF
}
void loop()
{
while (digitalRead(irmotionPin) == HIGH) { // If Motion detected
digitalWrite(relayPin, HIGH); // Turn Relay ON
Serial.println("Relay is ON");
delay(500);
}
digitalWrite(relayPin, LOW); // Turn Relay OFF
Serial.println("Relay is OFF");
delay(500);
}
อธิบายการทำงานของโปรแกรม
- เมื่อมีการเคลื่อนไหวผ่านตัวเซนเซอร์ ตัวเซนเซอร์จะส่งสัญญาณออกมาให้กับบอร์ด arduino เพื่อควบคุม LED หรือ หลอดไฟ เมื่อ สัญญาณ HIGH LED ก็จะติด แล้ว ปริ้นค่าออกทาง seriar monitor ข้อความว่า "Relay is ON"
| หน้าที่เข้าชม | 274,722 ครั้ง |
| ผู้ชมทั้งหมด | 125,241 ครั้ง |
| ร้านค้าอัพเดท | 4 ธ.ค. 2568 |