Have you ever wanted to be able to activate an LED light at night but be able to deactivate it during the day? This tutorial will be your step-by-step guide on how to use a photoresistor to sense when an area is dark and turn on your LED light. This tutorial is a part of a larger project where LED strips are wired in such a way that detects a person/runner and lights up both the closest LED to the person and also detects the direction of motion in order to light up a dark running trail.
Before we dive into the wiring here are the parts you will need:
- Arduino Uno
- Jumper wires
- LED
- PIR Sensor
- LDR Photoresistor
- Resistor 1k ohm
- Resistor 100 ohm
- Breadboard
Next, we have the wring which can be viewed in the diagram below. The wiring is rather simple but you should note that the shorter side of the LED ( the cathode) should be aligned with the digital pin 3, and the longer side (the anode) should be aligned with the resistor.
One you have your parts wired, plug your Arduino to your power source. This tutorial uses an USB plugged into a computer, however you may use another power source like a battery. Open up the Arduino program and copy the code below. The code is commented out and explains how each part functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// Description: This sketch is in part wth a tutorial that will allow an LED to be activated during ambient light settings and when there is movement using a photoresistor (to detect light) and a PIR sensor (to detect directional movement). // This code was adapted from Natalia Fargasch Norman's sketch on www.TinkerHobby.com at https://www.tinkerhobby.com/motion-and-light-sensors-with-arduino-and-without/ // Before getting started you should ensure that your wiring with your parts and the arduino match the code below (you may change the pins but you then have to modidfy your code) // Some things to note: "High" indicated the LED light is on, "LOW' indicates the LED if off. Now let's get started! // First, initate the Pins. THese pins should correlate to your wiring with your part and yoru arduino. #define LDR 0 // Photoresistor to Arduino Pin A0 #define PIR 2 // PIR sensor to Arduino Pin 2 #define LED 3 // LED to Arduino Pin 3 // initate global variables or varibles that can have differnt values inside of the loop int pirState; // store value for PIR sensor int ldrValue; // store value for Photoresistor void setup() { //Serial.begin(9600); // Initialize serial communications with the PC pinMode(LED, OUTPUT); // If LED is activated wil turn on as an ouput pinMode(PIR, INPUT); // PIR sensor detects if there is motion digitalWrite(LED, LOW); // LED turns off if there is light and or no motion } void loop(){ ldrValue = analogRead(LDR); // initialize Photoresistor output //Serial.print("Analog reading = "); //Serial.println(ldrValue); if (ldrValue <= 512) { // if photoresitor detects ldr value less than or equal to 512, digitalWrite(LED, HIGH); // then the LED light will turn on. } else { // ldrValue > 512 // or if the photoresistor detects the ldr to be higher then 512, pirState = digitalRead(PIR); // if (pirState == HIGH) { // but if the PIR still detetects movement digitalWrite(LED, HIGH); // then the LED light will turn on delay(1000); // wait 1000 milieseconds digitalWrite(LED, LOW); // LED light will then be turned off delay(1000); } else { // pirState == LOW // if PIR detects no movemt digitalWrite(LED, LOW); // then LED light will turn off } } // Note from sourced code: "The processing in the Arduino occurs faster // than the response from the PIR, and adding this delay // eliminated a flickering on the LED" delay(1000); } |
Once you have uploaded your sketch, try turning the light on and off and moving in front of your arduino. The LED should turn on either when the lights are off or when there is motion. The LED should be off when there is light and where there isn’t any motion. The idea is that the light dependent resistor should activate the LED if it is dark or the PIR sensor will activate the LED if movement is detected.
Have fun!
Tutorial by Kendra Hills