Go Down

Topic: How to write and read the EEPROM? (Read 4154 times) previous topic - next topic


Hi guys,

That´s my first post here so please, apologize me for the dumb question, i was looking for and i didn´t found it.  :smiley-red:

I need to get a number valor from the serial and save it in EEPROM and read it, same  after arduino was restarted.

It´s like in shell script, you know:
number_serial > number.txt

cat number.txt > NUMBER01

Help me plz ^^
www.babuino.info ^^

Jack Christensen

Help > Reference > Libraries > EEPROM


That´s what i need thk you =DDDD  XD

But I´m with doubts in the exemple:

#include <EEPROM.h>

void setup()
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, i);

void loop()

I have that comes form serial (IRlib):
  if (irrecv.decode(&results)) {  // IR
    Serial.println(results.value);  // IR
    irrecv.resume(); // IR

    IRr = (results.value * 1); 
    Serial.println(IRr );

How can I write IRr valor in the EEPROM?

Like this?
EEPROM.write(i, IRr);
www.babuino.info ^^


That are the kind of values that I need to storage:

IRn2= 13646
IRr= 13646
IRn= 13646

other exemple:
IRn2= 13646
IRr= -14870
IRn= 13646

www.babuino.info ^^

Jack Christensen

The EEPROM library only deals with bytes. There are several approaches to handle longer data types. For example: (1) Reduced the longer types to byte values using bit shift operations, (2) Use UNION to access the individual bytes in longer data types, (3) Use the eeprom_write_* and eeprom_read_* functions defined in AVR-Libc. Here is an example of the latter. Note there are also functions for double words (32-bit integers), blocks, and bytes.

Code: [Select]
#include <avr/eeprom.h>      //http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html
#include <EEPROM.h>          //http://arduino.cc/en/Reference/EEPROM
#include <Streaming.h>       //http://arduiniana.org/libraries/streaming/

int myInt1, myInt2;          //note that a 16-bit int is also referred to as a word
float myFloat1, myFloat2;    //floats require 32 bits

void setup(void)
   myInt1 = 31416;
   myFloat1 = 3.14159;
   eeprom_write_word( (uint16_t *) 10, myInt1 );    //write a 16-bit int to EEPROM address 10
   eeprom_write_float( (float *) 20, myFloat1 );    //write a float to address 20
   myInt2 = eeprom_read_word( (uint16_t *) 10 );    //read a 16-bit int from address 10
   myFloat2 = eeprom_read_float( (float *) 20 );    //read a float from address 20
   Serial << _DEC(myInt2) << endl;                  //print the values read
   Serial << _FLOAT(myFloat2, 5) << endl;

void loop(void)


We have the same topic I think. It might help.


Jack, how do I get that? #include <avr/eeprom.h>      //http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html

I just found that:


www.babuino.info ^^

Jack Christensen

Jack, how do I get that? #include <avr/eeprom.h>      //http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html

It's already there, just type it in. The code I posted above should compile and run fine. AVR-Libc is part of the Arduino toolchain.


www.babuino.info ^^

Jack Christensen


Check the Arduino Reference, it explains the steps nice and clear :)

http://arduino.cc/en/Reference/EEPROMRead - Read
http://arduino.cc/en/Reference/EEPROMWrite - Write

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131