≡ Menu

Light Sensor

This project will allow the photoresistor to sense how much lights there is.

The arduino can then turn the LED on if the room is dark or off if the room is lighter.

Parts List:

Note the parts needed:

2 LED

5 Wires

1 Reisistor

reisistor

 

 

 

1 Photoresistor

photoresistor

 

 

 

 

 

 

 

 

 

Wiring Diagram 1:

light_cdslitetestdiag

 

Code Example 1:

int photocellPin = 0; // the cell and 10K pulldown are connected to a0

int photocellReading; // the analog reading from the sensor divider

int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)

int LEDbrightness; //

void setup(void)

{

// We’ll send debugging information via the Serial monitor

Serial.begin(9600);

}

 

void loop(void)

{

photocellReading = analogRead(photocellPin);

 

Serial.print(“Analog reading = “);

Serial.println(photocellReading); // the raw analog reading

 

// LED gets brighter the darker it is at the sensor

// that means we have to -invert- the reading from 0-1023 back to 1023-0

photocellReading = 1023 photocellReading;

//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses

LEDbrightness = map(photocellReading, 0, 1023, 0, 255);

analogWrite(LEDpin, LEDbrightness);

 

delay(100);

}

Part B

This code doesn’t do any calculations, it just prints out what it interprets as the amount of light in a qualitative manner. For most projects, this is pretty much all that’s needed!

Wiring Diagram 2:

light_cdspulldowndiag

Code Example 2:

Connect one end of the photocell to 5V, the other end to Analog 0.

Then connect one end of a 10K resistor from Analog 0 to ground

 

For more information see http://learn.adafruit.com/photocells */

 

int photocellPin = 0; // the cell and 10K pulldown are connected to a0

int photocellReading; // the analog reading from the analog resistor divider

 

void setup(void) {

// We’ll send debugging information via the Serial monitor

Serial.begin(9600);

}

 

void loop(void) {

photocellReading = analogRead(photocellPin);

 

Serial.print(“Analog reading = “);

Serial.print(photocellReading); // the raw analog reading

 

// We’ll have a few threshholds, qualitatively determined

if (photocellReading < 10) {

Serial.println(” – Dark”);

} else if (photocellReading < 200) {

Serial.println(” – Dim”);

} else if (photocellReading < 500) {

Serial.println(” – Light”);

} else if (photocellReading < 800) {

Serial.println(” – Bright”);

} else {

Serial.println(” – Very bright”);

}

delay(1000);

}