Arduino Store int into EEPROM

In this tutorial I’m going to show you how to store an Arduino int data type into your EEPROM memory.

If you don’t know how to store and read a byte into EEPROM on Arduino, please check out this Arduino EEPROM tutorial first.

This tutorial applies to all Arduino boards that have EEPROM Memory, like the Arduino Uno, Mega, Nano. Arduino Due, Zero, and other Arduino with SAMD micro-controller do not have EEPROM memory.

>> Watch this video as an additional resource to this article:

After watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!


You are learning how to use Arduino to build your own projects?

Check out Arduino For Beginners and learn step by step.


Data types with more than one byte

So, it’s nice to be able to store bytes into the EEPROM, but what if you need to store bigger numbers? For example, on Arduino Uno/Mega, an int will take 2 bytes and a long will take 4 bytes. If you try to store an int number – bigger than 255 – then you’ll loose some data and you won’t be able to retrieve the number back.

Here’s how we’ll proceed instead:

Arduino EEPROM separate Int into 2 Bytes

We’ll simply break the number into several bytes, and store each byte separately. So, for an int, we’ll create 2 bytes, and for a long we’ll create 4 bytes.

It’s easy to check how many bytes a certain data type has. Simply use the sizeof() function and print the result on the Serial monitor. For this tutorial we’ll focus on int, unsigned int, and long.

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

  Serial.print("Size of Char: ");
  Serial.println(sizeof(char));

  Serial.print("Size of Bool: ");
  Serial.println(sizeof(bool));
  
  Serial.print("Size of Int: ");
  Serial.println(sizeof(int));

  Serial.print("Size of Unsigned Int: ");
  Serial.println(sizeof(unsigned int));

  Serial.print("Size of Long: ");
  Serial.println(sizeof(long));
}

void loop() { }

Here’s the result that you’ll see on the Serial Monitor:

Size of Char: 1
Size of Bool: 1
Size of Int: 2
Size of Unsigned Int: 2
Size of Long: 4

As you can see, char and bool data types only use 1 byte. So, no problem here.

As for int and unsigned int, you’ll need 2 bytes.

Finally, long numbers are stored on 4 bytes.

Great, now we know exactly how many bytes we need to store on EEPROM for each data type!

Store Int into Arduino EEPROM

Write Int into EEPROM

void writeIntIntoEEPROM(int address, int number)
{ 
  byte byte1 = number >> 8;
  byte byte2 = number & 0xFF;

  EEPROM.write(address, byte1);
  EEPROM.write(address + 1, byte2);
}

This function will take 2 arguments: the address from where you want to write the int, and the actual int number to store.

From the int number, we create 2 bytes. For the first byte, we shift all the bits from the number to the right, 8 times. This way, we only get the higher 8 bits.

For example, with the number 18404, the representation in bits will be: 01000111 11100100. If we shift this number 8 times to the right, we simply obtain 01000111.

For the second byte, we make an AND operation with 0xFF, which is the hexadecimal representation of 255 (you could write “& 255” instead of “& 0xFF”). This way, we only get the 8 bits on the right. 01000111 11100100 becomes 11100100.

Now that we have separated the number into 2 bytes, we can store each byte with the EEPROM.write() function. The first byte will be stored on the given address, and the second one on the next slot.

You can also reduce the amount of code you write (I’ve used a more detailed code to explain the concept, which is not quite optimized):

void writeIntIntoEEPROM(int address, int number)
{ 
  EEPROM.write(address, number >> 8);
  EEPROM.write(address + 1, number & 0xFF);
}

Read Int from EEPROM

Let’s now read the int number that we’ve just written into EEPROM.

int readIntFromEEPROM(int address)
{
  byte byte1 = EEPROM.read(address);
  byte byte2 = EEPROM.read(address + 1);

  return (byte1 << 8) + byte2;
}

This function will take one argument: the starting address we used for writing the number.

We then retrieve the 2 bytes with EEPROM.read().

And finally we re-construct the int number, by doing the opposite of what we did before. We shift the highest bits by 8, this time to the left. And we add the lower bits to the number.

Thus, if we stored 01000111 on the first address and 11100100 on the second address, we’ll get the number 01000111 11100100. This number, with a decimal representation, is 18404.

