Basic led brightness control circuit with ldr and passive components
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) 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
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.
Filed Under: Arduino Projects, Microcontroller Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.