Automatic Lighting – Well, almost ;-)

Creating a simple connection between an LED and a light sensitive resistor.

In this tutorial, I’ll use two inexpensive components (a LED and a Light Resistor) and create two circuits which will work together thanks to an Arduino.

You will need:

MAKING THE CONNECTIONS

Blog-4-Fritzing

 

Circuit one: LED

Attached one cable to Pin 9 on the Arduino and the other end to the breadboard.

Attach a second cable to a ground pin on the Arduino (marked GND) and the other end to the breadboard as shown above.

Insert a 220 ohm resistor into the breadboard between the pin 9 and GND connections.

Insert a LED but make sure that the short leg of your LED is orientated so that the short leg is closest to the resistor. This completes the first circuit.

Pin 9 will be given an output, an action, which will travel through the resistor, into the LED and the circuit will be completed by the attached GND jumper cable. The addition of a resistor is required to avoid damaging the LED and more importantly, your Arduino!

Circuit two: LDR

Attach a cable from the 5V pin to the breadboard.

Insert a 10K resistor.

Insert the LDR (Light Resistor) (doesn’t matter which way around).

now attach a cable to close the circuit on the breadboard and into a ground (GND) pin on the Arduino.

The circuit is complete. However, as we want to read the value at the point where the two resistors meet, we need to attach a third cable. This needs to be placed in the column the two components share, and the other end needs to go to the pin set in the sketch. In this case that’s Ao – We’re using analogue pins for this part because we want to get a variable reading from the components. Digital only allows you to read high or low (on or off), whereas analogue will read a variable value.

The Sketch

/* FILE: PhotoResistor_LED
 DATE: 16/01/15

REVISIONS:

19/09/13 Created version 0.1

This is an example of how to use the LXD5537 photoresistor and 5mm LED.

This sketch assumes that you have connected a 10K resistor in series 
with the sensor. The connection should be as follows:

Arduino +5V -> Photoresistor -> 10K resistor -> Arduino GND
                               ^
                               |
                               Arduino Pin A0 -------------


The sketch reads the light level via the Arduino's analogue
pin A0 and turns on the LED when a reading higher than 512 is read.
Expected ADC values are around 5 for total darkness to around 570 
for normal daylight.

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


/* Define the analogue pin used to read the photoresitor */
#define LDRPin 0
#define LEDPin 9

void setup()
{
 Serial.begin(9600); 
 pinMode(LEDPin, OUTPUT);     
 
}

/* Main Program */
void loop()
{
  int LDRValue;
 
  LDRValue = analogRead(LDRPin);
  Serial.print("Photoresistor: ");
  Serial.println(LDRValue);
/* Wait 1 second before reading again */
 //delay(1000);

/* If level output by Photoresistor is less than -- */
  if (LDRValue >= 512)
  {
    /* Then turn on the LED attached to pin 9 */
    digitalWrite( LEDPin , HIGH );
  }else
  {
    /* Turn off LED attached to pin 9 */
    digitalWrite( LEDPin , LOW );
  }
}

Add a Comment

Your email address will not be published.