PIR Motion Sensor Switch (Arduino)
This simple motion sensor by Arduino and PIR motion sensor. For your switch lamp or other. Please adjust delay time at 5 to 300 seconds of PIR module.
Parts List
- Arduino UNO
- PIR motion Sensor
- 1 Channel 5V Relay Module
Wiring
Wiring Arduino UNO + PIR motion Sensor + Relay Module
Code
int sensorPin = 2; // Set up a PIR sensor pin
int pirState = LOW;
int val = 0;
int relayPin = 3; //Set up a Relay pin
void setup() {
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(sensorPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(relayPin, HIGH); // turn Relay ON
delay(150);
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
digitalWrite(relayPin, LOW); // turn Relay OFF
delay(150);
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
}
