24C256 EEPROM does not write and read?

I am using either Nano and Uno with AMTEL 24C256 with 4.7K pull up on SDA and SCL pins.
The I2C scanner did detected shows 0X50 as very good.
A0, A1, A2, A3, and WP connected to all GND.

so here my IDE:

#include <Wire.h>

#define EEPROM_I2C_ADDRESS_0 0x50


int EEPROM_I2C_ADDRESS = NULL;
int i=0;
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop()
{
  Serial.println();
  Serial.println("Written into EEPROM...");
  for(i=0;i<94;i++)
  {
    if(i<256)
    {
      EEPROM_I2C_ADDRESS = 0x50;
      writeAT24(i, 33+i);
      Serial.print(char(33+i));
    }
  }
  delay(1000);
  Serial.println();
  Serial.println("Reading Data from EEPROM...");
  for(i=0;i<94;i++)
  {
    if(i<256)
    {
      EEPROM_I2C_ADDRESS = 0x50;
      Serial.print((char)readAT24(i));
    }
  }
  delay(1000);
  while(1)
  {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
  }

}


// Function to write to EEPROOM
void writeAT24(byte dataAddress, byte dataVal)
{
  Wire.beginTransmission(EEPROM_I2C_ADDRESS);

  Wire.write(dataAddress);
  Wire.write(dataVal);
  Wire.endTransmission();

  delay(5);
}

// Function to read from EEPROM
byte readAT24(byte dataAddress)
{
  byte readData = NULL;
  Wire.beginTransmission(EEPROM_I2C_ADDRESS);
  Wire.write(dataAddress);
  Wire.endTransmission();

  delay(5);
  Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);
  //delay(1);

  if(Wire.available())
  {
    readData =  Wire.read();
  }

  return readData;
}

The Serial Monitor show:

Written into EEPROM...
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
Reading Data from EEPROM...
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

Seem write and read not working... Did I missed something?

You are only sending one byte for the EEPROM address you want to read and write to. You need to send two bytes, that is a 16 bit number that contains the address you want to read or write to.

Read the chip's data sheet.

1 Like

Hi, @RAM_ELECTRONICS

Did you Google;

24C256 arduino

This might help. Take a good look at the schematic.

Can you please post a circuit diagram of your project?
An image of a hand draw circuit would be goood.

Tom... :smiley: :+1: :coffee: :australia:

Ummm Okay, can you give me code for example? I tried many way from the online no luck.
also I did replace chip 3 times still the same.
I tried this code and no luck:

Wire.write((int)(eeaddress >> 8));   // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB

Here exactly what I have:


but I added two of 4.7K ohms for pull up on SDA and SCL.

like this:

BUT I am sure that something missing the coding.

Hi,
Where are your 10K or 4K7 pull up resistors on the I2C lines in your picture?

Tom.. :smiley: :+1: :coffee: :australia:

That picture is not mine. I use link from someone else.
here again exactly what I do:

Hi,
Thanks for the image.
Although the A0, A1 and A2 are internally pulled to gnd, the data sheet suggests in most applications you hard wire HIGH or LOW.
microchip

Also can I suggest a 0.1uF cap across the Vcc and VSS pins.

Tom... :smiley: :+1: :coffee: :australia:

Yes I told you in the first reply, you are only using one byte as an address

Anything you send to the read or write functions, he address you want to read from or write to in your memory gets converted into a byte.

1 Like

Hi, @RAM_ELECTRONICS

Did you Google and look at other codes?

Tom... :smiley: :+1: :coffee: :australia:

Yes I tried tons with different codes from online NOT HELPS... but I see the mostly they use "LC" not "C" like 24LC256.

I am going try with grumpty_Mike's comments. let see

Okay, how can I do fix this? I tried many ways from online... but I do not understand the online with you tube have same of mine, they are working fine but not me.. I copied code from their.

The first parameter of this function, which is the address of the EEPROM you want to write from is defined as a byte. So what ever you pass into this function will be converted into a byte. So you pass an int into this function and it is converted into a byte!

This is bad because you only ever send a byte as the address. The EEPROM chip needs the following data as defined in the data sheet I told you to read and didn't.

The first word address has a * on the most significant bit, which means that the first word address is the most significant byte of the two byte address you must send.

So the first thing you must do is change the function definition of both read and write functions to a

void writeAT24(int dataAddress, byte dataVal)

Ah, I got it now... I use BOTH "int" as void writeAT24(int dataAddress, int dataVal). it's worked. thanks!

Here what's it working perfectly:

#include <Wire.h>

#define EEPROM_I2C_ADDRESS_0 0x50


int EEPROM_I2C_ADDRESS = 0x50;
int i=0;
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop()
{
  Serial.println();
  Serial.println("Written into EEPROM...");
  for(i=0;i<94;i++)
  {
    if(i<256)
    {
      EEPROM_I2C_ADDRESS = 0x50;
      writeAT24(i, 33+i);
      Serial.print(char(33+i));
    }
  }
  delay(1000);
  Serial.println();
  Serial.println("Reading Data from EEPROM...");
  for(i=0;i<94;i++)
  {
    if(i<256)
    {
      EEPROM_I2C_ADDRESS = 0x50;
      Serial.print((char)readAT24(i));
    }
  }
  delay(1000);
  while(1)
  {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
  }

}


// Function to write to EEPROOM
void writeAT24(int dataAddress, int dataVal)
{
  Wire.beginTransmission(EEPROM_I2C_ADDRESS);

  Wire.write((int)(dataAddress >> 8));   // MSB
  Wire.write((int)(dataAddress & 0xFF)); // LSB
  Wire.write(dataVal);
  Wire.endTransmission();

  delay(5);
}

// Function to read from EEPROM
byte readAT24(int dataAddress)
{
  byte readData = 0;
  Wire.beginTransmission(EEPROM_I2C_ADDRESS);
  Wire.write((int)(dataAddress >> 8));   // MSB
  Wire.write((int)(dataAddress & 0xFF)); // LSB
  Wire.endTransmission();

  delay(5);
  Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);
  //delay(1);

  if(Wire.available())
  {
    readData =  Wire.read();
  }

  return readData;
}
1 Like

Hi,
Great that you have it working.
Thanks for posting your working code.

Can you now tick the Solved box so it will show as a fixed problem for anyone else with a similar problem

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

I am trying to find the sloved box where is it?

Ah got it..

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