1

I'm pretty sure it's a minor mistake I've done, but I couldn't figure it out. I'm trying to use an M24256 EEPROM with Arduino Mega2560 via I2C protocol. The datasheet states that the address is 1010(E2)(E1)(E0)(R/W). I left all E pins unconnected, which means it should be A0 for writing and A1 for reading. I grounded the /WC pin so it's not write protected. I checked the device with the I2C scanner sketch and for some reason it was seen as 0x50. What's strange that I can read from it by using any address (I tried 0x50, 0xA1 and some random addresses), but I can't write to it - every time I get the default 0xFF as readback.

The code I tried:

#define memoryRead 0xA0
#define memoryWrite 0xA1
byte in=0x00;

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Serial.println("writing");
  for(int i=0;i<100;i++){
    Wire.beginTransmission(memoryWrite);
    Wire.write(byte(i >> 8));  // first the MSB (8-bits)
    Wire.write(byte(i)); 
    Wire.write(byte(i));
    Wire.endTransmission();
  }
  Serial.println("reading");

  Wire.beginTransmission(memoryWrite); //this should be resetting the pointer //to 0 if I got it well from the datasheet
  Wire.write(0x00);
  Wire.write(0x00);
  Wire.beginTransmission(memoryRead);
  for(int i=0;i<100;i++){
    in=Wire.read();
    Serial.println(in,HEX);

  }
  Wire.endTransmission();
  Serial.println("done");
}

void loop() {
}

What am I doing wrong? I double checked the wiring and the write protection, so it must be code.

Thanks in advance.

balu
  • 111
  • 1
  • 8
  • 2
    I would start your debugging process by writing code that successfully writes a single byte on the eeprom at your chosen memory address. – Robert Harvey Aug 07 '19 at 21:49

3 Answers3

3

I see several problems here:

  1. I left all E pins unconnected

    As stated on page 8 of the datasheet, "These inputs must be tied to VCC or VSS". Leaving them floating is not recommended.

  2. The Arduino Wire library uses 7-bit I2C addresses. If all E pins are grounded, the correct address to use for this part in this context will be 0x50 (1010000 binary). You do not need to use a separate address for read and write -- the last bit is inferred by the library.

  3. As noted by user3629249 (what a name!), the ~WC pin must be pulled low to permit writes.

  • The datasheet states that if left unconnected, they will be read as low. I connected them to ground to sure. I didn't know about the 7-bit addressing, thanks. – balu Aug 08 '19 at 09:11
1

regarding your statement: I grounded the /WC pin so it's not write protected

given EEPROM SPEC SHEET figure 8, the /WC MUST be high during the writing to the EEPROM. There fore, with (as you state) /WC is low, nothing will be written to the chip.

Your code really needs to pay attention to the ack bit that is transmitted by the EEPROM between each byte written. Until the ACK bit is received (by your code), the EEPROM is not ready for another byte of data. Per the spec sheet, that can be as much as 5ms after the byte of data is completely transmitted by your code

in general, unconnected pins will 'float' You cannot be sure that they will 'float' low. so, to be sure, E2 E1 and E0 should be tied to ground

Suggest tying /WC to one of the output pins on your arduino and write code to properly handle that pin value

The longest allowed write (and read) sequence is 64 bytes (all within the same 'page') so your code should be taking that into consideration

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • 2
    I think you misunderstood something in the datasheet, the sheet clearly states that if /WC is driven high, it will acknowledge the address bytes, but no modification will be done. – balu Aug 08 '19 at 09:13
  • @balu is right about this - I encountered the same problem – bem22 Dec 01 '21 at 18:15
0

I figured out how to get it working, I'm posting the code if anyone has a similar issue in the future:

#define memoryAddr 0x50
byte in=0x00;

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Serial.println("writing");

  Wire.beginTransmission(memoryAddr);
  Wire.write(0x00);  // first the MSB (8-bits)
  Wire.write(0x00);
  delay(10); 
  Wire.write(0x00); //data
  Wire.endTransmission();

  delay(10);
  Serial.println("reading");

  Wire.beginTransmission(memoryAddr); //writing the address to be read
  Wire.write(0x00);
  Wire.write(0x00);
  Wire.endTransmission();
  delay(10);
  Wire.beginTransmission(memoryAddr);
  Wire.requestFrom(memoryAddr,10,true);
  while(Wire.available()){
    in=Wire.read();
    Serial.println(in,HEX);

  }
  Wire.endTransmission();
  Serial.println("Done");
}

void loop() {
}
balu
  • 111
  • 1
  • 8