EXTERNAL EEPROM 24LC256

I am trying to write to a external EEPROM 24LC256 chip and I can get the code to write and read from the chip but when I repower or reset the ARDUINO the values written to the chip reset to zero.

I am using both PRO MINI and MEGA boards.

Attached is complete code.

Help appreciated.

Bill

WEATHER_POND_two_pullup_eeprom_MEGAjunk.ino (5.69 KB)

Have you tried running an existing EEPROM library example sketch to verify the hardware?
Do you have a test sketch with only EEPROM functions so we don't have to plow through all the Weather pond "MEGAjunk"?

Well there's an obvious bug here:

void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) //for 24LC256 4 bytes
{
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddresspage >> 8)); // MSB
    Wire.write((int)(eeaddresspage & 0xFF)); // LSB
    byte four =(data[0] & 0xFF);
    byte three =((data[1]>>8) & 0xFF);  // ZERO!   no need to shift if indexing a byte array
    byte two = ((data[2]>>16) & 0xFF);  // ZERO!!
    byte one =((data[3]>>24) & 0xFF);  //ZERO!!!

    Wire.write(four);
    Wire.write(three);
    Wire.write(two);
    Wire.write(one);
    Wire.endTransmission();
}

Also why is the length argument being ignored? Is that correct?

Why not loop to write the data:

  for (byte i = 0 ; i < length ; i++)
    Wire.write (data [i]) ;

Thanks for reply.

I created new code based on the extEEPROM library(//GitHub - PaoloP74/extEEPROM: Arduino library to support external I2C EEPROMs.) and deleted all unnecessay coding.

I am trying to make a counter that writes total counts to External EEPROM but my code sets the EEPROM to zero when I reset the ARDUINO(PRO MINI or MEGA) or repower the board.

I know the hardware works because I get the error "extEEPROM.begin() failed, status = 2" when I remove the 24LC256 EEPROM chip.

Pulling the RAIN pin(#22) low will increment total COUNTS by one and pulling the reset_rain pin(#26) low will set total COUNTS to whatever I have in reset_counter().

Help appreciated.

EEPROM_COUNTER.ino (1.58 KB)

The extEEPROM is found at GitHub - JChristensen/extEEPROM: Arduino library to support external I2C EEPROMs.

Have you tried running an existing EEPROM library example sketch to verify the hardware?

YES;

I used the eepromTest sketch that is part of the extEEPROM library. It works fine.

Bill

williamlynn:
YES;

I used the eepromTest sketch that is part of the extEEPROM library. It works fine.

Bill

Build upon that step by step instead of trying to unravel what you've already done. By the way, please post your code in code tags, don't post as an attachment.

The library example sketch appears to be designed for a different EEPROM. You must have made changes. Can you please explain what those were? Are you sure that 0x50 is the right I2C address?

but my code sets the EEPROM to zero when I reset the ARDUINO(PRO MINI or MEGA) or repower the boar

The sketch you attached never reads from the eeprom so how do you know this?.

Your syntax for the write is not correct. totalCounts is an integer.

// byte i2cStatA = POND_eep.write(0,totalCOUNTS,4);
byte i2cStatA = POND_eep.write(0, &totalCOUNTS, 4);

Why are you using 4 bytes instead of 2? Are you on a 32 bit platform?

// sketch for EEPROM COUNTER

#include <Wire.h>
#include<extEEPROM.h>;

#define disk1 0x50                 //address of 24LC256

int RAIN = 22;                    //counter input pin
int reset_rain = 26;
int reset_rain_counter;
int COUNT;
int lastCOUNT;
int totalCOUNTS;
int address;                      //byte address on 24LC256

extEEPROM POND_eep(kbits_256, 1, 64, disk1);

void setup() {
  Serial.begin (9600);
  Serial1.begin (4800);
  Wire.begin();
   uint8_t POND_eepStatus = POND_eep.begin();
  if (POND_eepStatus)
      {
      Serial.print(F("extEEPROM.begin() failed, status = "));
      Serial.println(POND_eepStatus);
      while (1);
      }
  pinMode(RAIN , INPUT_PULLUP);
  pinMode(reset_rain , INPUT_PULLUP);

  Serial.println("STARTING");
  Serial.print("totalCOUNTs = ");
  Serial.println(totalCOUNTS);
}

void loop() 
  {
  reset_rain_counter = digitalRead(reset_rain);
  if(reset_rain_counter == LOW)
  {
    Serial.println(reset_rain_counter);
    reset_counter();
  }
  COUNT = digitalRead(RAIN);
  if(COUNT != lastCOUNT)
  {
    if(COUNT == LOW)
    {
      totalCOUNTS = totalCOUNTS + 1;
      Serial.print("TOTAL COUNTS = ");
      Serial.println(totalCOUNTS);
      byte i2cStatA = POND_eep.write(0,totalCOUNTS,4);
      if (i2cStatA !=0)
      {
        if(i2cStatA == EEPROM_ADDR_ERR)
        {
          Serial.println("error");
          while(1);
        }
      }
      delay(200);
    }
    else
    {
    }
    delay(50);
  }
  lastCOUNT = COUNT;
}

void reset_counter()
{
  Serial.println("AT RESET COUNTER");
  totalCOUNTS = 405;
  POND_eep.write(0, totalCOUNTS, 4);
  delay(2000);
}