Go Down

Topic: Can I save small amounts of data in EEPROM or should buy datalogging shield? (Read 2462 times) previous topic - next topic

2xw2

Hello,

I'd like my arduino to take temperature readings every hour for a period (which will depend on my battery life I suppose). Given the readings will be taken every hour, can they be written to EEPROM, as opposed to saving to an SD card?

My reading around seems to have confused me more, if 1 character is 1 byte then a temperature reading will be 4 bytes (i.e 23.4) and leaving the arduino for 24 hours would result in 92 bytes of data, is this correct?

Could I then retrieve the device and read this list of numbers from the EEPROM?

If not, I will just buy an ethernet/datalogging shield.

Thanks for any help, sorry for the nooby question.


Magician

Quote
24 hours would result in 92 bytes of data, is this correct?

How come, 24 x 4 = 96.
Arduino IDE has an EEPROM library, with an example.
Code: [Select]
/*
* EEPROM Read
*
* Reads the value of each byte of the EEPROM and prints it
* to the computer.
* This example code is in the public domain.
*/

#include <EEPROM.h>

// start reading from the first byte (address 0) of the EEPROM
int address = 0;
byte value;

void setup()
{
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

void loop()
{
  // read a byte from the current address of the EEPROM
  value = EEPROM.read(address);
 
  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  Serial.println();
 
  // advance to the next address of the EEPROM
  address = address + 1;
 
  // there are only 512 bytes of EEPROM, from 0 to 511, so if we're
  // on address 512, wrap around to address 0
  if (address == 512)
    address = 0;
   
  delay(500);
}

What you need, is declare a union to have access to float as 4 individual bytes.

2xw2

Yes my terrible maths is terrible.

I didn't look at the libraries thank you very much for pointing that out and thanks for your time

wildbill

You should be able to store your temperature as an int and interpret it as a fixed point value - one or two decimals. That's two bytes, so on an Uno you can hold ~21 days of data.

2xw2


You should be able to store your temperature as an int and interpret it as a fixed point value - one or two decimals. That's two bytes, so on an Uno you can hold ~21 days of data.


That would be excellent, how is it that I would do that?

I currently have the temperature being stored, but only the first two numbers of it - making it accurate to 1 degree at a time; I would prefer it to be accurate to the tenth of a degree and be stored as a decimal (10.2 for example)

wildbill

To store in fixed point format, take the floating point temperature you have, multiply by 10 and store it in an int. So 10.2 is stored as 102. Push the int into EEPROM, keeping track of which the next unused available location is. Once you have this working, you may want to consider keeping that information in EEPROM too in case of a power outage.

Of course, when you retrieve the data, you'll need to divide the temp by 10 to get back to a proper reading - be careful that your math doesn't truncate it before you get it into a float again.

2xw2

Alright so my current code is

Code: [Select]

#include <EEPROM.h>

int addr = 0;

void setup()
{
}

void loop()
{
  int val = (5.0 * analogRead(0) * 100.0) / 1024;
 
  EEPROM.write(addr, val);
 
  addr = addr + 1;
  if (addr == 512)
    addr = 0;
 
  delay(10000);
}


So I take my int value which is the formula there and multiply it by ten? Is it that simple?

PaulS

Quote
Is it that simple?

No. An int is two bytes. You can't fit two bytes in one address. highByte() and lowByte() to the rescue. And, you want to scale the float by 10, not the int.

But, before you go there, what range of temperatures are you expecting? You might be able to fit all the values in a byte, by using an offset.
The art of getting good answers lies in asking good questions.

2xw2

I'm expecting values realistically between 5 and 16 degrees celcius.

wildbill

Then just measure in 0.1 of a degree and represent that as a byte. Then you can measure from 0C to 25.5C.

2xw2

I'm a little confused over how I should do that?

I think I'm tripping over myself here.

wildbill

Get your temperature in Centigrade in a float variable. Multiply it by 10.0 and store it in a byte.

Go Up