IAN LANG ELECTRONICS

# CIRC-09

This circuit is somewhat confusing as the schematic says put the anode of the LED to pin 13, whilst the code sets it up for pin 9. Hey-ho. Stick it to pin 13, as that way it does more of what an LDR sensor is supposed to, and don't forget to change the relevant bit in the code.

Build the circuit as described in the book, and upload the code.

The LDR (Light Dependent Resistor)

The light dependent resistor is an input transducer which reacts to brightness of light or the lack thereof.  It is made from cadmium sulphide (CdS) and resistance decreases as the brightness of light falling on the LDR increases. The most common use for the LDR is in the construction of a voltage divider for a sensor.

The symbol is given below.

"By day conducts, by night obstructs".

Go Back

This is a fairly easy construct but watch your junctions, it's easy to misplace a lead somewhere.

Once again the sensor is made of a voltage divider, this time consisting of an LDR and a fixed resistor. The LDR, as brightness increases, can go down to 200 ohms or so and as it gets darker, up into the millions of ohms. So, if it's 200 ohms in brightness, and the other ones 10 k

Vout= (200/10200)Vs   = =0.098V, or 90 mV which is digital value 20. That's not enough to make the LED come on. When it gets dark, lets say 30k  (30/40)5 is 3.75V, or 3750 mV or digital step 765, which is. We can do the same without  using maps by just accessing the digital steps directly. Move the jump wire to pin 9 and upload the following:

int (voltvalue);

void setup ( ){

}

void loop (){

if (voltvalue>613){

digitalWrite (9,HIGH);

}

else {

digitalWrite (9,LOW);

}

}

The LED will stay unlit until you put your finger over the LDR or, if you are working in a lighted room, switch off the lights. Then the LED will light. This is known as a dark activated sensor. The key is in this line:

if (voltvalue>613){

It's a conditional obviously, and the argument is the variable voltvalue being of greater value than 613. Remember that variable gets its value from reading the input at A0 which in turn is fed by the voltage present at the junction of the voltage divider made by the LDR and the fixed resistor. Consequently as the light rises and falls the voltage goes up and down, and we've set it so the LED comes on when it's roughly 3V or above. How?  Remember how I said that an analogue input is made of parcels each one having 4.89mV?  Well, 3000/4.89 = 613.5.

If you want to go the other way, simply change the line to read

if (voltvalue<613){

Here's the modified code:

int (voltvalue);

void setup ( ){

}

void loop (){

if (voltvalue<613){

digitalWrite (9,HIGH);

}

else {

digitalWrite (9,LOW);

}

}

This time the LED will light and go off when you cover up the LDR.

If you want a fady, try this. upload the code and hover your finger an inch or so over the LDR :

int (voltvalue);

void setup ( ){

}

void loop (){

voltvalue=map (voltvalue, 0,400,0,255);

analogWrite (9,voltvalue);

}

We can set the light to some predetermined level using a potentiometer. Place a pot on your breadboard, outside terminals connected to 5V and GND and wiper (middle terminal) to pin A5 of your Arduino board.

int (voltvalue);

int (refvalue);

void setup ( ){

}

void loop (){

if (voltvalue>refvalue){

digitalWrite (9,HIGH);

}

else {

digitalWrite (9,LOW);

}

}

Throttle the pot until the light just comes on, and throttle it back gently until the light goes off again. You'll find that it doesn't need to be as dark as it did to switch the light on. This sort of mechanism is used to adjust street lamps.

Ok, let's get rid of the pot and set two buttons up to pull two pins low. Set them up like this:

int (voltvalue);

int (dastate);

void setup ( ){

dastate=1;

}

void loop (){

dastate=1;}

dastate=0;}

if (dastate ==1){

if (voltvalue>613){

digitalWrite (9,HIGH);

}

else {

digitalWrite (9,LOW);

}

}

if (dastate ==0){

if (voltvalue<613){

digitalWrite (9,HIGH);

}

else {

digitalWrite (9,LOW);

}

}

}

Assuming all is well, here's what should happen.The LED should be off.

Put your finger over the LDR. The LED should come on.

Press the button you've attached to pin 2 of the Arduino board. Nothing sould have changed.

Put your finger over the LDR and the LED should come on.

Press the button you've attached to pin 3 of the board. The LED should come on.

Put your finger over the LDR. The LED should go off.

Press the button you have attached to pin 3. Nothing should change.

Press the button you have attached to pin 2. You should now be back to the original state.

Did it all work? Yes? Congratulations! You've made a switchable light sensor!

Light sensors are just about the sole use for the LDR. Here we have made a dark sensor, a light sensor, a calibratable sensor and a switchable one. That about wraps it up for light sensors. The next and penultimate lesson is about temperature. It's slightly harder.

5V

5V

GND

GND

pin 2

pin 3

CIRC 10 Copy & Paste Code from Here: