5 leds - 5 ldrs

hi there

i am trying to control 5 leds individually by using some ldrs.

i need about 5 leds to be controlled individually.

for example ldr on analog 0 to control led on pin 9
ldr on analog 1 to control led on pin 6
etc etc

how can i build a circuit that has an ldr controlling only one led?

i have tried several connections and codes but none seems to work.

any help please????

many thanks in advance!!!

The usual problem with LDRs is that people don't know how to wire them. They must be used as part of a voltage divider with the centre going to the analogue input. A 10k resistor usually works OK as the lower leg with most LDRs IE

5v-LDR-analogue pin-10k-gnd

This gives a nice range on the input pin and then its a simple matter to interpret these in your sketch and make the LED do whatever you want. You'll also need a resistor on the LED to stop it frying the arduino and/or the LED. (300r - 1k is usually OK for most LEDs)

i have my led and ledr connected exactly as in this example

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1270730797

i need to put another 4 leds and ldrs. but like i said in the earlier message i want each led to be control individually!

i need help with the circuit schematic as well as with the code. could you please help?

many thanks!!!!

Have you got the single one working ?. If not then theres little point in going on until you have that one working. The schematic is easy you just duplicate the LDR circuit on 5 different analogue pins and duplicate the LED circuit on 5 different digital pins.

LDR
5v-LDR-a pin-10k-GND

LED
Di Pin - LED - 1k - Gnd.

You pretty much just duplicate the code as well, but use different variable names for each analogue reading and different output pins for each one.

yes i have that one working :slight_smile:
and i also replicated the circuit and added only 3 leds and 3ldrs just to test before adding more.

Ok at the moment I have 2 leds on 9-6 and 2 ldrs on analog pins 0-1

The problem I am finding is :

The first ldr on analog0 it stops working, once I add a second ldr and a second led.

when I put the second ldr on on analog1 the leds only go on when I cover the 2nd ldr. Nothing happens when cover the 1st ldr on analog0.

Also I don't want all leds to go on. I want each ldr to control a led. At the moment it seems that each ldr controls all leds. If ofcourse all ldrs are working. Like I said above. The first ldr was fine but when I added the second one then the 1st stoped working.

Below is how I have the code running.

Thanks !!!

/* Photocell sketch that lights up 6 leds when blocking the light */

int photocellPin0 = 0; // the cell and 10K pulldown are connected to a0

int photocellReading0; // the analog reading from the sensor divider

int photocellPin1 = 1; // the cell and 10K pulldown are connected to a0

int photocellReading1; // the analog reading from the sensor divider

int photocellPin2 = 2; // the cell and 10K pulldown are connected to a0

int photocellReading2; // the analog reading from the sensor divider

// More POWAH, more leds!

int ledPins[] = {3, 5, 6, 9, 10, 11};

int LEDbrightness;

void setup(void) {

// We'll send debugging information via the Serial monitor

// Serial.begin(9600);

}

void loop(void) {

// Read analog pin

photocellReading0 = analogRead(photocellPin0);

photocellReading1 = analogRead(photocellPin1);

photocellReading2 = analogRead(photocellPin2);

// Debugging only

// Serial.print("Analog reading = ");

// LED gets brighter the darker it is at the sensor

photocellReading0 = 1023 - photocellReading0;

photocellReading1 = 1023 - photocellReading1;

photocellReading2 = 1023 - photocellReading2;

// Debugging only

//Serial.println(photocellReading);

//Ledpin9-analog0

// If statement to turn of all the leds when below a "normal" reading

if (photocellReading0 < 850)

{

for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)

analogWrite(ledPins*, 0);*
}

  • // Or else! Or else what? Exactly, turn on the leds*
  • else*
    {
  • //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses*
  • LEDbrightness = map(photocellReading0, 851, 1023, 0, 255);*
  • // More POWAH, turn them all on*
    for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)
    _ analogWrite(ledPins*, LEDbrightness);_
    _
    }*_

//ledpin6-analog1
// If statement to turn of all the leds when below a "normal" reading
if (photocellReading1 < 850)
{
for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)
_ analogWrite(ledPins*, 0);
}
// Or else! Or else what? Exactly, turn on the leds*
* else*
{
* //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses*
* LEDbrightness = map(photocellReading1, 851, 1023, 0, 255);
// More POWAH, turn them all on*
for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)
analogWrite(ledPins*, LEDbrightness);
}*_

//ledpin5-analog2
// If statement to turn of all the leds when below a "normal" reading
if (photocellReading2 < 850)
{
for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)
_ analogWrite(ledPins*, 0);
}
// Or else! Or else what? Exactly, turn on the leds*

* else*
{
* //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses*
* LEDbrightness = map(photocellReading2, 851, 1023, 0, 255);
// More POWAH, turn them all on*

for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)
analogWrite(ledPins*, LEDbrightness);
}*_

// Debugging only
// Serial.print("Brightness = ");
// Serial.println(LEDbrightness);

* delay(100);*
}

for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)

           analogWrite(ledPins[i],  0);

}

Will set all LEDs (i.e. whatever is in yor ledPins array) to the value 0, and then later to LEDbrightness...

Instead you want to:

if (photocellReading0 < 850) analogWrite(ledPins[0],  0);
else analogWrite(ledPins[0],  map(photocellReading0, 851, 1023, 0, 255));

if (photocellReading1 < 850) analogWrite(ledPins[1],  0);
else analogWrite(ledPins[1],  map(photocellReading1, 851, 1023, 0, 255));

and so on...

Of course, you should actually put your inputs into an array:

//warning, ugly code!
int[3] photocellReading;
photocellReading[0] = analogRead(photocellPin0);
photocellReading[1] = analogRead(photocellPin1);
photocellReading[2] = analogRead(photocellPin2);

And then create a function:

void updateLED(int led) {
  if (photocellReading[led] < 850) analogWrite(ledPins[led],  0);
  else analogWrite(ledPins[led],  map(photocellReading[led], 851, 1023, 0, 255));
}

This is all assuming your 'map'ping is correct.

I've only specified 2 LEDs here, you can work out getting the third in :slight_smile:

thanks for this :slight_smile:
could you please provide me with a schematic diagram , to make sure that everything is in the right place?

thank you very much!!!

See reply #3

how can i upload an image in a post ?

Upload to a server like image shack. Use the image include icon at top with link inserted in it.

ok so i have my connections exactly like in the image .

and i now have this code running

/* Photocell sketch that lights up 6 leds when blocking the light */

int photocellPin0 = 0; // the cell and 10K pulldown are connected to a0
int photocellReading0; // the analog reading from the sensor divider
int photocellPin1 = 1; // the cell and 10K pulldown are connected to a0
int photocellReading1; // the analog reading from the sensor divider
int photocellPin2 = 2; // the cell and 10K pulldown are connected to a0
int photocellReading2; // the analog reading from the sensor divider
// More POWAH, more leds!
int ledPins[] = {3, 5, 6, 9, 10, 11};
int LEDbrightness;

void setup(void) {
// We'll send debugging information via the Serial monitor
// Serial.begin(9600);
}

void loop() {

analogWrite(3, 255-analogRead(0)/4);

analogWrite(5, 255-analogRead(5)/4);

analogWrite(6, 255-analogRead(4)/4);

analogWrite(9, 255-analogRead(3)/4);

analogWrite(10, 255-analogRead(2)/4);

analogWrite(11, 255-analogRead(1)/4);

}

what happens is that all leds are on and none of the ldrs are responding.

thanks for the help:)

heres the image. it seems it didnt load in the previous post

6 analogWrites for 3 LEDs? (As per the diagram) ... I don't understand?

Meanwhile, instead of directly trying to control the LEDs... save to an INT and then also Serial.print(val); to see what's actually returning.

i.e.

int sensor = 255-analogRead(1)/4;
Serial.println(sensor);
analogWrite(9, sensor);

And uncomment your Serial.begin(9600);

Meanwhile... I take it this is a school assignment? There seems to be a flood of the same requests around.

im writing some code, but i doubt it'll work...

here's what i wrote... ic ant get the last part to work tho...

int photoPin1 = 0;
int photoPin2 = 1;
int photoPin3 = 2;
int photoPin4 = 3;
int photoPin5 = 4;
int ledPin1 = 11;
int ledPin2 = 10;
int ledPin3 = 9;
int ledPin4 = 6;
int ledPin5 = 5;
int LEDbrightness1;
int LEDbrightness2;
int LEDbrightness3;
int LEDbrightness4;
int LEDbrightness5;
int val1;
int val2;
int val3;
int val4;
int val5;

void setup() {
pinMode (ledPin1, OUTPUT);
pinMode (ledPin2, OUTPUT);
pinMode (ledPin3, OUTPUT);
pinMode (ledPin4, OUTPUT);
pinMode (ledPin5, OUTPUT);
}

