24LC512- reading and writing of external EEPROM

#include <Wire.h>
#define eeprom 0x50 //defines the base address of the EEPROM
uint8_t data=8;
void setup(void){
Wire.begin(); //creates a Wire object
Serial.begin(9600);
unsigned int address = 0; //first address of the EEPROM
Serial.println(“We write the zip code data, a zip code in Arlington, Virginia!”);
for(address = 0; address< 7; address++)
writeEEPROM(eeprom, address,data); // Writes data to the EEPROM
for(address = 0; address< 7; address++) {
Serial.print(readEEPROM(eeprom, address));
}
}
void loop(){
//there’s nothing in the loop() function because we don’t want the arduino to repeatedly write the same thing to the EEPROM over and over. We just want a one-time write, so the loop() function is avoided with EEPROMs.
}
//defines the writeEEPROM function
void writeEEPROM(int deviceaddress, unsigned int eeaddress,uint8_t wdata ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); //writes the MSB
Wire.write((int)(eeaddress & 0xFF)); //writes the LSB
Wire.write(wdata);
Wire.endTransmission();
}
//defines the readEEPROM function
byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); //writes the MSB
Wire.write((int)(eeaddress & 0xFF)); //writes the LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,24);
if(Wire.available())
data = Wire.read();
return data;
}

With this program, I can only read and write upto 6 digits into the IC.I have attached the serial monitor output to the this thread for reference

Serial O/P
We write the zip code data, a zip code in Arlington, Virginia!
88888871

What is your largest data that you want to save into the EEPROM?

i need to write and read atleast 8 digit variable on it

8-digit -- which one of the following?

0x00000000 to 0xFFFFFFFF

or

00000000 to 99999999

The Decimal one

suryasiddhart:
The Decimal one

1. Remember that the decimal number (say, 99999999) that you see/submit for storage into the EEPROM is always get stored as binary which is: 5F5E0FF (28-bit). In Arduino, there is no data size such as 28-it; so, we will use the next size which is 32-bit and declare is as:

long x = 99999999;    //99999999 --> 0x05F5EOFF

2. As I2C is a byte oriented protocol where data exchange 1-byte at a time, we need to convert the 4-byte wide composite data item of Step-1 into 4 separate bytes using the following union data structure:

union
{
    long x;
    byte myData[4];
}data;

data.x = 99999999;

3. This is the connection diagram between UNO and EEPROM using I2C Bus.
512eeprom.png

5. This is the sketch (untested) to perform read/write operation on the EEPROM.

#include<Wire.h>
union
{
    long x;
    byte myData[4];
}data;

void setup()
{
   Serial.begin(9600);
   Wire.begin();                 //edit
   data.x = 99999999;     //
   //------------------------------
    Wire.beginTransmission(0x50);
    Wire.write(0x00);        //higher part of EEPROM address 0010 from which data will be stored
    Wire.write(0x10);        //lower part of EEPROM address
    for(byte i = 0; i<4; i++)
    {
       Wire.write(data.myData[i]);     //data bytes being queued in buffer
     }
    Wire.endTransmission();
    delay(5);                                 //5 ms data write delay
    //--------------read and siaply----------------------------
    Wire.beginTransmission(0x50);
    Wire.write(0x00);        //point EEPROM's beginning address
    Wire.write(0x10);        //lower part of EEPROM address
    Wire.endTransmission();
    //----------------------------------------------------------
     Wire.requestFrom(0x50, 4);    //4 -byte to read
     Serial.println(Wire.read(), HEX);    //shows: FF
     Serial.println(Wire.read(), HEX);    //shows: E0
     Serial.println(Wire.read(), HEX);    //shows: FF
     Serial.println(Wire.read(), HEX);    //shows: 05
}

void loop()
{

}

512eeprom.png

I just tried the coding yoou posted . It givess reply with full of Fs

Add the following line in the setup() function and then rerun the sketch. Report the result -- you should have been able to find this error in the program.

Wire.begin();
1 Like