Introduction: Fade LED Using Light Dependent Resistor

ON/OFF LED using LDR:

/*
Using Light Dependent Resistor (LDR) to turn ON/OFF LED View activities using Serial Monitor (located at the top right side of your IDE) *

/ Analog input const int analogInput = A0; // Digital LED output pin 9 const int ledGreen = 9;

// Sensor Value int sensorVal = 0;

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

void loop() { sensorVal = analogRead(analogInput);

Serial.print("sensor = " ); Serial.print(sensorVal); Serial.print("; led = " ); if (sensorVal < 550) { Serial.println("is turn ON"); digitalWrite(ledGreen, HIGH); } else { Serial.println("is turn OFF"); digitalWrite(ledGreen, LOW); }

delay(5); }

Fading LED using LDR:

// Using LDR to Fade LED

const int analogInput = A0; const int ledGreen = 9;

int sensorVal = 0; int outputVal = 0;

void setup() { pinMode(ledGreen, OUTPUT); }

void loop() { sensorVal = analogRead(analogInput);

outputVal = 255 - (sensorVal / 4);

analogWrite(ledGreen, outputVal);

delay(2); }