Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Fading led with LDR(Light Dependent Resistor) using Arduino uno

By EG Projects July 22, 2020

Blinking an led is basic getting started program/project through which every newbie has to go through upon entering the field of microcontrollers(arduino, pic stm32 etc). After blinking led newly entrants try to explore more features of the particular microcontroller by making some diy(do it your self) projects. Led dimmer or intensity/brightness control of led using ldr(light dependent resistor) is another popular project after led blink which every entrant try to make. This helps to understand the ADC(analog to digital converter) functionality of a particular microcontroller. How? I am going to explain it in this tutorial. So basically in this tutorial i am going to teach you how to control led brightness using arduino uno? What microcontroller components/peripherals (ADC etc) are used to fade an led? and how to use them precisely?  

Basic led brightness control circuit with ldr and passive components

Basic led brightness/intensity control circuit using passive components is given below. This is the general circuit which every electronic engineer builds in his introduction to electronics course. A single transistor, ldr(photoresistor) and a variable resistor/potentiomemter is enough to fade an led depending on the intensity of available of light. I hope that you all know how this circuit works. 

General led dimmer or brightness control circuit using photoresistor, transistor and variable resistor

General led dimmer or brightness control circuit using photoresistor, transistor and variable resistor

Main functionality of the above traditional light dimmer circuit

  • Current passing through LDR increases/decreases depending on the amount of light thrown on the light detector. This current is input to the base of NPN transistor.
  • Varying current at input base of transistor varies the voltage across led, connected to collector side of transistor.  ​

LED brightness control using Light dependent resistor

In order to take the same functionality with arduino we need to consider the above two points as both are dependent on each other. So what we want arduino to do for us in order to accomplish the above two tasks is?

  • Determine the current/voltage allowed by light sensor to pass through it.
  • Output a voltage on its digital pin to fade led. Output voltage is dependent on the current/voltage allowed to pass by light sensor(Or intensity of light). ​

How to determine the amount of voltage allowed by LDR to pass through it using Arduino Uno?

Arduino build in ADC(Analog to digital converter) peripheral can be utilized to measure voltage. We can use arduino ADC(analog to digital) pins to measure the voltage passing the light detector.

Arduino has a 10-bit ADC module. It can measure maximum voltage of rating on which arduino is working typically 5 volts precisely. 10-bit ADC max reading value is 1024. So 1024 corresponds to 5 volts. 512 corresponds to 2.5 volts and 0 represents no voltage. Adruino uno has 6 analog channels. We can use any one of them for our project.

To read an analog voltage we use the command
Val = analogRead(analogPin);   
Where analogPin is analog channel used(A0—-A5) and Val is variable in which analog value returned by the function is saved.

So we are done with reading the light sensor allowed passing voltage which corresponds to the intensity of light.

How to fade/control brightness of led with arduino uno?

Led brightness can be varied by applying a variable voltage to it. Suppose if led max voltage requirement is 2 volts. If we supply varying voltage suppose 0.9 volts or 1.5 volts we can fade led. At 2 volts led will glow with full intensity/brightness and at 1 volt the led brightness will be half.

PWM(Pulse width modulation) is basic technique used by microcontrollers to fade or control the brightness of led. PWM actually outputs a varying voltage on digital pins of microcontroller. Varying voltage is generated using the timers of microcontrollers. I assume you people know about PWM. In case no, I am just giving a short introduction.

PWM utilizes timers and a particular pin is switched on and off for a specified period of time. On period is generally know as duty cycle we can compute the output voltage by changing the duty cycle. 100% duty cycle represents full voltage output and 50% duty cycle outputs half voltage.

PWM (Pulse width modulation) generation

PWM (Pulse width modulation) generation

PWM(Pulse width modulation) using Arduino

Arduino provides a lot of feasibility for pwm even a predefined library is present. We only need to initialize the pwm pin and output the voltage we want to that pin. Pwm functionality is present on arduino uno pins (3, 5, 6, 9, 10, or 11). PWM signal ranges between 0 to 255. Where 255 represents 5 volts and 0 represents 0 volts. 127 represents 2.5 volts. 

Arduino analogWrite(x,y) function allow us to output a pwm voltage of desired voltage. In function the x argument is the pin number to which we want our pwm signal to appear. y argument is pwm value. 

Lets start the main project. I am going to output the pwm signal on pin 9 of arduino. To read the input voltage by light sensor i am using analog channel 0 of arduino.  

In the project below I am going to read analog signal from LDR(light detecting resistor) and then write the signal corresponding pwm value to led in order to fade it. Actually writing means fading led. Analog Write outputs value in the form of PWM(Pulse width modulation). For high values PWM duty cycle will be high and for low values PWM(Pulse width modulation) duty cycle will be low.   

Fading led with ardino and light sensor – Project circuit diagram

