The Darker, the Brighter it is

INPUT: Photoresistor

DESCRIPTION: Control the brightness of the LED with the amount of light. The darker it is, the brighter the LED.

[code lang=”arduino” light=”true”]

int photocellPin = A6; // the pin the photocell is on
int photocellReading; // the sensor reading
int LEDpin = 5; // pin LED is on (PWM pin)
int LEDbrightness; //

void setup(void) {
// We’ll send debugging information via the Serial monitor

void loop(void) {
photocellReading = analogRead(photocellPin);

Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading – read your values and plug them into the map function
//Serial.println(LEDbrightness); // after mapping
// 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 = 30 – photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 30, 0, 255);

analogWrite(LEDpin, LEDbrightness);


Leave a Reply