You can also reduce the amount of code so as to not create intermediate variables:

int readIntFromEEPROM(int address)
{
  return (EEPROM.read(address) << 8) + EEPROM.read(address + 1);
}

Code example to write and read an Int into EEPROM

Here’s a complete example with the 2 functions discussed above, and a testing code.

#include <EEPROM.h>

void writeIntIntoEEPROM(int address, int number)
{ 
  EEPROM.write(address, number >> 8);
  EEPROM.write(address + 1, number & 0xFF);
}

int readIntFromEEPROM(int address)
{
  return (EEPROM.read(address) << 8) + EEPROM.read(address + 1);
}

void setup() {
  Serial.begin(9600);
  writeIntIntoEEPROM(45, 18404);
  int number = readIntFromEEPROM(45);
  Serial.print("Number: ");
  Serial.println(number);
}

void loop() {}

You can also test with negative int numbers, it will work the same.

Store unsigned int into EEPROM

An unsigned int also takes 2 bytes. Here’s a code example – which is 95% similar to the code for an int.

#include <EEPROM.h>

void writeUnsignedIntIntoEEPROM(int address, unsigned int number)
{ 
  EEPROM.write(address, number >> 8);
  EEPROM.write(address + 1, number & 0xFF);
}

unsigned int readUnsignedIntFromEEPROM(int address)
{
  return (EEPROM.read(address) << 8) + EEPROM.read(address + 1);
}

void setup() {
  Serial.begin(9600);
  writeUnsignedIntIntoEEPROM(87, 2500);
  unsigned int number = readUnsignedIntFromEEPROM(87);
  Serial.print("Number: ");
  Serial.println(number);
}

void loop() {}

Store Long into Arduino EEPROM

Now that you’ve seen how things work for an int number, well, good news for you: it will be almost the same for the long data type.

For a long number, you have 4 bytes instead of 2.

Here’s a code example working for long numbers.

void writeLongIntoEEPROM(int address, long number)
{ 
  EEPROM.write(address, (number >> 24) & 0xFF);
  EEPROM.write(address + 1, (number >> 16) & 0xFF);
  EEPROM.write(address + 2, (number >> 8) & 0xFF);
  EEPROM.write(address + 3, number & 0xFF);
}

long readLongFromEEPROM(int address)
{
  return ((long)EEPROM.read(address) << 24) +
         ((long)EEPROM.read(address + 1) << 16) +
         ((long)EEPROM.read(address + 2) << 8) +
         (long)EEPROM.read(address + 3);
}

void setup() {
  Serial.begin(9600);
  
  writeLongIntoEEPROM(100, 123456);
  long number = readLongFromEEPROM(100);
  
  Serial.print("Number: ");
  Serial.println(number);
}

void loop() {}

For this example, we store the number 123456. Converted to bits: 00000000 00000001 11100010 01000000.

From that point, what we’ll do is the exact same thing as for an int number, but we’ll just have to make more bit shifts. To get the first byte (8 bits), we have to shift 24 times to the right. For the second, 16 times. And for the third, 8 times. Also, each time we apply the AND operator (0xFF) to keep only the 8 bits that we want.

And then, to read the long number, you just have to reverse what you did for writing. Instead of shifting bits to the right, you’ll shift them to the left, and add all 4 bytes to get the final long number.

Conclusion

In this tutorial you’ve seen how to store int numbers into the Arduino EEPROM. You now also know how to store unsigned int and long numbers.

Note that the EEPROM memory is not finite. For example, on Arduino Uno, you only have 1024 bytes available. It means you can store a maximum of 512 int, or 256 long numbers.

Also, as you store numbers on multiple addresses, it’s best to know the starting address for a given number. If you store an int on address 76, and a long on address 78, then if you don’t know those 2 addresses you might end up reading on EEPROM with an unwanted offset, and all values will be garbage. Thus I advise you to follow a simple and predictable system for storing data into EEPROM.

To go further, check out how to store int arrays into the EEPROM.

Did you find this tutorial useful?

Do you want to learn Arduino from scratch?

If yes, this course is for you:

Arduino For Beginners - Complete Course

>> Arduino Programming For Beginners <<