EEPROM write and read values are different

Hi,

I’m experimenting with a basic temperature logging using Arduino Uno. I’m using a basic 10K thermistor and found Adafruit’s code on their website for this. I added some code for logging in this value in the 328’s EEPROM. However the value it writes into the EEPROM is different from the one that is read. Also, everytime I upload a sketch does the EEPROM gets cleared? What am I missing here?

I’ve attached the Serial monitor output with this post.

And here is the code:

#include <EEPROM.h>

// which analog pin to connect
#define THERMISTORPIN A0         
// how many samples to take and average, more takes longer but is more 'smooth'
#define NUMSAMPLES 5
// the value of the 'other' resistor
#define SERIESRESISTOR 15000

int samples[NUMSAMPLES];
int EEsize = 1024; 
int i;

void setup(void)
{
  analogReference(EXTERNAL);
  Serial.begin(9600);
}
 
void loop(void)
{
  float average;
  float res;
  float avg;
  float resprint;
  
  // take N samples in a row, with a slight delay
  for (i=0; i<NUMSAMPLES; i++)
  {
   samples[i] = analogRead(THERMISTORPIN);
   delay(50);
  }
 
  // average all the samples out
  average = 0;
  for (i=0; i<NUMSAMPLES; i++)
  {
     average += samples[i];
  }
  average /= NUMSAMPLES;

  // convert the value to resistance
  avg = 1023 / average - 1;
  res = SERIESRESISTOR / avg;
  
  for (int i=0; i<EEsize; i++)
  {
    EEPROM.write(i, res);
    delay(10);
    Serial.print("EEPROM write:");
    Serial.println(res);
    resprint = EEPROM.read(i);
    Serial.print("EEPROM read:");
    Serial.println(resprint);
    Serial.println(" ");
    delay(500);
  }
  delay(500);
}

The EEPROM contains are not affected by resets or power cycles, which means that your code would need to keep track of how much of the EEPROM had already been used.

EEPROM seems like a rather dodgy place for holding logged data, because it has a limited life and would eventually wear out if you keep writing to it. There is also not much of it. If you need to log data persistently then an SD card would be a much better choice. There are inexpensive Arduino clones that have integral SD sockets, and some official Arduino boards and shields do too.

Well, for starters, you can only write single bytes to EEPROM, so you have to split up an int, or a float, or whatever, into individual bytes.

PeterH:
EEPROM seems like a rather dodgy place for holding logged data, because it has a limited life and would eventually wear out if you keep writing to it. There is also not much of it. If you need to log data persistently then an SD card would be a much better choice. There are inexpensive Arduino clones that have integral SD sockets, and some official Arduino boards and shields do too.

Bear in mind that all flash memory, including SD cards have limited numbers of writes. But it is always a matter of trade-offs. If for instance, you are programming a raw ATtiny85 which is rated for 100,000 writes to EEPROM, and you write to it every 10 minutes continiously, it might fail in 2 years. Given the replacement cost is $2, it would be simpler just to replace the chip than to outfit it with a SD card reader and SD card.

Given that SD cards typically also tend to be rated in the order of 100,000 writes, you really aren't saving much if you are dealing with low cost processors. So you need to figure into your total system cost the cost of replacing either the chip or the SD card every year or so if you are doing a lot of writes.

If you are dealing with a higher cost development platform, then yes, the cost/benefit ratio would change. And certainly if you need more than 512 - 2048 bytes, you need to go with a SD card.

As Lar3ry pointed out, your posted example doesn't work because you are trying to write a floating point number (4 bytes) to EEProm, using a function call that writes only one byte.

There are libraries for writing all sorts of variables, one of which is described here: Arduino Playground - EEPROMex

Thanks folks! this certainly does help! I'm considering using an SD card but I'm a newbie to the Arduino and the AVR and I'd want to get my basic programming skills upto the mark. Till then the EEPROM suffices just fine.