putting 12 ldr on my arduino uno board

Hi everyone.
I have a project which i have to put 12 ldr on my arduino board, all i know is the ldr should put on analog pin(A0-A5),,
The question is can i put the ldr in digital pin? And anybody can give me sketch of arduino use a digital pin for ldr??
Thx before.. :slight_smile:

A digital pin can't read a ADC value so your 6 pins short.

You could charge a cap and see how long it takes to discharge. But that's a lot of work I would look for a ADC chip that has 12 inputs and let the arduino read that
like this one MAX1028

You can multiplex the LDR's quite easily with - SparkFun Analog/Digital MUX Breakout - CD74HC4067 - BOB-09056 - SparkFun Electronics? - you can address the board with 4 digital lines to select one of the 12 LDR's

select LDR 0 all lines are LOW -

A function like this can be handy to set the right lines HIGH/LOW

int readLDR(uint8_t nr)
{
  digitalWrite(..., nr & 1);
  digitalWrite(..., nr & 2);
  digitalWrite(..., nr & 4);
  digitalWrite(..., nr & 8);
  analogRead(A0);  // to prevent a bit of noise
  return analogRead(A0);
}

The question is can i put the ldr in digital pin?

The answer is a definitive yes:

  1. if you are looking for certain brightness / resistance out of the ldr, you can adjust the pull-up/down resistors so that it will trigger a logic '0' or '1';

  2. with the help of a capacitor, you can perform adc to detect the ldr's resistance - this was an old trick from early days of embedded programming.

hai dhenry ..

i'm sorry i have just read your reply, but actually, i can't understand about your answer, it cause i'm just a nubi on electronically ..
can you give me more explanation?

well,,
the next question is, can i put 12 ldr on my arduino board without breakout board or another additional board??

i have just read this link Using a Photocell | Photocells | Adafruit Learning System but i have not yet solve the problem, why my ldr value always give me a "zero value" ??
and here's the code

int photocellPin = 2; // the LDR and cap are connected to pin2
int photocellReading; // the digital reading
int ledPin = 13; // you can just use the 'built in' LED
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // have an LED for output
}
void loop(void) {
// read the resistor using the RCtime technique
photocellReading = RCtime(photocellPin);
if (photocellReading == 30000) {
// if we got 30000 that means we 'timed out'
Serial.println("Nothing connected!");
} else {
Serial.print("RCtime reading = ");
Serial.println(photocellReading); // the raw analog reading
// The brighter it is, the faster it blinks!
digitalWrite(ledPin, HIGH);
delay(photocellReading);
digitalWrite(ledPin, LOW);
delay(photocellReading);
}
delay(100);
}
// Uses a digital pin to measure a resistor (like an FSR or photocell!)
// We do this by having the resistor feed current into a capacitor and
// counting how long it takes to get to Vcc/2 (for most arduinos, thats 2.5V)
int RCtime(int RCpin) {
int reading = 0; // start with 0
// set the pin to an output and pull to LOW (ground)
pinMode(RCpin, OUTPUT);
digitalWrite(RCpin, LOW);
// Now set the pin to an input and...
pinMode(RCpin, INPUT);
while (digitalRead(RCpin) == LOW) { // count how long it takes to rise up to HIGH
reading++; // increment to keep track of time
if (reading == 30000) {
// if we got this far, the resistance is so high
// its likely that nothing is connected!
break; // leave the loop
}
}
// OK either we maxed out at 30000 or hopefully got a reading, return the count
return reading;
}

The adafruit page uses the adc trick i was talking about.

As to your issue, you may want to double check to make sure that your connection is correct and your code matches up well with your hardware.

i'm sorry i have just read your reply, but actually, i can't understand about your answer

It's not you it is him, most people who know what they are on about find it hard to follow this member.

What is being said is that you can feed the LDR into a digital pin, but then you will only get high and low readings, even then it is not very stable so scrub that one.

Second the suggestion of making some sort of analogue to digital converter by seeing how long it takes a capacitor to discharge is valid but a bit fiddley. As you have seen things have to be just right for it to work.

The best way is to use an analogue multiplexer on the A/D input but you seem to have this ruled out for some reason.

What type of LDR do you have? There are many and have different resistance ranges.

I played around a 10k photoresistor / 4.7uf cap. With my finder on the photoresistor, it is over 30k counts; with room lit, it is 19 - 26k counts; with a flash light shining on the photoresistor, it is 150ish.

Similar results when other capacitors are used.

Hi,

I am also looking to connect 12 LDRs to my Arduino UNO and was hoping to avail of the digital pins. I am only wishing to use the LDRs to produce 2 values, on and off.

This is for an audio project and I want to use the LDRs to trigger a note on or note off.

Is this possible?

I have heard about using multiplexers on the analogue pins but I don't really want to take the route as I have other plans for those pins :slight_smile:

This is for an audio project and I want to use the LDRs to trigger a note on or note off.

Yes but you will have to do some measurements and testing to get them set up.
Put the LDR between the digital input and the +5V. From the input also connect the wiper of a pot, connect one of the ends of the pot to grouns and leave the other end unconnected.
Put the light on and adjust the pot so that it is on the edge of reading a zero or a one and leave the pot on the side that gives you a one.
Then cover the LDR and you should read a zero.
Now remove the pot and without turning it measure the resistance you had between input and ground. Replace the pot with a fixed resistor of this value.
This will work but it will not be very tolls rent of variations in light level.

Is this possible?

Absolutely. Read the adafruit page linked earlier or the code presented here and you are set.

Thanks for the help! :slight_smile: