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

Notes

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:

Console

Construction

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.

Breadboard

The Schematic

The Build

Credits and References

About LEAP#188 EEPROMArduino

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

Project Source on GitHub Return to the LEAP Catalog
About LEAP

LEAP is my personal collection of electronics projects - 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.

Projects are often inspired by things found wild on the net, or ideas from the many great electronics podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know or send a pull-request.

NOTE: For a while I included various scale modelling projects here too, but I've now split them off into a new repository: check out LittleModelArt if you are looking for these projects.

Project Gallery view the projects as an image gallery Notebook reference materials and other notes Follow the Blog follow projects and notes as they are published in your favourite feed reader