How fragile are LDR sensors - and could mine be broken?

I wanted to try out the LDR Sensor from my starter kit. For a quick test, I wired it up like this:

to a


Arduino Uno. (It is a 100K Ohm Resistor.)

This is the code I'm using:

int LDR = A0; // select the input pin for LDR


int val = 0; // variable to store the sensor value 
void setup() {
Serial.begin(9600); //set serial port for communication
}
void loop() {
val=analogRead(LDR);
  val = map(val,0,1023,0,100);
  Serial.println(val);



delay(100);

}

But not matter if I'm shining 10W Flashlight on it or blocking it completely, the serial Monitor always gives out a reading of 100.

Does this mean my sensor is broken? Or, more likely, I'm doing something wrong :confused:

Always show us a good image of your actual wiring.

Measure the voltage drop with a DVM.

I'm measuring 0V on the LDR

Measure the Light and Dark resistance of the photo cell (LDR)

You have 100K in series. That's quite a bit.

Typical inexpensive LDRs like you get a dozen for a few dollars on Amazon in bright light a few hundred ohms and in dark (covered) I get about 9 or 10K. So do the math for your series circuit. Using 200 ohms and 10 K Ohms what voltage swing will the Arduino analog in channel see?

Ron

LDR sensors are quite robust. Check it with a multimeter by measuring the light and dark resistance. If you don't have a multimeter, buy one. They are essential tools for this hobby (and for pros).

I took 100K Ohm because the instructions said so, and I trusted them :slight_smile:

Now with 200 Ohms, 0.1% of my initial Resistor value mind you, the Serial Monitor reports 6 (Dark) to 700(10W Flashlight). And with ceiling lights (57 in Monitor) I measure 4.6 Volts on my DMM.

In the meantime, I also fond someone who connected the LDR directly in between A0 and A1, without any resistor or something in between. Wouldn't that damage the ports? Source:

Wouldn't that damage the ports?

No, but not a very useful thing to do, either.

Resistors connected between two inputs do nothing dangerous, nor nothing very useful! Its passive and can't generate any voltage by itself. Connecting an LDR between Vcc and GND could be dangerous in bright light - enough current might flow to destroy the thing or short out the supply.

Ron_Blain:
in dark (covered) I get about 9 or 10K.

Cover it better. A properly darkened LDR (mind: the edges and the back of it count as well) should go to 1M easily.

For the OP: it is possible to wire an LDR between a pin and GND, then enable the internal pull-up. Using analogRea() or even digitalRead() (A0-A6 are basically digital pins with analog input ability) you can distinguish between light and dark.

Thanks! I ended up getting a basic Blink LED to work, but only by using a 200 Ohm Resistor, not as described in the tutorial 100K Ohm!

wvmarle:
For the OP: it is possible to wire an LDR between a pin and GND, then enable the internal pull-up. Using analogRea() or even digitalRead() (A0-A6 are basically digital pins with analog input ability) you can distinguish between light and dark.

But in the picture I've provided, the LDR is NOT wired in between Pin and GND, but directly to two Analogue pins?

It is intended for a night-mode. Here's the code from the Sketch that goes with that picture. (Unfortunately, they never created a proper wiring diagram :frowning: )

 // setup measurement for LDR
  digitalWrite(A0, HIGH);  // set pullup on analog pin 0 
  pinMode(A1, OUTPUT);
  digitalWrite(A1, LOW);    
  
}

unsigned int ldr=0;

void doLDR() {
  unsigned int ldr_value = analogRead(0);          //reads the LDR values
  
  //Adjust for LDR variations.
  //dark = 1023
  //light = 0
  //If your LDR for example reaches a max analogRead value of 350 instead of 1023 for 100% dark.
  //unsigned int ldr_adjusted=map(ldr_value,0,350,0,1023);
  unsigned int ldr_adjusted=map(ldr_value,0,700,0,1023);

  //set max limit (to avoid potential 'divide-by-zero's).
  if (ldr_adjusted >= 1023) ldr_adjusted = 1023;

  //ldr follows ldr_adjusted, but is averaged so that values only change gradually
  ldr = ((unsigned long)(FILTER_LEN-1)*ldr+ldr_adjusted)/FILTER_LEN;

  unsigned int light_level=0;

  //translate [0-1023] to [15-0]
  //light_level = (1024-ldr)>>6;
  light_level=map(ldr,0,1023,15,0);  
  if (light_level >= 15) light_level = 15;
  if (light_level <= 1) light_level = 1;

Source

If I reproduce that, I don't get any reading on A0 or A1 at all. I probably should try to wire it like in my example and put the code in to make it work.

Wired between A0 and A1, where A1 is used as ground, and using the internal pull-up of A0 to create the voltage divider.

That's probably a newbie that doesn't realise you can connect multiple things to a single ground pin... It could have been used to save battery power but then A1 should be set to INPUT or HIGH when no readings required, and that doesn't happen.

When you remove the LDR your readings should jump to 1023.

I actually got it to work, there is an adjustment line unsigned int ldr_adjusted=map(ldr_value,6,400,0,1023); - I set it to 400 and it seems to work. They maped the readings to go from 1 to 15. I learned a great deal replicating that clock project, but the most difficult part was that LDR :slight_smile: Because I have not seen any tutorial mentioning this setup

In this case best to start by printing the actual reading you get from the pin, and go from there.

There is no need whatever for a series resistor. You connect the LDR from an analogue input to ground. Then either configure it's internal pull up, or put your 100K from 5V rail to the LDR input pin.

AJLElectronics:
There is no need whatever for a series resistor. You connect the LDR from an analogue input to ground. Then either configure it's internal pull up, or put your 100K from 5V rail to the LDR input pin.

Don't you see the obvious contradiction in what you're writing here?

wvmarle:
Don't you see the obvious contradiction in what you're writing here?

No, not unless we are discussing semantics? A series resistor and a pullup resistor are not the same. I can't really see the OP's picture well enough to see what is there, but from other posts, it seems that he has 100K in series with the LDR to the input pin? If I have got that right, then it shouldn't be there because it will make the resolution pitiful.

My suggestion was to wire it correctly and use the 100K as a weak pullup. Happy to accept that I have got it all around my neck, so please elucidate if so.

The built-in pull-up resistor IS your series resistor. Same for the 100k you mention, that's also placed in series with the LDR.

wvmarle:
The built-in pull-up resistor IS your series resistor. Same for the 100k you mention, that's also placed in series with the LDR.

Well maybe in your local dialect, but not the Queen's English! A SERIES resistor is in series with the LDR. In series with the circuit it makes from ground to the input pin. The pull up resistor may be in series with the supply, but in this example it is in parallel to the supply. I can see why you consider it series though.

So that pull-up resistor is not in series with the LDR? Just another word for series resistor.

Yet another name: that LDR + pull-up combo is also commonly called "voltage divider", where a voltage divider is made up of two resistors in series...

Image from #17:

That's really the first time ever I see someone call that specifically a "series resistor", and the other one not. Very unusual parlance. But honestly, I don't know how much your Queen knows about electronics, so no idea how she would possibly call it.

AJLElectronics:
There is no need whatever for a series resistor. You connect the LDR from an analogue input to ground. Then either configure it's internal pull up, or put your 100K from 5V rail to the LDR input pin.

Analog pins do not have an internal pull up. Using digitalWrite only pertains to a digital pin. The latter "or put your 100K from 5V rail to the LDR input pin". That's where it all began. Now you have a series LDR voltage divider.

Ron