Skip to content

Instantly share code, notes, and snippets.

@ViliusKraujutis
Created May 2, 2016 15:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ViliusKraujutis/bba9e5f5830275be2bcdd43e4a0f05b9 to your computer and use it in GitHub Desktop.
Attached PIR sensor to Arduino, so it could adjust power 12V ~18W LED lamp sticker-strip under kitchen's cupboards. Just a simple, although interesting project combining electronics, automation and programming.
int relayPin = 3;
int pirPin = 2;
int ledPin = 13; // just a LED on Arduino board. Not that huge ~18W LED lamp.
long lastDetection = 0; // millis
long DELAY = 3 * 1000; // N seconds (also adjustable at PIR sensor too)
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int pirValue = digitalRead(pirPin);
if (pirValue == HIGH) {
lastDetection = millis();
Serial.println("Moved: " + lastDetection);
}
if (lastDetection + DELAY > millis()) {
digitalWrite(relayPin, LOW); // Switch relay, so it would turn on LED strip
digitalWrite(ledPin, HIGH); // Also turn on indicator LED added to Arduino board
} else {
digitalWrite(relayPin, HIGH);
digitalWrite(ledPin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment