## Intensity control of an LED using LDR and Arduino board

This is another simple project that you might want to try out using your Arduino board.

So what is exactly our goal here?

As the title says, we are going to control the intensity of an LED(Light Emitting Diode) using an LDR (Light Dependent Resistor).
But more importantly we are going to look at how we take in an analog input, make sense of that input, and output what appears to be analog signal ( …but it ‘s not !!..its actually PWM) using  an Arduino board.

Some Theory

Okay it might be possible that there were a few terms in the above paragraph that might be new to you. In case you have completely understood it, you may skip ahead but in case you haven’t this might clear it up a bit.

LDR:

It stands for “Light Dependent Resistor”. It ‘s a type of resistor whose value changes with intensity or the amount of light falling on it. As the amount of light falling on it increases the its resistance decreases and the other way round. Understanding why it happens will require going into the depths of the material with which it is made and this isn’t really an article for that. However there are many articles which will solve the question but for now you only need to remember that it ‘s resistance value changes with the amount of light falling on it.

Analog to Digital Converter:

It takes in an analog value( in this case the voltage across the LDR) and converts it to a 10 bit digital. Since we have 10 bits, the output of the ADC ranges from 0-1023 (i.e. 1024 values 2^10).  Every voltage between 0 – 5V is mapped to a value between 0-1023. Why till 1023? The reason is the resolution of our ADC. Here we represent every value with a combination of 10 bits. As the number of bits used to represent a value increases, the output value gets much more accurate. The following figure gives an idea of what happens in an ADC using 2 bit resolution.

Value         Bit Representation

0 – 1.25             00

1.25-2.5            01

2.5-3.75            10

3.75-5                11

PWM:

Pulse Width Modulation is a technique in which the width of the pulses can be varied according to the message signal. In this case the led is connected to a PWM output pin of the arduino board and using the analogWrite() function we make the width of the pulses on the pin vary. Varying the width of the pulses can be seen as varying the average voltage on the pin. So although we are only changing the amount of time for which the led is on or off, due to the persistence of vision it is as if we are varying the voltage across the led which causes it to fade and brighten up.

How do we get there?

Okay so lets get started. We are going to require the following items:

• Resistors
• Light Emitting Diode
• Light Dependent Resistor
• Arduino board
• Connecting wires

The connections are as follows:

• Connect one end of a resistor ( I have a 1k one…you can use any other value if you prefer.. just make sure it is not too high or too low) to the 5V pin of the arduino board. Connect the other end of the resistor to one end of the LDR. The other end of the LDR should be connected to the ground (GND).
• Take a wire from the junction of the resistor & the LDR connected earlier and connect it to the Analog Input 5 on the arduino board.
• Next take a wire from any of the PWM pins on your board and connect it to the Anode of the LED. Connect the cathode of the LED to one end of a resistor with the other end connected to the GND.

Those are all the connections we have to make.

Next we need to write a program to make it work.

The program is quite self explanatory

Some things to note are

• When I uploaded the program to the board I found that the value in the variable “value” varied over a range of 990 – 1010. This is a very small range in comparison to 0 – 1023. So I constrained it between 990 – 1010 before mapping it to 0 – 255. If I had not done this  the value written to the ledPin would have varied over a much smaller range and this would not have been noticeable in the varying brightness of the led.
• I have also put in the Serial.println() function. It ‘s a good idea to use some of these when you want to check the values being read or written. Helps in debugging.

The circuit in sufficient light

Now when I cover the circuit the led begins to glow. Its brightness increases as I continue enclosing the LDR more.Until the point..

In the dark

So…there you go controlling the intensity of the LED using an LDR.

You can check out the video here: Here ‘s the Video

Credits google for the first two pics.