Arduino led fading using light detection sensor-resistor

Arduino led fading using light detection sensor-resistor

Fading Led with LDR(Light Detecting Resistor) and Arduino uno.

Fading Led with LDR(Light Detecting Resistor) and Arduino uno.

Coming to the schematic of the circuit. Connect one leg of LDR(light detective resistor) with 10 k ohm resistor in series. Make the other end of resistor ground. Apply 9 volts on the other leg of the LDR(light sensor). Now take a wire and connect it in between the LDR and resistor. Connect other end to A0 analog pin 0 of the Arduino uno.

May be you people are thinking why we connect the wire in between the LDR and 10 k ohm resistor or why we use resistance here. Well their is a big logic behind it. Actually LDR normally has very high resistance and in this condition no current flows through it and when light is thrown on its surface its resistance decreases and it allows the current to flow through it. In the schematic if i directly connect the LDR with the A0 analog pin of Arduino and shine/through some light on it whole of the 9 volt will start appearing across the analog pin of arduino. Which might destroy the arduino analog channel or potentially arduino board. Since arduino operates on 5 volts so its pins can only sink 5 volts. Any voltage greater than the operational voltage(5 v) can destroy the arduino board or produces buggy output. R2 is used to lower down the voltage appearing on the arduino analog channel in its acceptable range(0-5 volts).

Using 9 volts are even dangerous you can reduce it to 5 volts. Since light dependent resistor resistance plays an important role in determining the below of R2. We need some extra resistance calculations which is hard to understand at this level. So its better to use 5 volts as input.

Connect led anode to pin# 9 of arduino and cathode to ground in series with a resistor.

Arduino led brightness control- Project Code

The code of the project is simple. First I initialized the pins that are going to be part of arduino project. Pin name sensor is for analog channel 0 of arduino and output is for arduino pwm pin#9. Then I declared the pin mode for both pins in void setup() function. Pin#9 of Arduino is declared as output pin and Pin#A0 is declared as input pin.

Note: You have a choice to declare arduino analog pin as input or not. It will work if you do not define it but you are bound to define all other pins that are not used as analog input. I did not declared it as input in setup function.

Next in the void loop function i started reading the output voltage value by light sensor from analog channel 0. The code statement int reading=analogRead(sensor) reads the sensor output voltage and place the numeric value in variable reading. 

​I divided the reading by 4. This is because arduino analog channel outputs analog value ranging from 0 to 1023.  Where as arduino PWM function analogWrite() can only output the values ranging from 0 to 255. So to brought the read value in 0 to 255 range i divided the reading by 4. Now 1023 corresponds to 255, like wise 1000 to 250 and 0 to 0.  At last I outputted the analog read value corresponding pwm signal on pin 9 using analogWrite() function.

 

See the Project Video Here…

Arduino light detector – Future Work

  • Project logic can be altered, increasing light intensity to decrease led brightness and decreasing light intensity to increase led brightness.  
  • Project can be used as light detector switch to toggle bulb on and off during night and day time.
  • Room security alarm can be made with ldr to detect if some one entered and switched on the light.
Project Code/Files

Filed Under: Arduino Projects, Microcontroller Projects

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Power Efficiency
Discover proven strategies for power conversion, wide bandgap devices, and motor control — balancing performance, cost, and sustainability across industrial, automotive, and IoT systems.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions.

  • Can anyone please help me with this single board computer?
  • Anyone successfully communicated to the BlueNRG-M0 module on the B-L4S5I-IOT01A board
  • How to Build an Audio Tone Control?
  • Current version of LTspice not working on Windows 11?
  • Addressable Latch in My Logic Project

RSS Electro-Tech-Online.com Discussions

  • Measuring controller current output with a meter
  • KiCad custom symbol definition correct approach
  • restarting this Christmas project
  • Anyone In The US Ordered From AliExpress Recently?
  • My Advanced Realistic Humanoid Robots Project

Featured Tutorials

Real Time Hardware Filter Design

  • Practical implementation of bandpass and band reject filters
    Practical implementation of bandpass and band reject filters
  • Practical application of hardware filters with real-life examples
    Practical application of hardware filters with real-life examples
  • A filter design example
    A filter design example
  • Types of filter responses
    Types of filter responses
  • What are the two types of hardware filters?
    What are the two types of hardware filters?
  • What are hardware filters and their types?
    What are hardware filters and their types?
More Tutorials >

Recent Articles

  • Posifa sensors improve low-flow accuracy in compact systems
  • Acopian releases low-profile power supplies rated to 900 W
  • Octavo Systems OSDZU-3 REF Development Platform
  • Same Sky adds enclosure design to its audio engineering capabilities
  • Waterproof SMA-to-MHF I LK assemblies introduced by Amphenol RF

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe