Analogue pins won't read LDR values

Hi Everybody

I have a strange problem that I don’t know if it is software or hardware related. It happens with 022 and 023 versions.
What I want to do is read an analogue value and send it on to an EEPROM. I have adapted a sketch from the Arduino examples called Switch Case under the Control section. That sketch works OK for me but mine doesn’t.

This is not the first time my attempts at reading an analogue value.

So far my trouble shooting has been:

  • trying other analoue pins except for A4 and A5 of course :wink:
  • not using aliases like A0, A1 etc. Instead using 0, 1, 3 etc.
  • trying a global for sensorReading
  • swapping from Uno to a ATmega168 clone board

The other is I now have is the Serial monitor shows nothing at all. Before I started troubleshooting at least I had the serial but 0 for the sensor value

/****************************************************************
** A sketch to read an LDR or thermistor. The data is then sent *
** to an EEPROM 24LC32A by I2C. I intend to expand this sketch *
** have more eeproms and fill each on in turn with data *******/

#include <Wire.h>

byte high = 0x00;
byte low = 0x00;
/int sensorReading = 0; I have tried declaring sensor reading as
a global as an alternative. No difference
/

void setup() {
Wire.begin();
Serial.begin(9600);
}

void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// take that reading then send it on to EEPROM
Wire.beginTransmission(0x50);
Wire.send(high);
Wire.send(low);
Wire.send(byte(sensorReading));
Wire.endTransmission();
delay(200);

Wire.beginTransmission(0x50);
Wire.send(high);
Wire.send(low);
Wire.endTransmission();
Wire.requestFrom(0x50,1);
delay(200);

Serial.print(int(Wire.receive()));
Serial.print(" Address…");
Serial.print(int(high));
Serial.print("…");
Serial.println(int(low));

//increment so all address spots are filled with data
if(low == 255) high++; //0-255 address in low byte
low++;

}

Please help.

What results are you getting? What results do you expect?

How is the LDR wired? You need a pull-up or pull-down resistor to fight against the LDR.

What happens if you wire the input to the supply voltage or to ground?

I am getting correct values when I serial print them. I had not tried that. So it appears I have no hardware problems. However I expected the values to be stored in the EEPROM and when I used this line:

Serial.print(int(Wire.receive()));

I thought I would get the LDR values I sent to it.

However I expected the values to be stored in the EEPROM and when I used this line:

Only if you have the other stuff before it to prime the EEPROM.

Do you have a 24LC32A connected up and have you put pull up resistors (4K7) on the clock and data lines? Note that Wire.send(byte(sensorReading)); Is taking a 10 bit value and converting it into a byte, this is the wrong way to do things. You should do a :- sensorReading = sensorReading >> 2; before to convert it correctly into a byte.

The way to write to the internal EEPROM is here:- http://www.arduino.cc/en/Tutorial/EEPROMWrite and you read it like this:- http://arduino.cc/en/Tutorial/EEPROMRead

Grumpy_Mike:

However I expected the values to be stored in the EEPROM and when I used this line:

Only if you have the other stuff before it to prime the EEPROM.

Do you have a 24LC32A connected up and have you put pull up resistors (4K7) on the clock and data lines? Note that Wire.send(byte(sensorReading)); Is taking a 10 bit value and converting it into a byte, this is the wrong way to do things. You should do a :- sensorReading = sensorReading >> 2; before to convert it correctly into a byte.

The way to write to the internal EEPROM is here:- http://www.arduino.cc/en/Tutorial/EEPROMWrite and you read it like this:- http://arduino.cc/en/Tutorial/EEPROMRead

Thanks. I'll give that a read. I probably should done that first :blush: