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

Supplies:

Share

    Recommendations

    • Arduino Contest 2019

      Arduino Contest 2019
    • Party Challenge

      Party Challenge
    • Fandom Contest

      Fandom Contest

    Discussions

    0
    None
    Swansong

    1 year 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.

    Best,

    Instructables Community Manager