void loop() {
{  
  val1 = analogRead(photoPin1);
  val2 = analogRead(photoPin2);
  val3 = analogRead(photoPin3);
  val4 = analogRead(photoPin4);
  val5 = analogRead(photoPin5);
  val1 = map(val1, 0, 1023, 0, 225);
  val2 = map(val2, 0, 1023, 0, 225);
  val3 = map(val3, 0, 1023, 0, 225);
  val4 = map(val4, 0, 1023, 0, 225);
  val5 = map(val5, 0, 1023, 0, 225);
}

{
  for(int LEDbrightness1 =val1);
  analogWrite(ledPin1, LEDbrightness1); 
  
  for(int LEDbrightness2 =val2);
  analogWrite(ledPin2, LEDbrightness2); 
  
  for(int LEDbrightness3 =val3);
  analogWrite(ledPin3, LEDbrightness3);   
  
  for(int LEDbrightness4 =val4);
  analogWrite(ledPin4, LEDbrightness4);
 
  for(int LEDbrightness5 =val5);
  analogWrite(ledPin5, LEDbrightness5); 
 
  delay(25);
}
}

I've never seen C code like that... it really doesn't make sense...
Meanwhile, SERIAL DEBUGGING IS YOUR FRIEND... You can then SEE the values you are playing with and have an idea what's happening.

Simplify to:

int photoPin1 = 0;
int photoPin2 = 1;
int photoPin3 = 2;
int photoPin4 = 3;
int photoPin5 = 4;
int ledPin1 = 11;
int ledPin2 = 10;
int ledPin3 = 9;
int ledPin4 = 6;
int ledPin5 = 5;
int val1;
int val2;
int val3;
int val4;
int val5;

void setup() {
  Serial.begin(9600);
  pinMode (ledPin1, OUTPUT);
  pinMode (ledPin2, OUTPUT);
  pinMode (ledPin3, OUTPUT);
  pinMode (ledPin4, OUTPUT);
  pinMode (ledPin5, OUTPUT);
}

