Code for writing to EEPROM

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

//######### eeprom_write.ino ##########

#include <Wire.h> //I2C library

  void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {  // function for writing one byte to given address
    int rdata = data;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.write(rdata);
    Wire.endTransmission();
  }

  byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {              // function for reading one byte at given address
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available()) rdata = Wire.read();
    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);
      Serial.print(tmp);
  }

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

    int StartAddr = 1;  // address to start write from 
    int EndAddr = 6;    // address to end write at

    String OutputString = "Writing zeroes to EEPROM from address ";
    OutputString = OutputString + StartAddr + " to " + EndAddr;
    Serial.println(OutputString);

    for (int addr = StartAddr; addr <= EndAddr; addr++) // memory begin and end address to write to
    {
      i2c_eeprom_write_byte(0x50, addr, 0);  // write zeros to eeprom
      Serial.print("#");  // print progress
      delay(10);    // add a small delay
    }

    Serial.println("\nMemory written\n");
  }

  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;
    Serial.println(OutputString);

    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
    }

    Serial.print("\n");
    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_write because Arduino IDE requires the .ino files to each reside in it’s own directory named same as the file.

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

Next section: Testing

One thought on “Code for writing to EEPROM

  1. i want to comment on how to modify Panasonic Tv TX-L32E30B which is UK standard to
    TX-L32E30E by way editing the peaks eeprom.

Leave a Reply

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