Code for reading from EEPROM

For reading the I2C EEPROM at given addresses I’ve written the following code in Arduino IDE language. I’ve added inline comments to help you understand what each line does.

//######### eeprom_read.ino ##########

#include <Wire.h> //I2C library

  byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {              // function for reading one byte at given address
    byte rdata = 0xFF;
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    if (Wire.available()) rdata =;
    return rdata;

  void printHex(int num) {    // function to convert DEC to HEX
      char tmp[16];
      char format[128];

      sprintf(format, "0x%%.%dX", 2);

      sprintf(tmp, format, num);

  void setup() 
    Wire.begin();         // initialise the connection
    Serial.begin(9600);   // set baud rate

  void loop()
    int StartAddr = 0;  // address to start read from 
    int EndAddr = 9;    // address to end read at

    String OutputString = "Reading EEPROM memory from address ";
    OutputString = OutputString + StartAddr + " to " + EndAddr;

    for (int addr = StartAddr; addr <= EndAddr; addr++) // memory begin and end address to read from
      byte b = i2c_eeprom_read_byte(0x50, addr); // read from eeprom
      printHex(b);  // print as HEX value
      Serial.print(" "); // values separator

    delay(2000);    // wait a bit before reading loop

You can either copy the whole code into text editor and save the file with a .ino extension or you can download the file from the link bellow. Save it into folder named eeprom_read because Arduino IDE requires the .ino files to each reside in it’s own directory named same as the file.

eeprom_read.ino (right-click and choose save target as)

Next: Code for writing to EEPROM

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.