How can i read two LDR's at once. One displays 0-23 and the other 0-1023

Hello all,

Here is the portion of my code im using to display A0 and A1 (LDR signals) side by side in serial monitor. My circuit has been checked over and over and when i switch the signal cables between A0 and A1 the reading follows ruling out any circuit or component failures. It currently displays between 0-1023 for A0 and 0-30 for A1. Both use the same ground and power.

Any ideas on what im doing wrong or missing?

Thanks,
Dean.

Below shows how i have my LDR’s wired (im using two so far).

Coding

#include <Servo.h> // include Servo library 

Servo S1; //servo 1
int servo =90; //servo initial position

int ldrl =A0; //LDR right
int ldrr= A1; //LDR left

void setup()
{
  Serial.begin(9600); //standard baud
  S1.attach(9); //Attaching pin 9 to servo
}

void loop() 
{
  int lt = analogRead(ldrl); // Reads A0 analogue ouput
  int lr = analogRead(ldrr); //Reads A1 analogue output
 Serial.print("A0 lt: ");Serial.print(lt); //Prints the value of A0 to serial monitor
 Serial.print(" A1 lr: ");Serial.println(lr); ////Prints the value of A1 to serial monitor

You're attempting to read data from A0 and A1 but your wiring shows nothing connected to those pins.

Your last line has four forward slashes in a row. This can cause problems. Delete two of them.

Any slash after the first two is taken as text to be ignored. I use that often to distinguish between lines to be left in the code as comments and those to be deleted in the final version. A search for '////' finds them easily.

Because your reading different analogue pins one after the other it helps to double read each one and discard the first result. When the ADC multiplexer switches pins it can take a little time to settle down. Reading and discarding the first result causes the multiplexer to switch to the new pin and read it, then the second read does not need to switch pins as the last read was on the same pin and you should get a more stable result.

Any slash after the first two is taken as text to be ignored.

Had to look up a much older thread which had a problem ... the OP had //// ... . The forward slash within his text caused the following line of code to be interpreted as text.

Yes, your correct.

Arctic_Eddie: You're attempting to read data from A0 and A1 but your wiring shows nothing connected to those pins.

I should have mentioned thats not my image and was just using it as a representation. Good observation all the same!

Thanks for your reply. :)

I should have mentioned thats not my image and was just using it as a representation.

Isn't that special.

.

Riva: Because your reading different analogue pins one after the other it helps to double read each one and discard the first result. When the ADC multiplexer switches pins it can take a little time to settle down. Reading and discarding the first result causes the multiplexer to switch to the new pin and read it, then the second read does not need to switch pins as the last read was on the same pin and you should get a more stable result.

Unfortunately it hasn't worked (considering iv done it correctly). But i see what you mean. This is how I tried it. Thanks.

int lt= analogRead(ldrl); // Reads A0 analogue ouput lt= analogRead(ldrl); // Reads A0 analogue ouput int lr=analogRead(ldrr); //Reads A1 analogue output lr=analogRead(ldrr); //Reads A1 analogue output

Code works fine here (after adding a closing bracket). It must be your hardware. Check/measure the value of the pull up resistors. Leo..

and when i switch the signal cables between A0 and A1 the reading follows ruling out any circuit or component failures.

The reading follows?

Do you mean A1 becomes operational or does it still give readings of 0-30?

...or do you mean the problem moves to A0?

If the problem moves, then you have a problem with one of the sensors then it could be a wrong resistor value or bad connection or defective sensor or possibly you have a split-rail breadboard.

If the problem remains at A1, then you could still have the wrong resistor value, defective analog input or possibly you have a split-rail breadboard.

You can test A1 by disconnecting the LDR then connect a 1K pullup resistor to 5V. What reading do you get?

Split rail breadboard (need to add jumper in the middle to join the red bus, also a jumper to join the blue bus) |500x500

Maybe you have 2 different LDR's? What resistance readings do you get on a multimeter if you measure across the LDR pins in the same lighting conditions?

Hi,

Can you please post a copy of [u]your circuit[/u], in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a picture of your project?

Do you have a DMM?

Thanks.. Tom... :)