EEPROM 24LC256— Reading and Writing Arduino Sketch
How to Save Integers and Longs to EEPROM — NaveTECH & UNIR Series — Episode # 11
Why am I going to need an EEPROM in my project?
Use 24LC256, 256Kb I2C compatible 2-wire Serial EEPROM.
EEPROM (Electrically Erasable Programmable Read-Only Memory) can be a valuable component in many projects for several reasons:
Non-volatile Memory
: EEPROM retains its stored data even when power is turned off. This feature is useful for storing critical data that needs to be preserved across power cycles, such as configuration settings, calibration values, user preferences, saving the program’s critical variables states, or historical data logs;Limited Write Cycles
: Unlike RAM (Random Access Memory), which can be written to and erased an unlimited number of times, EEPROM has a finite number of write cycles. However, EEPROM can typically endure many thousands of write cycles before degradation, making it suitable for storing data that is infrequently modified. External EEPROM, not internal, offers 10x the lifespan. Internal: 100,000 resets; External: 1,000,000 resets;Data Persistence
: If your project requires storing user-specific settings or data that needs to persist between power cycles, EEPROM is an excellent solution. It allows your device to remember user preferences or configurations without the need for external storage devices;Space Efficiency
: EEPROM is often integrated into microcontrollers, providing a compact and efficient solution for storing small amounts of non-volatile data directly on the device. This can be advantageous in space-constrained applications or where external storage devices are impractical; but remember, Arduino UNO: 1024 bytes EEPROM, less than 24LC256’s 32,768 bytes. Significant difference in memory capacity between the two (32x less).Reliability
: EEPROM technology offers reliable data storage with low power consumption and high endurance. This makes it suitable for critical applications where data integrity is paramount.
Common uses of EEPROM in projects include storing:
- Configuration settings (e.g., display preferences, communication parameters)
- Calibration data (e.g., sensor offsets, calibration coefficients)
- User profiles or preferences
- Historical data logs
- Security keys or cryptographic material
Overall, EEPROM is a valuable resource in projects where non-volatile data storage is required, offering reliability, data persistence, and space efficiency.
Let’s practice:
1#Step — Get your hardware like this:
2#Step — Code I — Reading and writing bytes to and from EEPROM.
Upload this code:
This code snippet is for interacting with a 24LC256 EEPROM chip using an Arduino board via the I2C (Inter-Integrated Circuit) communication protocol. Let’s break down the code and its functionality:
- Include
Libraries and Define Constants
: The code begins by including theWire.h
library, which provides functions for I2C communication; It defines a constantdisk1
with the address0x50
, which is the default address of the 24LC256 EEPROM chip. - I2C Scanner (
scani2c
function): Thescani2c
function scans the I2C bus for devices and prints their addresses; It iterates through possible I2C addresses from1
to127
and attempts to communicate with each address; If a device acknowledges communication (returns no error), it prints the device address (0x50); If there’s an unknown error, it prints an error message; Finally, it prints the number of devices found or a message indicating no devices were found; - EEPROM Write Function (
EEPROMWrite
): This function writes a byte of data to a specified address in the EEPROM; It begins an I2C transmission to the specified EEPROM address (disk
); It writes the high byte and low byte of the EEPROM address where data will be written; It writes the data byte (eepromdata
) to the specified address; It ends the I2C transmission and includes a delay to allow time for the EEPROM to complete the write operation; - EEPROM Read Function (
EEPROMRead
): This function reads a specified number of bytes from the EEPROM starting at a given address. It checks if the number of bytes requested is within the limit (32 bytes in this case); It begins an I2C transmission to the specified EEPROM address (disk
); It writes the high byte and low byte of the starting EEPROM address. It ends the transmission and requests the specified number of bytes from the EEPROM; It returns1
if the operation was successful or0
if there was an error (e.g., too many bytes requested); - Setup Function (
setup
): Initializes serial communication at a baud rate of 9600; CallsWire.begin()
to initialize the I2C communication; Callsscani2c()
to scan for I2C devices; Performs a read operation on the EEPROM, reading 32 bytes at a time, and prints the received bytes; Writes the first 5 bytes (0 to 4) to the EEPROM; Reads back the first 5 bytes from the EEPROM and prints them; - Loop Function (
loop
): Theloop
function is empty, as there's no continuous operation required in this example.
Overall, this code demonstrates how to interact with a 24LC256 EEPROM chip using Arduino and perform read and write operations on it via the I2C protocol.
3#Step — Code II— Now let’s get version 2! Now we address Integers and Longs:
This main function, EEPROMReadLong
, reads a 32-bit (4-byte) long integer from an EEPROM device connected to an Arduino via the I2C protocol. Let's break down how it works:
- Function Signature:
long EEPR OMReadLong(int disk, int startdatareadaddress)
: This function takes two parameters:disk
, which specifies the address of the EEPROM device on the I2C bus, andstartdatareadaddress
, which indicates the starting address in the EEPROM from where the long integer will be read; - Variable Declaration:
byte firstByte, secondByte, thirdByte, forthByte
: These variables store the individual bytes read from the EEPROM;long returnLong
: This variable stores the final long integer value constructed from the four bytes read from the EEPROM; - I2C Transmission:
Wire.beginTransmission(disk1)
: Begins a transmission to the specified EEPROM device address (disk
).Wire.write(startdatareadaddress >> 8)
,Wire.write(startdatareadaddress & 0xFF)
: Writes the high and low bytes of thestartdatareadaddress
to specify the address from where data will be read.Wire.endTransmission()
: Ends the I2C transmission; - Request Bytes:
Wire.requestFrom(disk, 4)
: Requests 4 bytes from the EEPROM device. Since we're reading a long integer (4 bytes), we request 4 bytes; - Read Bytes:
firstByte = Wire.read()
,secondByte = Wire.read()
,thirdByte = Wire.read()
,forthByte = Wire.read()
: Reads each byte from the EEPROM and stores it in the respective variables; The order of bytes is significant here; the most significant byte (MSB) is read first. Each byte read is also printed to the serial monitor for debugging purposes; - Construct Long Integer:
returnLong = (firstByte * 16777216) +(secondByte * 65536) +(thirdByte * 256) + forthByte
: Constructs the long integer by combining the four bytes read from the EEPROM; The multiplication factors (16777216, 65536, 256) represent the positional value of each byte within the 32-bit integer; The long integer value is calculated by multiplying each byte by its corresponding positional value and summing them together; - Print and Return:
Serial.println(returnLong)
: Prints the constructed long integer value to the serial monitor for debugging;return returnLong
: Returns the constructed long integer value from the function.
Overall, this function facilitates reading a 32-bit long integer from an EEPROM device and reconstructs it into its proper data type for further processing in Arduino sketches.
I’m glad you found the explanation helpful. If you ever need more assistance or have further questions about Arduino or any other topic, feel free to ask. Have a great time exploring the Arduino world, and I’ll be here whenever you need assistance. Goodbye, and take care!
Share the Digital Town page, inspire, and recommend this awesome Arduino series to others. Feel free to do so. Sharing knowledge and resources is a wonderful way to support the learning community.
👉GitHub (Projects #42 and #43)
Credits & References:
Arduino UNO 24LC256 External EEPROM by Digital Town
24LC256 — 256Kb I2C compatible 2-wire Serial EEPROM by microchip.com
Related Posts:
02#Ep — EEPROM — PIC 18 EEPROM Complete Lib — A Mockup for a Little EEPROM Lib — PIC Unit #11
01#Ep —HLVD — PIC 18 HLVD — Voltage Detection Intro — What results from a catastrophic error on an MC?
01#Ep I2 — I2C— 18 I2C — External EEPROM — Interfacing external EEPROM with PIC Microcontroller