Arduino code:
const int LEDPin = 3; const int PIRPin = 2; // input pin for the PIR sensor int PIRstate; // variable to hold the last PIR state int val; // variable for reading the pin status void setup() { pinMode(PIRPin, INPUT); //declare PIR as input PIRstate = digitalRead(PIRPin); //assign PIR state to PIRstate } void loop() { val = digitalRead(PIRPin); // read input value if (val != PIRstate) // check if the input has changed { if(val == HIGH){ analogWrite(LEDPin, 255); } else{ for(int fadeValue = 255 ; fadeValue= 0; fadeValue -=5) { // sets the value (range from 0 to 255): analogWrite(LEDPin, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } } } PIRstate = val; // save the new state to the variable }
About the PIR sensor I used:
Leave a comment