LDR spitting out seemingly nonsense values

Hi there.

Using an Elegoo Uno R3. Here is the relevant code:

int LDR = A1;
const int BUTTON1 = 2;
const int BUTTON2 = 3;
const int BUTTON3 = 4;
const int BUTTON4 = 5;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0; 

void setup() {
  pixels.begin();
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(BUTTON3, INPUT);
  pinMode(BUTTON4, INPUT);
  pinMode(A1, INPUT);
  Serial.begin(9600);
}

void loop() {
 LDR = analogRead ( A1 );
Serial.println ( LDR );
delay ( 2000 );  
  if ( LDR < 90 ) {
    pixels.clear();
   pixels.setBrightness(0);
    pixels.setPixelColor(0, pixels.Color(255, 255, 255));
    pixels.setPixelColor(1, pixels.Color(255, 255, 255));
    pixels.setPixelColor(2, pixels.Color(255, 255, 255));
    pixels.setPixelColor(3, pixels.Color(255, 255, 255));
    pixels.setPixelColor(4, pixels.Color(255, 255, 255));
    pixels.setPixelColor(5, pixels.Color(255, 255, 255));
    pixels.setPixelColor(6, pixels.Color(255, 255, 255));
    pixels.setPixelColor(7, pixels.Color(255, 255, 255));
    pixels.setPixelColor(8, pixels.Color(255, 255, 255));
    pixels.setPixelColor(9, pixels.Color(255, 255, 255));
    pixels.setPixelColor(10, pixels.Color(255, 255, 255));
    pixels.setPixelColor(11, pixels.Color(255, 255, 255));
    pixels.setPixelColor(12, pixels.Color(255, 255, 255));
    pixels.setPixelColor(13, pixels.Color(255, 255, 255));
    pixels.setPixelColor(14, pixels.Color(255, 255, 255));
    pixels.setPixelColor(15, pixels.Color(255, 255, 255));
    pixels.setPixelColor(16, pixels.Color(255, 255, 255));
    pixels.setPixelColor(17, pixels.Color(255, 255, 255));
    pixels.setPixelColor(18, pixels.Color(255, 255, 255));
    pixels.setPixelColor(19, pixels.Color(255, 255, 255));
    pixels.setPixelColor(20, pixels.Color(255, 255, 255));
    pixels.setPixelColor(21, pixels.Color(255, 255, 255));
    pixels.setPixelColor(22, pixels.Color(255, 255, 255));
    pixels.setPixelColor(23, pixels.Color(255, 255, 255));
    pixels.show();
  }
  else {
    pixels.clear();
  pixels.setBrightness(75);
    pixels.setPixelColor(0, pixels.Color(255, 255, 255));
    pixels.setPixelColor(1, pixels.Color(255, 255, 255));
    pixels.setPixelColor(2, pixels.Color(255, 255, 255));
    pixels.setPixelColor(3, pixels.Color(255, 255, 255));
    pixels.setPixelColor(4, pixels.Color(255, 255, 255));
    pixels.setPixelColor(5, pixels.Color(255, 255, 255));
    pixels.setPixelColor(6, pixels.Color(255, 255, 255));
    pixels.setPixelColor(7, pixels.Color(255, 255, 255));
    pixels.setPixelColor(8, pixels.Color(255, 255, 255));
    pixels.setPixelColor(9, pixels.Color(255, 255, 255));
    pixels.setPixelColor(10, pixels.Color(255, 255, 255));
    pixels.setPixelColor(11, pixels.Color(255, 255, 255));
    pixels.setPixelColor(12, pixels.Color(255, 255, 255));
    pixels.setPixelColor(13, pixels.Color(255, 255, 255));
    pixels.setPixelColor(14, pixels.Color(255, 255, 255));
    pixels.setPixelColor(15, pixels.Color(255, 255, 255));
    pixels.setPixelColor(16, pixels.Color(255, 255, 255));
    pixels.setPixelColor(17, pixels.Color(255, 255, 255));
    pixels.setPixelColor(18, pixels.Color(255, 255, 255));
    pixels.setPixelColor(19, pixels.Color(255, 255, 255));
    pixels.setPixelColor(20, pixels.Color(255, 255, 255));
    pixels.setPixelColor(21, pixels.Color(255, 255, 255));
    pixels.setPixelColor(22, pixels.Color(255, 255, 255));
    pixels.setPixelColor(23, pixels.Color(255, 255, 255));
    pixels.show();
  }

I'm really not familiar with schematics, but to describe the circuit: One leg of the LDR to positive, other leg inline with first leg of 10K resistor and wire going back to A1 analog input, other leg of 10K resistor goes back to ground.

At one point, with this exact setup, I was getting values in the serial monitor flipping between 0 and ~110. Now I'm getting 15-17 repeatedly. Covering the LDR makes no difference. Putting a light to the LDR makes no difference. I have tried using a different LDR, same thing. Removing the LDR even makes no difference. These seem to be completely nonsense irrelevant values.

I've attached a picture of the physical circuit as well.

Please help.

Thanks,
JC

Don't post a TinkerCad link, many people can't or won't read it. Please edit your post and attach your code in line using code tags, as indicated in the help threads at the top of the forum.

Also please provide a real wiring diagram or schematic, including component values.

aarg:
Don't post a TinkerCad link, many people can't or won't read it. Please edit your post and attach your code in line using code tags, as indicated in the help threads at the top of the forum.

Also please provide a real wiring diagram or schematic, including component values.

Sorry, thanks for the heads up. I just fixed the post to the best of my ability.

Thanks, we're almost there. You're not able to draw a schematic and upload a photo? If you don't know anything about schematics, now is the time to learn. The problem with verbal descriptions is that they always, "sound right" because they subsume the writers blind spots. A schematic is more literal so it can't so easily be deceptive. Also please re-shoot or augment your image so that we can see ALL the connections.

int LDR = A1;
...
  pinMode(A1, INPUT);
...
void loop() {
 LDR = analogRead ( A1 );
...

You appear to be confused about naming pin numbers. The usual way would be like:

int LDR = A1;
...
  pinMode(LDR, INPUT);
...
void loop() {
int reading = analogRead ( LDR );
...

By the way, it's not necessary to set the pin mode to input when using it for analog readings. Have a look at the example analog sketches that ship with the IDE.

What are the red, black and orange wires that leave the picture connected to?

Huh. Yeah - that ought to be working. I can see you have a delay there, and I assume you have an appropriately valued resistor for the voltage divider.

I don't think you need a pinMode on analog inputs?

What happens when you replace the LDR with a resistor whose value you know? If the resistor has the same value as the one in your divider, A1 should read 512-ish.

How about put in a fixed resistor, get the reading you expect, and try wiggling the wires a bit?

aarg:

int LDR = A1;

...
  pinMode(A1, INPUT);
...
void loop() {
LDR = analogRead ( A1 );
...




You appear to be confused about naming pin numbers. The usual way would be like:



int LDR = A1;
...
  pinMode(LDR, INPUT);
...
void loop() {
int reading = analogRead ( LDR );
...




By the way, it's not necessary to set the pin mode to input when using it for analog readings. Have a look at the example analog sketches that ship with the IDE.

Changed the code to exactly what you've got here, and now I'm getting 15 repeatedly in the serial monitor.

PaulMurrayCbr:
Huh. Yeah - that ought to be working. I can see you have a delay there, and I assume you have an appropriately valued resistor for the voltage divider.

I don't think you need a pinMode on analog inputs?

What happens when you replace the LDR with a resistor whose value you know? If the resistor has the same value as the one in your divider, A1 should read 512-ish.

How about put in a fixed resistor, get the reading you expect, and try wiggling the wires a bit?

Replaced the LDR with a 1K resistor. Still 15 repeatedly.
Although when I change
Serial.println ( LDR ));
to

Serial.println (analogRead(A1));

I get fluctuating values between ~80 and 100 (with the LDR) that have no significance to the LDR being covered or exposed to light. With the resistor, it's more like 45-50.

I've been trying to make this thing work all day. I'm a design major doing this for one of my projects. I got everything working in my demo here except the LDR, and good lord, this thing is about to make me lose my mind...

Then please provide the information that you have been asked for...

aarg:
Then please provide the information that you have been asked for...

I believe I did the schematic right...
Forum did not want to accept the jpegs for some reason so here is an imgur link to schematic/detailed images

Edit: I forgot to add values to it. Moved too quick. Resistor is 10K and I do not know the exact value of the photoresistor, kit did not state it

pins_arduino.h, line 57 says that pin number A1 is 15.
One mystery solved, ten more to go.

To avoid confusing between the pin number and the analog value, we like the term 'pin' in the name of a pin.
The analog value from analogRead() can be called 'value' or 'reading' or 'rawADC' and so on.

const int ldrPin = A1;

int reading = analogRead( ldrPin);

A LDR has a certain resistance range, but the cheap ones used for Arduino work best with a 10k resistor.

I see two series jumpers on the A0 lead. Those jumpers are notoriously unreliable, especially the male pins. I would try re-wiring everything once with different wires just to be sure.

Koepel:
pins_arduino.h, line 57 says that pin number A1 is 15.
One mystery solved, ten more to go.

To avoid confusing between the pin number and the analog value, we like the term 'pin' in the name of a pin.
The analog value from analogRead() can be called 'value' or 'reading' or 'rawADC' and so on.

const int ldrPin = A1;

int reading = analogRead( ldrPin);




A LDR has a certain resistance range, but the cheap ones used for Arduino work best with a 10k resistor.

I am indeed using a 10k resistor.
With this change in code, it still seems to be spitting out the pin number as the value.
Here is my new code

#define DELAYVAL 500 
// Time (in milliseconds) to pause between pixels
const int ldrPin = A1;
const int BUTTON1 = 2;
const int BUTTON2 = 3;
const int BUTTON3 = 4;
const int BUTTON4 = 5;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0; 
void setup() {
  pixels.begin();
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(BUTTON3, INPUT);
  pinMode(BUTTON4, INPUT);
  Serial.begin(9600);
}
void loop() {
 int reading = analogRead( ldrPin);
delay ( 500 );  
  if ( ldrPin < 14 ) {

Threshold for if here was just to get my light to come on (it does).

I googled how to use an LDR with the Arduino, figuring I could find some surely working code, and now have this...

const int LDR = A1; 
const int BUTTON1 = 2;
const int BUTTON2 = 3;
const int BUTTON3 = 4;
const int BUTTON4 = 5;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0; 
int LDRReading = 0; 

void setup() {
  pixels.begin();
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(BUTTON3, INPUT);
  pinMode(BUTTON4, INPUT);
  Serial.begin(9600);
}

void loop() {
 LDRReading = analogRead(LDR);
 Serial.println(LDRReading); 
delay ( 500 );  
  if (LDRReading < 14 ) {

With this code I get values bouncing wildly from 49 all the way up to roughly 150, LDR having no effect. Unplugging the wire from the LDR to A1 shoots the values into 300, and that's the only response I can get...

aarg:
I see two series jumpers on the A0 lead. Those jumpers are notoriously unreliable, especially the male pins. I would try re-wiring everything once with different wires just to be sure.

You weren't entirely right. But you were kinda right. I'm almost 90% sure it was my wire going to the positive rail. I had already switched out the jumper on A0 with a different one to no avail. Just swapped the positive connection running to the LDR and suddenly it's working.

Banged my head against a wall for hours over a wire. Wow. Thanks for your help! And thank you to everyone else who replied as well.

I'm glad you got it working. You are not the first one with this problem :frowning:

Can you please call the pin number "LDRpin", "ldrPin", "pinLDR", "pin_ldr" or something like that ?

When you touch (or almost touch) an analog input pin and its value shoots up or down or gives a lot of noise, then it could be a broken wire or bad breadboard connection. The impedance of a analog input is very high.

Let's look at this again.

int LDR = A1;

.....
LDR = analogRead ( A1 );

ok. is the variable named LDR meant to be the pin number of the LDR or is it meant to be the reading of the LDR? The reason you get spam '15' from your serial.print is probably because A1 is pin 15 on the hardware and that's what you are printing.

I like to end variables that hold in numbers with the word 'pin'. I usually declare them as const byte:

const byte ldrPin = A1;

All of my pin mode and pin read functions will have a first argument ending with 'pin', and nothing else will. Keeps things neat.

I'm almost 90% sure it was my wire going to the positive rail.

Oh. problem solved.