Wire.h for Uno changed?

I'm confused. I have several programs to test external EEProms and now they act as if they don't get written.`

type or paste code here

`#include <Wire.h> 

#define eeprom 0x50 //defines the base address of the EEPROM

void setup()  {
  Wire.begin(); //creates a Wire object
  Serial.begin(115200); 

  unsigned int address = 0; //first address of the EEPROM
  Serial.println("We write the zip code 22222, a zip code");
  for(address = 0; address< 5; address++) 
    writeEEPROM(eeprom, address, '2'); // Writes 22222 to the EEPROM

  for(address = 0; address< 5; address++) {
    Serial.print(readEEPROM(eeprom, address), HEX); 
    }
  }

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, byte data ) {
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));      //writes the MSB
  Wire.write((int)(eeaddress & 0xFF));    //writes the LSB
  Wire.write(data);
  Wire.endTransmission();
  }

//defines the readEEPROM function
byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
  byte rdata = 0xFF;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));      //writes the MSB
  Wire.write((int)(eeaddress & 0xFF));    //writes the LSB
  Wire.endTransmission();
  Wire.requestFrom(deviceaddress,1);
  if (Wire.available()) 
    rdata = Wire.read();
  return rdata;
  }

This now outputs FFFFFFFFFF Looked at it with pulseview and it looks like it's not writing. I may be wrong, but don't understand.

I've tried several 24LCxx chips. 24lc256, 24lc512, and 24lc16 with the same results. I will admit it was quite awhile ago that I previously ran this.
Hope I posted the code ok.

You can get an inexpensive Logic Analyzer from Amazon (< $20) and download free software. Use it to monitor what's really happening on the SDC / SCL lines. The software will decode the I2C protocol for you. Do you see valid I2C transactions? Do they comply with the device's specifications and protocol?

Are the EEPROMs detected by the I2C scanner sketch?

Do you have pull-up resistors on SDA and SCL?

can you check the value returned by Wire.endTransmission()?

yes, I have one and tried using pulseview. I see the transactions, but syncing it not too good. I can see it addressing the write, but not sending the write data (if I am synced correctly) I should mention that I tried Drone workshop's eeprom program and it ran fine. Also this one ran fine the last time I ran it (probably about a year ago).

yes

How?

It returns a status code

  • Returns

  • 0: success.

  • 1: data too long to fit in transmit buffer.

  • 2: received NACK on transmit of address.

  • 3: received NACK on transmit of data.

  • 4: other error.

  • 5: timeout

ok, but how would you see it? Or print it out?

Well something like

int code = Wire.endTransmission();
And then a switch on the code and for each possible value a Serial.println()

ok, will try tomorrow. Getting late.

On write: 0,0,0,0,0,
On read: 0,2,2,2,2 (failing reads??)

added a delay(5) in both for loops now reads
On write: 0,0,0,0,0
On read: 0,0,0,0,0
but still returns 0xFF
not getting wire.available

First one is OK, the rest are not.

May be you can force to keep the bus locked for you

After sleeping on it, it hit me this morning. It's one of those simple things that's so easy to overlook when you're looking for something more complicated to be wrong.

You need to have a delay at the end of the writeEEPROM function in order for the EEPROM to actually do the write.

//defines the writeEEPROM function
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));      //writes the MSB
  Wire.write((int)(eeaddress & 0xFF));    //writes the LSB
  Wire.write(data);
  Wire.endTransmission();
  delay(5); // <-- allow time for EEPROM to write
  }

yep, I put the delay in the wrong place. it works now.
Thank you all

So those did not work in the past either, right?

I remember of a library that actually tests if it can talk to the eeprom to see if the previous operation is completed.

I didn't write myself a note saying it did not run, so I think it did when I first tried it. And....I was doing this on a 24lc256. :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.