March 9, 2011

Using an LDR (Light Dependent Resistor) with Arduino

Today we are looking on how we can use the analog input on arduino, to read a sensor. This is basic knowledge but very useful in robotic projects.

So what is an analog input? Unlike the digital input which can read two states, 0 or LOW or no voltage and 1 or HIGH or high voltage, the analog input can read up to 1024 different states which are 0,1,2,3,4,...,1022,1023. These are voltage equivalents for when you break 5V into 1024 equal "pieces" of 0.00488V each. That means that 0V is 0, 0.00488V is 1, 0.00977V is 2, ... , 4.99512V is 1023. So basically what the input is, is a scale of 0-5V in 1024 states.

Now let's see how a sensor works. Most sensors use a physical characteristic to transform the physical property that you want to measure into something that can be used in an electrical circuit.

The Light Dependent Resistor, as it name suggests is a resistor whose value changes depending an how much light it receives on it's top. The more light it gets that less resistive it becomes, i.e. more current can pass through it. But remember we said that the input on arduino can only read voltage? So how do we transfrom this resistance change into voltage change?

We use the circuit below:


We measure what the voltage is in between the LDR and the resistor. That voltage is 3.3V minus the voltage drop on the LDR. And that voltage drop is dependent on the resistance of the LDR which in turn is dependent on the light that hits this sensor.

So build this circuit in a breadboard or directly on the arduino board, and try it out.

Now the code needed for this project is very easy

/*
 * seeing_light.pde
 * -----------------
 * Reading a simple LDR
 * and printing the result.
 *
 * http://spacetinkerer.blogspot.com
 */

void setup(){
  Serial.begin(9600);
}

void loop() {
 
  delay(100);
  Serial.println(analogRead(0),DEC);
}

We use the analogRead() function to read from pin A0, then we send that value at the serial monitor.

Here is a video of this program.



So what can you build with this? If you have 2 LDRs then put one on the left and one on the right of a solar tracker and depending on where there is more light, make it turn that way, so it is always centered directly under the sun. This could be used with solar panels, for optimum output.

You can download the code here.

If you like my posts then subscribe via rss, or via e-mail to stay updated.

Happy Tinkering!

If you liked this article then take a second to bookmark it with...


2 comments:

  1. But have you tried to use the sd card reader technique to record the data.

    ReplyDelete