Controlling an LED (2/5): Adjusting LED brightness using a Photoresistor
Overview:

In the first tutorial, the potentiometer was used to control the LED brightness. In this tutorial, the same concept is followed, however instead of a potentiometer, a photoresistor is used. This tutorial aims to give insight on what component you can use to control the LED brightness without having human intervention, such as twisting a knob. Thus, when the photoresistor detects a certain light intensity, the LED will decrease its brightness accordingly.

This type of project also mimics an ambient light sensor module.

 

Hardware:

  • Arduino Uno x 1

  • LED x 1
  • 
    
  • Photoresistor x 1
  • 
    
  • 220 ohm resistor x 1
  • 10K ohm resistor x 1
  • 
    
  • Breadboard x 1
  • 
    
    

    You can buy all this Hardware at Createlabz.

     

    Software:

     

    Application Discussion

    In the first project, in order to control LED brightness, a knob is turned. In this project, human intervention is limited with the use of a specific sensor. One sensor that is frequently being encountered is the light sensor. Light sensors can be found in consumer electronics, automobiles, and even light posts.

    This project replaces the use of a potentiometer in adjusting LED brightness with a light sensor. This concept is similar to using an ambient light sensor module. The difference between the ambient light sensor module and the photoresistor is that the former uses a more accurate measurement on the intensity of the surrounding light. Thus, making it more sensitive than the latter.

    Photoresistor

    The photoresistor is a variable resistor whose resistance vary inversely with the intensity of light. The photoresistor is not polarized, unlike the LED with legs that differ in length, the legs of the photoresistor are of equal length.

    Photoresistors are simple and low-cost devices. These are commonly used in street lights, intensity meters, alarms, and other consumer electronics.

    How it works:

    The photoresistors are also knows as photoconductive cells or photocell. This is explained by its working principle. When the intensity of the light increases, the resistance decreases. Since conductance is the reciprocal of resistance, it simply means that the conductance increases as the light intensity increases. Thus exhibiting a photoconductive behavior.

    If you want to know more about the Arduino and LED, click here.

     

    Set-up the hardware

     

    Set-up the software

    Open the Arduino IDE and upload the code on the Arduino.

    The Arduino code can be found in Github here or you can copy the code below

    
    const int LED = 9; // set pin for led
    const int LDR = A1; //set pin for photoresistor
    
    int inputval=0; //variable for values to be read from A1
    int outval =0; //variable for output value from A1
    
    void setup() {
    //Initialize pinModes
      pinMode(LED, OUTPUT);
      pinMode(LDR, INPUT);
    
    }
    
    void loop() {
        inputval = analogRead(LDR); 
        outval = map(inputval, 0, 380, 255, 0); //The 0-1023 can be changed depending on light from your setup
    
        int lightval = constrain(outval, 0,255);//the contain output values within 0-255 range
        analogWrite(LED, lightval);  
        delay (500);
    }
    

     

    Code Breakdown

    • const int var
      – This specifies that the value assigned to the variable cannot be changed, thus, making it a read only variable.

    • int var
      – Creates a variable integer.

    • pinMode(pin,mode) 
      – Configures the identified pin to behave as an input or an output.

    • analogRead
      – This can range from 0-1023 depending on the input used.

    • analogWrite
      – Has a lower range compared to analogRead and can only range from 0-255.

    • map (fromLow,fromHigh,toLow,toHigh)  
      – Used to compress a larger number to a smaller range
      – It is important to note that the fromLow and fromHigh values can be changed depending on the intensity of light from your setup.

    • constrain()
      – It is used to contain the output values within range. This precaution is done because using map() does not guarantee that the output values can remain within its “to” range.

     

    Conclusion

    With the world becoming increasingly automated, it is not a surprise for the emergence of various sensors. These sensors allow little to no human interaction for circuits to activate. Thus, this project reflects an automated version of the first project in the series.

    To find more projects you can do, check out CreateLabz Knowledgebase and Tutorials.

     

    Checkout other tutorials in the Controlling an LED series:

    Adjusting LED Brightness using a Potentiometer

    Turning on/off multiple LEDs and buzzer

    Music reactive LEDs using Sound Module

     

    Reference

    https://www.electronicshub.org/bh1750-ambient-light-sensor/

    https://www.seeedstudio.com/blog/2020/01/08/what-is-a-light-sensor-types-uses-arduino-guide/

    https://www.circuitstoday.com/photoresistor

    https://bit.ly/34xcS9m

    ArduinoArduino unoLdrLedLight dependent resistorLight sensorPhotoresistor

    Leave a comment

    All comments are moderated before being published