Problems storing in 24Lc512 EEPROM

Hi

I´m trying to use a 24LC512 EEPROM, using the library deveoped here: http://arduino.cc/playground/Main/LibraryForI2CEEPROM

The problem is that it seems I´not able to write in the EEPROM. Here is some code:

#include <I2C_eeprom.h>
.
.
.
I2C_eeprom ee(0x50);
.
.
.

for (byte k=1;k<=5;k++)  {
     if (k==1)  {
         ee.writeByte(1,1);    // write a "1" to the position 1 of the EEPPROM memory
     }
     updateDisplay(-1);  // this function updates a 7 segment display. "-1" just clears the display
     delay(1000);
     updateDisplay(ee.readByte(1));     // here the display shows the content of the cell 1 of the EEPROM
     delay(1000);
}

The result is that in the firts iteraction of the for loop, the displays shows “1”, but then it always shows “0”. It seems it is not really writing the EEPROM, and the ee.readByte function shows only some kind of temporal buffer.

I dont know what I´m doing wrong. I´ve double checked all the chip connections. Even the WP pin of the 24LC512 is set to GND (unprotect).

Any help will be very welcome.

Regards

Alvaro

The 24LC512 is an SPI EEPROM. The library appears to be for I2C EEPROMs. Sounds like a major disconnect.

hy@all

the 24LC512 is definitely a I2C EEPROM - see datasheet from microchip.com!
i’m not 100% sure - but i think all 24xx eeproms have I2C - and all 25xx types have a SPI interface.

i didn’t used this I2C_EEPROM lib by my own, but i know that it’s always tricky to set all bits right for another device than the lib was written for.

so i would suggest to write these some lines of code by your own with lower level library like the wire.h.
have a detailed look into the datasheet for setting all bits right - so the functions for writing and reading a single byte looks like this
(i took a short look into the datasheet and think this should be right)

    int hwAddress = 0b1010000; // wire lib uses 7bit addressing

    byte readByteFromEEPROM(unsigned long address) { // this works fine
      byte rdata;
      Wire.beginTransmission(hwAddress); // see in database for correct hw address and other bits
      Wire.send((byte)((address >> 8) & 0xFF)); // MSB of address
      Wire.send((byte)(address & 0xFF)); // LSB // of address
      Wire.endTransmission();
      Wire.requestFrom(hwAddress,1);
      while(!Wire.available()) {delay(1);}
      rdata = Wire.receive();
      return rdata;
    }

    void writeByteToEEPROM(unsigned long address, byte data) { // this works fine
      Wire.beginTransmission(hwAddress);
      Wire.send((byte)((address >> 8) & 0xFF)); // MSB
      Wire.send((byte)(address & 0xFF)); // LSB
      Wire.send(data);
      Wire.endTransmission();
      delay(5);
    }

test this functions with something like this:

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();
  
  Serial.println("STARTED WRITING");
  for(unsigned char i=0; i<128; i++) {
    writeByteToEEPROM(i,i);
  }
  delay(500);
  Serial.println("FINISHED WRITING");  
 
}

void loop() {
 Serial.println("START READING");
  for(long i=0; i<128; i++) {
    Serial.print("Read from address ");
    Serial.print(i);
    Serial.print(" - value = ");
    Serial.print(readByteFromEEPROM(i), DEC);
    Serial.println("");    
  }  
  Serial.println("FINISHED READING");
  Serial.println("Will read again in 5sec...");
  delay(5000);
}

i hope this will work for you - i tested it on 24AA1025 - this should work also on your eeprom.
your eeprom has just another operating voltage range and only half the size!

connect your A0,A1,A2 pins to ground!

greetz!

btw: wire.h library works fine - but i think there’s no possibility to check for acks with this lib.

Thanks so much for your answers. I´ll try your suggestions as I get home.

The 24LC512 is an I2C EEPROM, and the library described here: http://arduino.cc/playground/Main/LibraryForI2CEEPROM is suppossed to work with this chip. It says,

"2011-05-18 lib works with 24LC512 too"

That´s why I don´t understand why ee.readByte and ee.writeBye functions don´t work.

Thanks again.

Regards

First try the I2C scanner to check the address of the EEPROM.

Note that the returnvalue of the read is a byte 1, not a char "1" , ==> what does your 7segment display? bytes or chars?

@x0xbanderer

but i think there's no possibility to check for acks with this lib.

IIRC Wire.endTransmission(); has a return value that can be checked - maybe not all you want but at least a value seldom checked...

I can confirm that I'm in the middle of a project recently using both the 24LC256 and 24LC512 and I used the same library you used (and yes, of course they're I2C). In my testing it worked perfect and correctly handled the page boundary issues as described. Hopefully that settles any concern you had over the library or anything else.

I strongly suspect you have another logic problem elsewhere in your code as your EEPROM code looks correct. First, remove all other variables and try something like the code sample below in your setup() and use your serial monitoring to see if the value being read from your EEPROM is the one you expect. From the code below you should see the output of "178 (B2)" in your serial monitor for both values if it's correctly writing and reading.

byte addr = 1;
byte myValue = 178;  //Something you'll recognize
ee.writeByte(addr, myValue);
byte eeValue = ee.readByte(addr);

Serial.print("Original: ");
Serial.print(myValue, DEC);
Serial.print(" (0x");
Serial.print(myValue, HEX);
Serial.println(")");

Serial.print("Read: ");
Serial.print(eeValue, DEC);
Serial.print(" (0x");
Serial.print(eeValue, HEX);
Serial.println(")");

Note: Code uncompiled/untested

If you use the EEPROM very heavily I recommend writing yourself an entirely separate program that does nothing but dumping, displaying, parsing of your stored EEPROM values. Every time you have a question about whether something's being written exactly as you expect you can flash this program and use your serial monitoring to check. I made my even with a nice menu that gives me options to dump raw page contents or parse them into my expected uses and display very friendly output -- all over serial monitoring. Doing it as a separate program will prevent you from consuming your flash and SRAM space for all of the debugging code you only need occasionally and you can make it as robust and reusable as you like.