void loop() {
{
  val1 = analogRead(photoPin1);
  val2 = analogRead(photoPin2);
  val3 = analogRead(photoPin3);
  val4 = analogRead(photoPin4);
  val5 = analogRead(photoPin5);

  Serial.print("Before map: {");
  Serial.print(val1);
  Serial.print(",");
  Serial.print(val2);
  Serial.print(",");
  Serial.print(val3);
  Serial.print(",");
  Serial.print(val4);
  Serial.print(",");
  Serial.print(val5);
  Serial.println("}");

  val1 = map(val1, 0, 1023, 0, 225);
  val2 = map(val2, 0, 1023, 0, 225);
  val3 = map(val3, 0, 1023, 0, 225);
  val4 = map(val4, 0, 1023, 0, 225);
  val5 = map(val5, 0, 1023, 0, 225);

  Serial.print("After map: {");
  Serial.print(val1);
  Serial.print(",");
  Serial.print(val2);
  Serial.print(",");
  Serial.print(val3);
  Serial.print(",");
  Serial.print(val4);
  Serial.print(",");
  Serial.print(val5);
  Serial.println("}");

  analogWrite(ledPin1, val1);
  analogWrite(ledPin2, val1);
  analogWrite(ledPin3, val3);
  analogWrite(ledPin4, val4);
  analogWrite(ledPin5, val5);

  delay(1000);
}

Run this code and then tell us what your Serial debug console outputs!?
If you haven't used the Serial console yet, then GOOGLE it or search on the Arduino Playground.

hi
thank you all for your responses but unfortunately i didnt manage to succeed in what i want to do.

just a reminder i want to use 3-4 ldrs to light up 3-4 respectively.
meaning that each ldr acts like a stepping stone to light up a led.
for example ldr1--->led1 / ldr2--->led2 / ldr3--->led3 etc etc

i have run the code above but only one led is lit, the led on digital pin 9 and connected to analog pin 3.

note that on my circuit i have led digital pin 9 on analog 3, led digital pin 10 on analog2 and led digital pin 11 on analog 1

running the above code only led on diital pin 9 is lit, and there is no interaction with the ldr whatsoever.

i think i have explained clear what i want to do but i am not succeeding. i dont think that is that complicated, i just dont know what to do....:frowning: i dont know if it s the schematic, which is exactly like this one ImageShack - Best place for all of your image hosting and image sharing needs or is the code.

@ StevenH
thanks for your suggestion but i can get it to work or so i think...

am all new in all these things as you may all have realised :slight_smile:

i have downloaded the console from Console download | SourceForge.net
i hope this is the right one!

however i have run the code you suggested and like i mentioned above , with that only led on digital pin 9 lit.
i looked at the values from the serial monitor on arduino and this is what i get

Before map: {386,0,382,449,425}
After map: {84,0,84,98,93}
Before map: {359,0,379,455,429}
After map: {78,0,83,100,94}
Before map: {362,0,375,449,426}
After map: {79,0,82,98,93}
Before map: {363,0,379,449,427}
After map: {79,0,83,98,93}
Before map: {352,0,366,316,347}
After map: {77,0,80,69,76}
Before map: {281,0,375,44,155}
After map: {61,0,82,9,34}
Before map: {298,0,254,348,326}

thank you very much !!!:slight_smile:

m56789,
I understand your problem exactly, I just don't understand why you can get it to work with one LED, but not two. Your diagram looks ok.

This code should successfully change the brightness of the LED.

First, run this code and tell me if the LED fades from bright to off. We will only work with the first LED on PIN 9.

void setup() {
  pinMode(9, OUTPUT);
}

int val = 0;
void loop() {
  val++;
  if (val > 250) val = 0;
  analogWrite(9, val);
  delay(50);
}

If that is correct, then try this code:

int val;

void setup() {
  Serial.begin(9600);
  pinMode (9, OUTPUT);
}

void loop() {
{
  val = analogRead(3);

  Serial.print("Before map: {");
  Serial.print(val);
  Serial.println("}");

  analogWrite(9, val);

  delay(500);
}

And then wave your hand over the LDR and copy and paste the Serial output here.

hey
first of all thank you very much:)
ok
running the first code....nothing happened. the led was on all the time

now...
running the second code the led faded when covering the ldr

here are the outputs from the serial monitor

Before map: {360}
Before map: {257}
Before map: {183}
Before map: {350}
Before map: {363}
Before map: {360}
Before map: {363}
Before map: {364}
Before map: {357}
Before map: {181}
Before map: {53}
Before map: {15}
Before map: {15}
Before map: {348}

the small numbers to the end are when the ldr was covered

this is fine....how ever one small problem
i want the opposite to happen.
the led to be off and light up once the ldr is covered
and ofcourse...well you know i need to use at least 5 ldrs to control 5 leds.....

it s for my thesis design project you see and i need to prove some kind of stuff :stuck_out_tongue:

nevertheless i thank u very much for the help!

by the way something else crossed my mind...is this supposed to work with all types all ldrs? or is it a particular one that suits it best? it might be the case that the ldr i have is not the best suitable for this purpose....perhaps?

thanks!!!!

ok so since the last code worked, although i want the opposite effect to happen i thought to give it a try to get the other 2 leds and ldrs working

i have changed the code to this

int val;

void setup() {
Serial.begin(9600);
pinMode (9, OUTPUT);
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);
}

void loop() {
{
val = analogRead(3);

Serial.print("Before map: {");
Serial.print(val);
Serial.println("}");

analogWrite(9, val);

delay(500);
}
{
val = analogRead(2);

Serial.print("Before map: {");
Serial.print(val);
Serial.println("}");

analogWrite(10, val);

delay(500);
}
{
val = analogRead(1);

Serial.print("Before map: {");
Serial.print(val);
Serial.println("}");

analogWrite(11, val);

delay(500);
}
}

and the values i got are below

Before map: {342}
Before map: {392}
Before map: {0}
Before map: {341}
Before map: {391}
Before map: {0}
Before map: {344}
Before map: {384}
Before map: {0}
Before map: {343}
Before map: {386}
Before map: {0}
Before map: {340}
Before map: {387}
Before map: {0}
Before map: {58}
Before map: {392}
Before map: {0}
Before map: {48}
Before map: {389}
Before map: {0}
Before map: {53}
Before map: {252}
Before map: {0}
Before map: {300}
Before map: {11}
Before map: {0}
Before map: {302}
Before map: {10}
Before map: {0}
Before map: {302}
Before map: {364}
Before map: {0}
Before map: {299}
Before map: {278}
Before map: {0}
Before map: {304}
Before map: {274}
Before map: {0}
Before map: {300}
Before map: {380}
Before map: {0}
Before map: {143}
Before map: {387}
Before map: {0}
Before map: {70}
Before map: {391}
Before map: {0}
Before map: {79}
Before map: {144}
Before map: {0}
Before map: {287}
Before map: {11}
Before map: {0}
Before map: {289}
Before map: {370}
Before map: {0}
Before map: {309}
Before map: {380

number {0} is the 3rd ldr on analog 1 which seems to be dead.
however the 2nd one is working although the led did not respond at all to it.

thanks!