Automated Arduino Nightlight Using Photoresistors
by TechMartian in Circuits > Arduino
8200 Views, 14 Favorites, 0 Comments
Automated Arduino Nightlight Using Photoresistors
![2016-07-08 12.22.47.jpg](/proxy/?url=https://content.instructables.com/FO8/9T2X/J4SQF2WF/FO89T2XJ4SQF2WF.jpg&filename=2016-07-08 12.22.47.jpg)
This is the fifth module in a series of lesson for using the Arduino 101 ranging from beginner to advanced skills.
This is a hands free nightlight that will automatically turn on when it is dark.
In this project we will be using photoresistors, whose resistance values varies depending on the amount of light input, to detect the light level. When the level is below a certain calibration threshold it turns on an LED automatically.
Tools and Materials
![2016-07-08 12.17.15.jpg](/proxy/?url=https://content.instructables.com/FPE/PR1D/J4SQF2W8/FPEPR1DJ4SQF2W8.jpg&filename=2016-07-08 12.17.15.jpg)
![2016-07-08 12.17.23.jpg](/proxy/?url=https://content.instructables.com/FVQ/RFGK/J4SQF2W9/FVQRFGKJ4SQF2W9.jpg&filename=2016-07-08 12.17.23.jpg)
![2016-07-08 12.17.39.jpg](/proxy/?url=https://content.instructables.com/FUE/7FM9/J4SQF2WA/FUE7FM9J4SQF2WA.jpg&filename=2016-07-08 12.17.39.jpg)
- Arduino 101 or Arduino Uno
- Breadboard
- LED
- LDR or photoresistor
- Jumper Wires
- 100 Ω Resistor
- 10K Ω Resistor
Circuit
![2016-07-08 12.20.32.jpg](/proxy/?url=https://content.instructables.com/F0R/IF0O/J4SQF2WB/F0RIF0OJ4SQF2WB.jpg&filename=2016-07-08 12.20.32.jpg)
![Screen Shot 2017-07-10 at 12.05.55 AM.png](/proxy/?url=https://content.instructables.com/FXQ/FUYC/J4SQF59O/FXQFUYCJ4SQF59O.png&filename=Screen Shot 2017-07-10 at 12.05.55 AM.png)
![2016-07-08 12.23.13.jpg](/proxy/?url=https://content.instructables.com/F96/6YC6/J4SQF2WE/F966YC6J4SQF2WE.jpg&filename=2016-07-08 12.23.13.jpg)
Firstly, we need to wire the Arduino Power to the breadboard
- Connect the 3.3V pin of the Arduino to any pin in the red power rail of the breadboard with a red jumper wire.
- Connect the any GND pin of the Arduino to any pin in the black rail of the breadboard with a black jumper wire.
Then, we will wire the circuit for the LED.
- Connect the negative pin of the LED (flat edge) to the black ground rail of the breadboard with a brown jumper wire.
- Connect the positive pin of the LED to a 100Ω resistor and then connect in series to the red power rail of the breadboard with an orange jumper wire
And last but not least, we will connect the photoresists to the Arduino
- The photoresistor changes resistance, but an Arduino reads voltages. However, due to Ohm's law we can use a voltage divider (one of the most important basic circuits) to read this change in resistance as a change in voltage form the relationship V = IR, where I is current and R is resistance.
- Connect one of the photoresistor pins to a 10k resistor and connected in series to ground, and also connect the pin to A0 of the Arduino.
- Connect the other pin to the red power rail on the breadboard.
Coding
![Screen Shot 2017-07-10 at 12.05.15 AM.png](/proxy/?url=https://content.instructables.com/FUK/W9SD/J4SQF59K/FUKW9SDJ4SQF59K.png&filename=Screen Shot 2017-07-10 at 12.05.15 AM.png)
// As usual, we'll create constants to name the pins we're using.
// This will make it easier to follow the code below.
const int sensorPin = 0; const int ledPin = 9;
// We'll also set up some global variables for the light level a calibration value and //and a raw light value int lightCal; int lightVal;
void setup() { // We'll set up the LED pin to be an output. pinMode(ledPin, OUTPUT); lightCal = analogRead(sensorPin); //we will take a single reading from the light sensor and store it in the lightCal //variable. This will give us a prelinary value to compare against in the loop }
void loop() { //Take a reading using analogRead() on sensor pin and store it in lightVal lightVal = analogRead(sensorPin);
//if lightVal is less than our initial reading (lightCal) minus 50 it is dark and //turn pin 9 HIGH. The (-50) part of the statement sets the sensitivity. The smaller //the number the more sensitive the circuit will be to variances in light. if(lightVal < lightCal - 50) { digitalWrite(9,HIGH); }
//else, it is bright, turn pin 9 LOW else { digitalWrite(9,LOW); }
}