Using I2C EEPROM ST24C04 with Arduino IDE

Recently , I got hold of this EEPROM IC while browsing online , I searched the web and I found out that the IC, having part number ST24C04 supported I2C communication which gave me excitement thinking that I could make this work using an Arduino !

For those who are unaware, an EEPROM stands for Electrically Erasable Programmable Read Only Memory. It is non-volatile and can store information even after being powered off. Essentially it behaves like a tiny hard disk which opens the gate to many Arduino projects that requires data storage like logging configuration data, storing weather data , or even storing passwords ! Just imagine if you could use your own encryption techniques to store passwords of all your social media accounts in an EEPROM that does not lose the data or is prone to getting hacked. Guess what , it is definitely possible with this IC.

But firstly we have got to understand how the ST24C04 works and how to store data in it using the Arduino IDE. The ST24C04 is a 4KBit or 512 byte capable EEPROM which consists of various write modes like Byte Write, Page Write, MultiByte Write, Write Protection etc. These bytes are arranged in two blocks of 256 bytes.Well, you might say 512 byte is too small for anything. I would contradict you by saying this is not meant for storing music or videos but small bits of information like passwords etc. In this article , I will show you how to read or write from a single byte at a time . The pin diagram for the IC is below :

I will be using an Arduino Mega to communicate with this chip although any Arduino board will do the job. To communicate via the I2C protocol , only the SCL and SDA pins are required. Every I2C device has an 8 bit address that can be found from the datasheet. For this device the address consists of the device code 1010, followed by values of E1 and E2 which are 0 in this case. The next bit defines the block out of the two available. The last bit is the read/write bit. While using Arduino IDE for I2C communication for read/ write operations, the Wire Library is used , which handles the read/write bit. Hence, we only have to mention the 7 bit address which in this case is 1010000 (we are working with the 0th block hence the block bit is 0). The corresponding hex value is 0x50. Using this address , we can work on the first 256 bytes . For the next 256 bytes, we will work with block 1, hence the address would be 0x51. Now, we connect the circuit like it is shown below :

The mode pin is used for the various modes like Page Write, MultiByte Write etc, which we will not use hence we connect it to ground. We will be using two pull up resistors each 6.5K ohm to pull the SCL and SDA pins to supply voltage (as per I2C protocol). The PRE pin is left floating as the Write Protect feature won’t be shown here.

I will be writing /reading from the first address of block 0 , this will just be a reference for working on the other addresses of the chip. The Arduino code is shown below:

// Adapted from Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device known as the 24C04WP EEPROM IC
// Created 18 May 2019

#include <Wire.h>

void setup() {                                                                     
  Serial.begin(9600);                                                              
  Wire.begin(); // join i2c bus (address optional for master)

//Writing a byte   
Wire.beginTransmission(0x50); //slave address
  Wire.write(0);               // address of the 1 byte memory block to write to. 256 such 
                                  blocks can be accessed with addresses from 0 to 255.  
  Wire.write(235);              // sends one byte
  Wire.endTransmission();    // stop transmitting
  delay(5);

//Reading the written byte
  Wire.beginTransmission(0x50); 
  Wire.write(0);        //address of the byte  to read from
  Wire.endTransmission();
  delay(5);

 Wire.requestFrom(0x50,1); // gets the value from the address mentioned above
 delay(10);
 if(Wire.available()){
 byte data = Wire.read();
 Serial.println(data);} 
}
void loop() {}

To start I2C write process, the address of the EEPROM which is 0x50, is specified , followed by the address to write to . This is done with the command Wire.write(0). For each block the address can be from 0 to 255. This is followed by the data to write to the address , which is 235 in this case. Remember the value can only be a 8 bit value from 0 – 255.

The read process is similar to the write process, except we only mention the address to read from. Then, to read the value , the requestFrom() function is used, which takes the slave address and the number of bytes to read as arguments . The byte that is read, is then displayed onto the Serial Monitor .

If you follow the wiring precisely and run this code , you will see 235 on the Serial Monitor on the Arduino IDE. Similarly, you can program the other 511 bytes of the EEPROM IC as well. Why not try to create your own password storage device like I mentioned before?

I will present its other features on another article and maybe make the password storage device along with super cool encryption features ! Till then , you explore this chip even more with its datasheet . Cheers πŸ™‚

7 thoughts on “Using I2C EEPROM ST24C04 with Arduino IDE

  1. Pingback: Back To Basics With An Arduino And An EEPROM – Ans Info

  2. Oops! Your two 6.5K pullups are shown in the schematic to be connected to GND rather than 3V3. You say “pullup” in your text, and that is correct according to the I2C protocol. ‘Twould be good were you to correct your schematic!

    Like

  3. Pingback: BACK TO BASICS WITH AN ARDUINO AND AN EEPROM -Use Arduino for Projects

  4. Pingback: Arduino on a Breadboard – With No Additional Components – Electronics with TheMagicSmoke

  5. Moha Elec

    It’s a good idea to program an EEPROM with Arduino ! But somewhere I have read or saw that we can use this EEPROM IC (24C04 or 24CXX) to replace combinational logic ? How can we do it ?
    Thank you in advance for your answer !!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s