Pages: [1]   Go Down
Author Topic: Only 1 input reading from LDR on analog input?  (Read 522 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi guys,

I have 2 LDR resistors hooked up to test, hoping to make a photovore as my first real project, but no matter what I do i can only get a readout from the first one,

I have the wiring like this:

5v (common) - 1k resistor - (wire to analog pin) + another wire from resistor to LDR - LDR to ground

exactly the same wiring for each LDR, I want to use analog input 0 and 1, but I have tried in every combination accross all the analog pins, and no matter what I try I only get the input for the first LDR, I have tried swapping the LDRs in case one of them was faulty, but no matter what way around all I get is the readout of the first one smiley-sad

my code is this:

<code> int reading1;
int reading2;

void setup()
{
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
 
}
void loop()
{
  reading1 = analogRead(A0);
  delay(5);
  reading2 = analogRead(A1);
 
  Serial.print("sensor 1 = ");
  Serial.print(reading1);
  Serial.print("  sensor 2 = ");
  Serial.println(reading2);
 
  delay(500);
} </code>

and all i get as a readout is:


I really don't get what I'm doing wrong here, as I know both LDRs work, and the wiring is the same for both, all i can think is either there is something wrong with my code, or something broken in my arduino.

thanks
Logged

Offline Offline
Edison Member
*
Karma: 64
Posts: 2488
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't need these lines:
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);

If you change the code to only use A1 what happens?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for replying smiley

I tried that, changed the code to:

int reading1;
int reading2;

void setup()
{
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
 
}
void loop()
{
  reading1 = analogRead(A0);
  delay(5);
  reading2 = analogRead(A1);
 
  Serial.print("sensor 1 = ");
  Serial.print(reading1);
  Serial.print("  sensor 2 = ");
  Serial.println(reading2);
 
  delay(500);
}


but still just getting the same thing in the Serial Monitor, all the values for the first LDR (Pin A0) are coming through, and just 0 for the second one (Pin A1).

any other ideas? I suppose i could try connecting the second LDR to the digital pins with a capacitor, but I would prefer to have it as analog.

Thanks
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33375
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
all i can think is either there is something wrong with my code, or something broken in my arduino.
Well the code is fine. Well it is not posted correctly and you don't need the delay between reads.
So it is either your wiring, no ground from the other LDRs or something is wrong on your arduino.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tested it out on a friends arduino and it worked fine, must be a hardware problem with mine, thanks everyone for your help though smiley
Logged

Pages: [1]   Go Up
Jump to: