Trying to save sensor data to EEPROM

Hi there,

Currently building a smoke sensor with an MQ2. I am trying to write only 5 pieces of data to the first five addresses of my EEPROM (only doing that as a test bc I don’t want to burn out my EEPROM). The moment I unplug it from the computer to a 12 V battery supply, I believe it writes. But when I plug it into the computer and read it in serial monitor, the data is different as opposed to values I expected. Is it because of my code?

I’ve attached it as follows

#include <EEPROM.h>

//pin definitions
int smokeA0=A0;

int counter;
int EEsize=5;

void setup() {
//setup pin modes
pinMode(smokeA0, INPUT);
//Serial Port initialize
//counter will read from the address;


void loop() {
// put your main code here, to run repeatedly:

for (int a=0; a<EEsize; a++)
int analogSensor=analogRead(smokeA0);
int smoke=analogSensor/4;
//DETECT here
counter =;
Serial.print("EEPROM position: “);
Serial.print(” contains ");
if (EEsize=5)

sketch_feb16a.ino (747 Bytes)

There are all sorts of problems with your sketch

If EEsize =5 stands out ( ==)

In setup you read a value into counter , which is a value contained at location “0” which will be some random number; not sure why you are doing that

Have a look the examples provided with the eeprom
Library .

Thanks for the advice!

I'm not sure what you mean besides all sorts of problems. The two you mentioned (syntax, read values)I can fix, but shouldn't you read from the first address of EEPROM (0)?

From the sketch you can gather I'm not great at EEPROM and I have looked at all of the libraries but I'm not picking up from it. What else should I work on in my sketch?

each eeprom address is limited to 100,000 write cycles give or take , well, you go beyond it, that particular

'address' will be rendered 'useless' thereafter

shouldn't you read from the first address of EEPROM (0)?

You can read from any address you want, there's no rule saying you have to start at address zero.

I have looked at all of the libraries but I'm not picking up from it.

Can you be more specific?

Also, there's no magical cutoff at 100,000 writes. There may be 100,000 more left, it's just that >100,000 is not guaranteed. And, it's not for the whole EEPROM, only for the location that has accumulated that many. If this many writes is an issue, there are *leveling algorithms * to spread the writes over the whole EEPROM.

I prefer to start at Address 0 just for simplicity. I understand you don’t have to be specific but that’s fine.

There are all sorts of problems with your sketch

Library .

I’m wondering what sorts of problems I have. I don’t think I have a lot but I need more guidance than just problems.

Can you be more specific?

I looked at the codes EEPROM.write and examples, which told me I could use the codes from it or just reference the function from the EEPROM library (which is what I did). I just want to know whether or not I’m saving. It seems like it’s saving values but again I’m not entirely sure what stands out in my code that says it’s not.


counter is being given the initial value of whatever happens to be in location “0”. ( which could be anything from 0 to 255).
Did you mean “ counter=0”?

I mention the library examples, as they will give you a working sketch and show you the correct syntax, they are a good positive place to start.

Solved me issue!

I wasn't clearing my data and I was rewriting and stupidly reading at the same time. Ergo it would make sense for the Arduino to overwrite current data instead of saving it.

And yes it was because I limited my reads to 25 seconds. I ended up changing it to mins (but no greater than 10 mins with some initial delay).

if (EEsize=5)

The first line is just wrong as was pointed out in #1 already.
The exit(0) has no function in Arduino that I'm aware of. There's nothing to exit to.

EEPROM reportedly can do 1-2 mln writes before it breaks down. It's guaranteed 100k writes.