Go Down

Topic: How fragile are LDR sensors - and could mine be broken? (Read 330 times) previous topic - next topic

Coulomb-d

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:

Code: [Select]
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  :smiley-confuse:

larryd

Always show us a good image of your actual wiring.

Measure the voltage drop with a DVM.


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Coulomb-d




I'm measuring 0V on the LDR

Ron_Blain

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






jremington

#4
Feb 17, 2020, 07:26 pm Last Edit: Feb 17, 2020, 07:27 pm by jremington
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).

Coulomb-d

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

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:


jremington

Quote
Wouldn't that damage the ports?
No, but not a very useful thing to do, either.

MarkT

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.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

wvmarle

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.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Coulomb-d

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!

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 :( )

Code: [Select]
// 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.

wvmarle

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.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Coulomb-d

I actually got it to work, there is an adjustment line
Code: [Select]
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 :) Because I have not seen any tutorial mentioning this setup

wvmarle

In this case best to start by printing the actual reading you get from the pin, and go from there.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

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.

wvmarle

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?
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up