Fade LED Using Light Dependent Resistor


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); }

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Be the First to Share


    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest



    2 years ago

    Hello there!

    We're glad you want to share something with the Instructables community! And we’re here to help you out. In order to be published live on the site, Instructables must consist of the following things:

    - Multiple steps showing how you made your project

    - Written instructions in each step

    - Your own original images

    Beyond making your Instructable simply publishable, this guide (https://www.instructables.com/id/How-to-Create-a-Feature-Worthy-Instructable/) explains what is required to have your Instructables featured by our site editors. It’s very helpful and definitely worth checking out.

    We would love to review your project again after you have made the necessary edits, and we will publish your project if it is eligible.If you have any questions, please feel free to ask right here or send us an email at service@instructables.com.


    Instructables Community Manager