24AA02 EEPROM Problem

Hello!

I use a 24AA02 EEPROM, here is the Datasheet LINK

I have this code for write to eeprom:

void writeEEPROM(int deviceaddress,  int eeaddress, byte data ) 
{
  Serial.print("Adress is: ");  
  Serial.println(eeaddress);
  Wire.beginTransmission(deviceaddress);   // I2C Bus Address
  Wire.write(10100000);                         // Control byte for WRITE
  Wire.write(eeaddress);                        // Register Address              
  Wire.write(data);                               // Data byte
  Wire.endTransmission();

  delay(5);
}

And this code for read from eeprom

void readEEPROM(int deviceaddress, byte eeaddress ) 
{
  byte rdata = 0xFF;

  Wire.beginTransmission(deviceaddress);
  Wire.write(10100001);
  Wire.endTransmission();

  Wire.requestFrom(deviceaddress,2);


    while (Wire.available()){
    Serial.print("EEPROM has Read: ");
    Serial.println(Wire.read());
    }

  
}

For testing, i run this:

writeEEPROM(STORAGE_ADDRESS, 0x01, 5);
  writeEEPROM(STORAGE_ADDRESS, 0x02, 6);
  
  readEEPROM(STORAGE_ADDRESS,1);
  readEEPROM2(STORAGE_ADDRESS,1);

The Serial Monitor gives me the following output:

Adress is: 1
Adress is: 2
EEPROM has Read: 6
EEPROM has Read: 255

I would expect to see 5 and then 6, not 6 and 255

My first thought, was that the register-pointer is at the wrong position, so i added another function:

void readEEPROM2(int deviceaddress, byte eeaddress ) 
{
  byte rdata = 0xFF;
  


  Wire.beginTransmission(deviceaddress);
  Wire.write(10100000);
  Wire.write(eeaddress); // LSB 
  Wire.endTransmission();

  Wire.beginTransmission(deviceaddress);
  Wire.write(10100001);
  Wire.endTransmission();

  Wire.requestFrom(deviceaddress,2);

  while (Wire.available()){
    Serial.print("EEPROM-Function2 has Read: ");
    Serial.println(Wire.read());
    }
    
}

But i get no Output from it, it looks like Wire is not available.

Does anybody knows what i do wrong?

Wire.write(10100001);?

AWOL: Wire.write(10100001);?

Yes, thats the command for Read

0101 = Control Code 000 = "Block Adress" - Don't Care 0 or 1 = Read = 1, Write = 0

At least thats what the datasheet says on page 7.

Look again, carefully. That's a very big number.

AWOL: Look again, carefully. That's a very big number.

Sorry...but i don't get it?

If something is wrong with that, why does the writing works?

Could you tell me what u mean?

Did you miss out a "0b"?

It would be so nice to see ALL your code like it says in How to post code properly.

You are addressing the EEPROM in entirely the wrong way. The device address is specified to the Wire library as the 7 bit address of the EEPROM without the Read/Write bit. When you are only using one of these EEPROMs you would use the address 0b1010000 = 0x50 (you could add more EEPROMS with addresses from 0x51 up to 0x57). This is the address you use in the call to beginTransmission. The next byte that you write is the address in the EEPROM (the storage address) that you wish to read or write.

Writing to the EEPROM would be like this where deviceaaddress is 0x50 (or 0b1010000):

 Wire.beginTransmission(deviceaddress);   // I2C Bus Address
  Wire.write(eeaddress);                        // Register Address             
  Wire.write(data);                               // Data byte
  Wire.endTransmission();

Reading:

 Wire.beginTransmission(deviceaddress);   // I2C Bus Address
  Wire.write(eeaddress);                        // Register Address             
  Wire.write(data);                               // Data byte
  Wire.endTransmission();

Wire.requestFrom(deviceaddress,2); [/code]

Pete

Thanks! This works

Datasheet was a little confusing for me.