Fork me on GitHub

Project Notes

#188 Basic AT24C02 EEPROM Read/Write

Basic read/write operations to external EEPROM (AT24C02) with the Wire library

The Build


I have some AT24C02BN Two-wire Serial EEPROM chips with a whopping capacity of 2K! (256 x 8) That’s not a whole lot more than the onboard EEPROM in an Arduino, but it does have the advantage of being external:

  • it can stay in place even if the Arduino is exchanged
  • multiple devices can share access

There are a few libraries available that encapsulate the interaction with external EEPROM. All that I’ve seen use the Wire library under the covers. Some like the AH_24Cxx library seem to do a really good job of providing compatibility across the whole range of EEPROM chips and protecting/avoiding issues with page addressing etc - things you otherwise need to take care of youself.

For this simple test of the basic read and write capability, I’m going to use the Wire library directly. That means the code is necessarily only good for chips that work in the same way as the AT24C02 (that basically means nothing over 16k in memory).

So first as always: read the datasheet. And I immediately note Not Recommended for New Design ;-) It looks like AT24C02C and AT24C02D variants are current. No matter, AT24C02BN is good enough for me now. It seems the later version don’t change the basic behaviour.

Device Addressing

Each AT24C02 has an 8-bit device address word used in communications.

The device address word consists of a mandatory one, zero sequence for the first four most significant bits. This is common to all the EEPROM devices.

The next 3 bits are the A2, A1 and A0 device address bits corresponding to the hard-wired input pins.

MSB             LSB
1 0 1 0 A2 A1 A0 R/W

The Wire library takes care of the R/W bit and in fact just expects the 7-bit address (shifted right 1 bit).

So if A2=0, A1=0, A0=1, the address expected by the Wire library would be 0x51.

Memory Addressing

With such a small memory size, that’s easy here - a single 8-bit word is used for the address. Note that for chips with larger memory, the address size does increase (and this code wont work).

The Test Program

The code here just runs a simple counter:

  • on startup, it loads last value from EEPROM, else initialises to 0

Every second, it:

  • increments the counter
  • echoes the new value to the Serial console
  • saves the new value in EEPROM
  • reads the value back from EEPROM and echos this value to the console

So nothing real exciting, just proving it works. This is what it looks like in the coneols:



Note that I have surface mount AT24C02BN chips, so I used a SO-16 to DIP breadboard adapter to mount the chips for testing purposes.


The Schematic

The Build

Credits and References

Project Source on GitHub Project Gallery Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is just my personal collection of projects. Two main themes have emerged in recent years, sometimes combined:

  • electronics - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting
  • scale modelling - I caught the bug after deciding to build a Harrier during covid to demonstrate an electronic jet engine simulation. Let the fun begin..
To be honest, I haven't quite figured out if these two interests belong in the same GitHub repo or not. But for now - they are all here!

Projects are often inspired by things found wild on the net, or ideas from the many great electronics and scale modelling podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know (or send a PR!). See the individual projects for credits where due.