### Basic Setup and Initialization Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Demonstrates how to include the library, create an EEPROM object, initialize the I2C bus, and verify communication with the chip. ```APIDOC ## Basic Setup and Initialization ### Description Include the header file for your specific chip type, create a chip object with the I2C address, and initialize the Wire interface before use. ### Method N/A (Setup code) ### Endpoint N/A ### Parameters N/A ### Request Example ```cpp #include // Create EEPROM object at address 0 (0x50) AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Serial.begin(115200); // Initialize the I2C bus - required before any EEPROM operations Wire.begin(); // Verify communication with the chip uint8_t testValue = eprom.read(0); if (eprom.getLastError() == 0) { Serial.println("EEPROM connected successfully"); Serial.print("Memory size: "); Serial.print(eprom.length()); Serial.println(" bytes"); } else { Serial.println("Error: EEPROM not found at specified address"); } } void loop() { // Your application code } ``` ### Response N/A (Setup code) #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Basic Setup and Initialization for AT24C256 Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Include the appropriate header, create an EEPROM object with the I2C address, and initialize the Wire interface. This example demonstrates verifying communication and checking the memory size. ```cpp #include // Create EEPROM object at address 0 (0x50) AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Serial.begin(115200); // Initialize the I2C bus - required before any EEPROM operations Wire.begin(); // Verify communication with the chip uint8_t testValue = eprom.read(0); if (eprom.getLastError() == 0) { Serial.println("EEPROM connected successfully"); Serial.print("Memory size: "); Serial.print(eprom.length()); Serial.println(" bytes"); } else { Serial.println("Error: EEPROM not found at specified address"); } } void loop() { // Your application code } ``` -------------------------------- ### Setup AT24C256 EEPROM Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md Include the chip header, create an EEPROM object with its address, and initialize the Wire interface. This is necessary before performing any EEPROM operations. ```C++ #include AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Wire.begin(); uint8_t byte = eprom.read(0); } ``` -------------------------------- ### get() - Read Any Data Type Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Template method that reads any basic type or struct from memory starting at the specified address. Returns a reference to the populated variable. ```APIDOC ## get(address, data) ### Description Reads any basic type or struct from memory starting at the specified address. Returns a reference to the populated variable. ### Parameters - **address** (uint16_t) - Required - The memory address to start reading from. - **data** (T) - Required - The variable or struct to populate with data read from memory. ``` -------------------------------- ### Connect Multiple EEPROM Chips Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md Instantiate multiple EEPROM objects for different chips, specifying their types and addresses. Ensure the Wire interface is initialized in `setup`. ```C++ #include #include AT24C256 eprom0(AT24C_ADDRESS_0); AT24C256 eprom1(AT24C_ADDRESS_1); AT24C02 eprom2(AT24C_ADDRESS_2); void setup() { Wire.begin(); uint8_t byte = eprom0.read(0); } ``` -------------------------------- ### Read Data Types with get() Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Reads basic types or structs from memory. Returns a reference to the variable populated with the retrieved data. ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); struct SensorConfig { int sensorId; float calibrationFactor; bool enabled; char name[16]; }; void setup() { Wire.begin(); // Read basic types int intValue; eprom.get(0, intValue); Serial.print("Integer value: "); Serial.println(intValue); double pi; eprom.get(10, pi); Serial.print("Double value: "); Serial.println(pi, 10); // Read a complete struct SensorConfig config; eprom.get(100, config); if (eprom.getLastError() == 0) { Serial.println("--- Sensor Configuration ---"); Serial.print("ID: "); Serial.println(config.sensorId); Serial.print("Calibration: "); Serial.println(config.calibrationFactor); Serial.print("Enabled: "); Serial.println(config.enabled ? "Yes" : "No"); Serial.print("Name: "); Serial.println(config.name); } } // Output: Integer value: 12345 // Double value: 3.1415926536 // --- Sensor Configuration --- // ID: 1 // Calibration: 1.05 // Enabled: Yes // Name: Temperature ``` -------------------------------- ### put() - Write Any Data Type Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Template method that writes any basic type or struct to memory starting at the specified address. Automatically handles the size of the data type. ```APIDOC ## put(address, data) ### Description Writes any basic type or struct to memory starting at the specified address. Automatically handles the size of the data type. ### Parameters - **address** (uint16_t) - Required - The memory address to start writing at. - **data** (T) - Required - The variable or struct to write to memory. ``` -------------------------------- ### Read/Write Basic Types Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md Use `put` to write basic data types (int, long, double) to a specified memory address and `get` to read them back. Ensure the variable type matches the data being read. ```C++ int foo = 42; eprom.put(0, foo); // Write the integer value 42 to address 0 int foo_in; eprom.get(0, foo_in); // Read the integer value at address 0 into variable foo_in ``` -------------------------------- ### readBuffer() - Read Byte Array Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Reads an array of bytes from memory starting at the specified address. Returns the number of bytes actually read. ```APIDOC ## readBuffer(address, buffer, length) ### Description Reads an array of bytes from memory starting at the specified address. ### Parameters - **address** (uint16_t) - Required - The memory address to start reading from. - **buffer** (uint8_t*) - Required - Pointer to the buffer to store the read data. - **length** (size_t) - Required - Number of bytes to read. ### Response - **bytesRead** (int) - The number of bytes actually read. ``` -------------------------------- ### writeBuffer() - Write Byte Array Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Writes an array of bytes to memory starting at the specified address. Handles page boundaries automatically and returns the number of bytes written. ```APIDOC ## writeBuffer(address, buffer, length) ### Description Writes an array of bytes to memory starting at the specified address. Handles page boundaries automatically. ### Parameters - **address** (uint16_t) - Required - The memory address to start writing at. - **buffer** (uint8_t*) - Required - Pointer to the byte array to write. - **length** (size_t) - Required - Number of bytes to write. ### Response - **bytesWritten** (int) - The number of bytes successfully written. ``` -------------------------------- ### Get EEPROM Memory Size with length() Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Retrieve the total size of the EEPROM in bytes. This is useful for writing adaptive code that works with different EEPROM chip types. ```cpp #include #include AT24C256 largeEprom(AT24C_ADDRESS_0); AT24C02 smallEprom(AT24C_ADDRESS_1); void setup() { Wire.begin(); Serial.print("AT24C256 size: "); Serial.print(largeEprom.length()); Serial.println(" bytes"); Serial.print("AT24C02 size: "); Serial.print(smallEprom.length()); Serial.println(" bytes"); // Generic function that works with any chip size fillWithPattern(largeEprom); } // Template function that adapts to any EEPROM size template void fillWithPattern(T& eeprom) { uint16_t size = eeprom.length(); Serial.print("Filling "); Serial.print(size); Serial.println(" bytes with test pattern..."); for (uint16_t addr = 0; addr < size; addr += 64) { eeprom.write(addr, (uint8_t)(addr & 0xFF)); } } ``` -------------------------------- ### Read/Write Complex Types (Structs) Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md The `put` and `get` methods can also be used for complex data types like structs. Ensure the struct definition matches the data stored in the EEPROM. ```C++ struct Coordinate { int x; int y; }; Coordinate point = {17, 42}; eprom.put(0, point); // Write the struct point to address 0 Coordinate point_in; eprom.get(0, point_in); // Read the values of the struct point_in from address 0 ``` -------------------------------- ### length() - Get Memory Size Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Retrieves the total size of the EEPROM in bytes. This function is useful for creating adaptive code that works with various EEPROM chip sizes. ```APIDOC ## length() - Get Memory Size ### Description Returns the total size of the EEPROM in bytes. Useful for writing code that automatically adapts to different chip types. ### Method `length()` ### Parameters None ### Request Example ```cpp #include AT24C256 largeEprom(AT24C_ADDRESS_0); Serial.print(largeEprom.length()); ``` ### Response - **size** (uint16_t) - The total size of the EEPROM in bytes. ### Response Example ``` 32768 ``` ``` -------------------------------- ### Get EEPROM Chip Size Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md Retrieve the size of the connected EEPROM chip. This allows your code to adapt automatically to different chip types, ensuring compatibility with the built-in EEPROM library. ```C++ int size = eprom.length()); ``` -------------------------------- ### Instantiate AT24C04, AT24C08, and AT24C16 Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md Demonstrates how to instantiate AT24C04, AT24C08, and AT24C16 objects. Note that AT24C04 and AT24C08 have specific address enums due to multiple available addresses, while AT24C16 has a hardcoded single address and does not take an address parameter in its constructor. ```C++ #include #include #include AT24C04 eprom0(AT24C04_ADDRESS_1); AT24C08 eprom1(AT24C08_ADDRESS_1); AT24C16 eprom2(); ``` -------------------------------- ### AT24C Library Methods Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/keywords.txt Core methods available for interacting with AT24C EEPROM devices. ```APIDOC ## Library Methods ### Description Methods provided by the AT24C library to perform read/write operations and status checks on EEPROM devices. ### Methods - **read**: Reads data from the EEPROM. - **write**: Writes data to the EEPROM. - **update**: Updates data in the EEPROM. - **length**: Returns the storage capacity of the device. - **writeBuffer**: Writes a buffer of data to the EEPROM. - **readBuffer**: Reads a buffer of data from the EEPROM. - **getLastError**: Retrieves the last error code encountered. ``` -------------------------------- ### Supported Devices and Constants Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/keywords.txt List of supported AT24C chip models and available address constants. ```APIDOC ## Supported Devices - AT24C01, AT24C02, AT24C04, AT24C08, AT24C16, AT24C32, AT24C64, AT24C128, AT24C256 ## Address Constants - AT24C_ADDRESS_0 through AT24C_ADDRESS_7 ``` -------------------------------- ### Using Multiple I2C Buses Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Explains how to utilize multiple I2C buses available on certain Arduino boards to connect EEPROM chips. ```APIDOC ## Using Multiple I2C Buses ### Description On Arduino boards with multiple I2C interfaces, you can specify which `TwoWire` bus each chip uses. This allows for more flexible hardware configurations. ### Example Usage ```cpp #include #include // Assuming Wire and Wire1 are available (e.g., on ESP32, Arduino Due) AT24C256 eprom0(AT24C_ADDRESS_0, Wire); // Chip on default I2C bus AT24C256 eprom1(AT24C_ADDRESS_0, Wire1); // Same address, different bus void setup() { Wire.begin(); Wire1.begin(); // ... operations on eprom0 and eprom1 ... } ``` ### Notes When using different I2C buses, EEPROM chips can share the same I2C address because they are isolated on separate buses. ``` -------------------------------- ### Multiple Chips on Same Bus Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Demonstrates how to use multiple EEPROM chips, even of different types, on the same I2C bus. ```APIDOC ## Multiple Chips on Same Bus ### Description The library supports multiple EEPROM chips connected to the same I2C bus, including chips of different types. ### Example Usage ```cpp #include #include #include AT24C256 configStorage(AT24C_ADDRESS_0); AT24C64 dataLog(AT24C_ADDRESS_1); AT24C02 calibration(AT24C_ADDRESS_2); void setup() { Wire.begin(); // ... operations on configStorage, dataLog, calibration ... } ``` ### Notes Each chip can be instantiated with a unique I2C address on the bus. The `getLastError()` function can be used to verify chip presence after operations. ``` -------------------------------- ### Specify Write Cycle Time for AT24C256 Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md Instantiate an AT24C256 object with a custom write cycle time. Use this for older AT24C256A chips that may require up to 20 mS. Setting to 0 disables internal waits for time-critical applications, but requires manual synchronization. ```C++ #include AT24C256 eprom0(AT24C_ADDRESS_0, Wire, 20); ``` -------------------------------- ### Configure Custom Write Cycle Times Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Adjust the write cycle delay for older chip variants or optimize for time-critical applications. Manual timing is required when setting the delay to 0ms. ```cpp #include // Standard chip with default 6ms write cycle time AT24C256 standardChip(AT24C_ADDRESS_0); // Older AT24C256A variant requiring 20ms write cycle time AT24C256 oldChip(AT24C_ADDRESS_1, Wire, 20); // Time-critical application with 0ms delay (manual timing required) AT24C256 fastChip(AT24C_ADDRESS_2, Wire, 0); void setup() { Wire.begin(); Serial.begin(115200); // Standard chip - normal operation unsigned long start = millis(); standardChip.write(0, 0x55); Serial.print("Standard chip write time: "); Serial.print(millis() - start); Serial.println(" ms"); // Old chip - longer delay per write start = millis(); oldChip.write(0, 0x55); Serial.print("Old chip write time: "); Serial.print(millis() - start); Serial.println(" ms"); // Fast chip - no delay, but requires manual timing start = millis(); fastChip.write(0, 0x55); Serial.print("Fast chip write time: "); Serial.print(millis() - start); Serial.println(" ms"); // WARNING: Must wait before next operation on fast chip delay(6); // Manual delay required uint8_t value = fastChip.read(0); // Now safe to read } void loop() {} // Output: Standard chip write time: 6 ms // Old chip write time: 20 ms // Fast chip write time: 0 ms ``` -------------------------------- ### Read/Write Byte Buffers Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md Write and read byte buffers of any size using `writeBuffer` and `readBuffer`. The library handles partitioning for I2C limitations. ```C++ uint8_t out[15] = "Test of buffer"; eprom.writeBuffer(0, out, 15); uint8_t in[15]; eprom.readBuffer(0, in, 15); ``` -------------------------------- ### Specify TwoWire Interface Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md For boards with multiple I2C buses, you can explicitly assign a `TwoWire` interface to an EEPROM object during its instantiation. ```C++ #include AT24C256 eprom0(AT24C_ADDRESS_0, Wire); ``` -------------------------------- ### Write Byte Arrays with writeBuffer() Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Writes an array of bytes to memory. This method handles page boundaries automatically and returns the total bytes written. ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Wire.begin(); // Write a short string buffer uint8_t message[20] = "Hello, EEPROM!"; int bytesWritten = eprom.writeBuffer(0, message, 20); Serial.print("Short buffer: "); Serial.print(bytesWritten); Serial.println(" bytes written"); // Write a large buffer that spans multiple pages (page size is 64 bytes for AT24C256) uint8_t largeData[150]; for (int i = 0; i < 150; i++) { largeData[i] = i; // Fill with incrementing values } bytesWritten = eprom.writeBuffer(1000, largeData, 150); if (eprom.getLastError() == 0) { Serial.print("Large buffer: "); Serial.print(bytesWritten); Serial.println(" bytes written successfully"); } else { Serial.print("Write error: "); Serial.println(eprom.getLastError()); } } // Output: Short buffer: 20 bytes written // Large buffer: 150 bytes written successfully ``` -------------------------------- ### Read/Write Single Bytes Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md Use `write` to set a single byte at a specific address. `update` is similar but only writes if the new value differs from the current one, reducing wear. `read` retrieves a single byte's value. ```C++ eprom.write(0, 77); // Writes the value 77 to byte at address 0 eprom.update(0, 77); // In this case `update` does nothing, since it only writes if the value differs from the current uint8_t value = eprom.read(0); // Reads the value of the byte at address 0 ``` -------------------------------- ### Utilizing Multiple I2C Buses Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Configure EEPROM chips to use different I2C buses on boards that support multiple interfaces, such as ESP32 or Arduino Due. This allows using multiple chips even if they share the same I2C address. ```cpp #include #include // On boards with multiple I2C buses (e.g., ESP32, Arduino Due) // Wire is the default bus, Wire1 is the second bus AT24C256 eprom0(AT24C_ADDRESS_0, Wire); // Chip on default I2C bus AT24C256 eprom1(AT24C_ADDRESS_0, Wire1); // Same address, different bus void setup() { Serial.begin(115200); // Initialize both I2C buses Wire.begin(); Wire1.begin(); // Each chip can have the same address since they're on different buses eprom0.write(0, 0xAA); eprom1.write(0, 0xBB); Serial.print("Bus 0, Address 0: 0x"); Serial.println(eprom0.read(0), HEX); Serial.print("Bus 1, Address 0: 0x"); Serial.println(eprom1.read(0), HEX); } void loop() {} ``` -------------------------------- ### Write Data Types with put() Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Writes basic types or structs to memory. The method automatically determines the size of the data type based on the platform. ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); // Define a custom struct struct SensorConfig { int sensorId; float calibrationFactor; bool enabled; char name[16]; }; void setup() { Wire.begin(); // Write basic types int intValue = 12345; eprom.put(0, intValue); // Writes 2 bytes (on AVR) or 4 bytes (on ARM) double pi = 3.14159265359; eprom.put(10, pi); // Writes 4 or 8 bytes depending on platform // Write a complete struct SensorConfig config = { .sensorId = 1, .calibrationFactor = 1.05, .enabled = true, .name = "Temperature" }; eprom.put(100, config); if (eprom.getLastError() == 0) { Serial.println("All data written successfully"); Serial.print("Struct size: "); Serial.print(sizeof(SensorConfig)); Serial.println(" bytes"); } } // Output: All data written successfully // Struct size: 24 bytes ``` -------------------------------- ### Read Byte Arrays with readBuffer() Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Reads a specified number of bytes from memory into a buffer. Returns the actual number of bytes read. ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Wire.begin(); // Read a string buffer uint8_t message[20]; int bytesRead = eprom.readBuffer(0, message, 20); if (eprom.getLastError() == 0) { Serial.print("Read "); Serial.print(bytesRead); Serial.print(" bytes: "); Serial.println((char*)message); } // Read and verify large buffer uint8_t largeData[150]; bytesRead = eprom.readBuffer(1000, largeData, 150); Serial.print("Large buffer: "); Serial.print(bytesRead); Serial.println(" bytes read"); // Verify data integrity bool dataValid = true; for (int i = 0; i < 150; i++) { if (largeData[i] != i) { dataValid = false; Serial.print("Mismatch at index "); Serial.println(i); break; } } if (dataValid) { Serial.println("Data integrity verified"); } } // Output: Read 20 bytes: Hello, EEPROM! // Large buffer: 150 bytes read // Data integrity verified ``` -------------------------------- ### Implement Special Addressing for AT24C04, AT24C08, and AT24C16 Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Utilize specific addressing schemes for chips with limited address pins. The library automatically manages internal address overflows for larger memory ranges. ```cpp #include #include #include // AT24C04: Only 4 addresses available (uses A2 and A1 pins, A0 is internal) AT24C04 chip04_0(AT24C04_ADDRESS_0); // 0x50 AT24C04 chip04_1(AT24C04_ADDRESS_1); // 0x52 AT24C04 chip04_2(AT24C04_ADDRESS_2); // 0x54 AT24C04 chip04_3(AT24C04_ADDRESS_3); // 0x56 // AT24C08: Only 2 addresses available (uses A2 pin only) AT24C08 chip08_0(AT24C08_ADDRESS_0); // 0x50 AT24C08 chip08_1(AT24C08_ADDRESS_1); // 0x54 // AT24C16: Fixed address, no address pins - only ONE chip per I2C bus AT24C16 chip16; // No address parameter - always at 0x50 void setup() { Wire.begin(); Serial.begin(115200); // Write across the full address range of AT24C16 (2048 bytes) // The library handles the internal address overflow automatically for (uint16_t addr = 0; addr < 2048; addr += 256) { chip16.write(addr, (uint8_t)(addr >> 8)); if (chip16.getLastError() == 0) { Serial.print("Wrote to address "); Serial.println(addr); } } // Verify the writes Serial.println("Verification:"); for (uint16_t addr = 0; addr < 2048; addr += 256) { uint8_t value = chip16.read(addr); Serial.print("Address "); Serial.print(addr); Serial.print(": "); Serial.println(value); } } void loop() {} // Output: Wrote to address 0 // Wrote to address 256 // Wrote to address 512 // ... // Verification: // Address 0: 0 // Address 256: 1 // Address 512: 2 // ... ``` -------------------------------- ### write() - Write Single Byte Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Writes a single byte to the specified memory address. Always performs the write operation regardless of the current value. ```APIDOC ## write() - Write Single Byte ### Description Writes a single byte to the specified memory address. Always performs the write operation regardless of current value. ### Method N/A (Library function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Wire.begin(); // Write value 42 to address 0 eprom.write(0, 42); if (eprom.getLastError() == 0) { Serial.println("Write successful"); // Verify by reading back uint8_t readBack = eprom.read(0); Serial.print("Verification read: "); Serial.println(readBack); } else { Serial.print("Write failed with error: "); Serial.println(eprom.getLastError()); } } ``` ### Response #### Success Response (200) N/A (Write operation) #### Response Example ``` Write successful Verification read: 42 ``` ``` -------------------------------- ### Check EEPROM Operation Errors Source: https://github.com/stefangs/arduino-library-at24cxxx/blob/main/README.md After any EEPROM operation, check `getLastError()` to detect communication failures. A return value of 0 indicates success. Non-zero values signify errors like buffer overflows or NACK responses. ```C++ uint8_t data = eprom.read(0); if (eprom.getLastError() != 0) { Serial.print("Error reading from eeprom"); } ``` -------------------------------- ### Write Single Byte to EEPROM Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Writes a single byte to a specified memory address using the write() method. This method always performs the write operation, regardless of the current value. ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Wire.begin(); // Write value 42 to address 0 eprom.write(0, 42); if (eprom.getLastError() == 0) { Serial.println("Write successful"); // Verify by reading back uint8_t readBack = eprom.read(0); Serial.print("Verification read: "); Serial.println(readBack); } else { Serial.print("Write failed with error: "); Serial.println(eprom.getLastError()); } } // Output: Write successful // Verification read: 42 ``` -------------------------------- ### Managing Multiple EEPROM Chips on the Same I2C Bus Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Connect and manage multiple EEPROM chips, including different types, on a single I2C bus. Each chip can be accessed independently using its unique address. ```cpp #include #include #include // Three different chips on the same I2C bus AT24C256 configStorage(AT24C_ADDRESS_0); // 32KB for configuration AT24C64 dataLog(AT24C_ADDRESS_1); // 8KB for data logging AT24C02 calibration(AT24C_ADDRESS_2); // 256 bytes for calibration data void setup() { Wire.begin(); Serial.begin(115200); // Verify all chips are present Serial.println("Checking EEPROM chips..."); configStorage.read(0); Serial.print("Config storage (AT24C256): "); Serial.println(configStorage.getLastError() == 0 ? "OK" : "NOT FOUND"); dataLog.read(0); Serial.print("Data log (AT24C64): "); Serial.println(dataLog.getLastError() == 0 ? "OK" : "NOT FOUND"); calibration.read(0); Serial.print("Calibration (AT24C02): "); Serial.println(calibration.getLastError() == 0 ? "OK" : "NOT FOUND"); // Use each chip for its purpose int configVersion = 1; configStorage.put(0, configVersion); float calFactor = 1.0234; calibration.put(0, calFactor); uint8_t logEntry[32] = "System started"; dataLog.writeBuffer(0, logEntry, 32); } void loop() { // Application code using all three chips } ``` -------------------------------- ### Error Handling with getLastError() Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Obtain the error code from the last EEPROM operation. This is crucial for diagnosing communication issues with external memory devices. ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); AT24C256 missingEprom(AT24C_ADDRESS_7); // Assume no chip at this address void setup() { Wire.begin(); Serial.begin(115200); // Successful operation eprom.write(0, 123); uint8_t error = eprom.getLastError(); printError("Write to connected EEPROM", error); // Failed operation - chip not present missingEprom.write(0, 123); error = missingEprom.getLastError(); printError("Write to missing EEPROM", error); // Read from missing chip missingEprom.read(0); error = missingEprom.getLastError(); printError("Read from missing EEPROM", error); } void printError(const char* operation, uint8_t error) { Serial.print(operation); Serial.print(": "); switch (error) { case 0: Serial.println("Success"); break; case 1: Serial.println("Error: Buffer overflow"); break; case 2: Serial.println("Error: NACK on address (device not found)"); break; case 3: Serial.println("Error: NACK on data"); break; case 4: Serial.println("Error: Bus error"); break; default: Serial.println("Error: Unknown"); break; } } ``` -------------------------------- ### read() - Read Single Byte Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Reads a single byte from the specified memory address. Returns the byte value at the given address. ```APIDOC ## read() - Read Single Byte ### Description Reads a single byte from the specified memory address. Returns the byte value at the given address. ### Method N/A (Library function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Wire.begin(); // Read single byte from address 100 uint8_t value = eprom.read(100); // Check for communication errors if (eprom.getLastError() == 0) { Serial.print("Value at address 100: "); Serial.println(value); } else { Serial.print("Read error code: "); Serial.println(eprom.getLastError()); } } ``` ### Response #### Success Response (200) - **value** (uint8_t) - The byte value read from the specified address. #### Response Example ``` Value at address 100: 255 (default value for erased EEPROM) ``` ``` -------------------------------- ### Read Single Byte from EEPROM Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Reads a single byte from a specified memory address using the read() method. Always check getLastError() for communication issues. ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Wire.begin(); // Read single byte from address 100 uint8_t value = eprom.read(100); // Check for communication errors if (eprom.getLastError() == 0) { Serial.print("Value at address 100: "); Serial.println(value); } else { Serial.print("Read error code: "); Serial.println(eprom.getLastError()); } } // Output: Value at address 100: 255 (default value for erased EEPROM) ``` -------------------------------- ### Conditional Write Single Byte using update() Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Writes a byte to memory only if the new value differs from the current value, using the update() method. This helps extend EEPROM cell life by reducing unnecessary writes. ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Wire.begin(); // First write - will perform actual write eprom.write(0, 77); Serial.println("Initial write of 77 complete"); // Update with same value - no write performed (preserves cell life) eprom.update(0, 77); Serial.println("Update with 77 - no write needed"); // Update with different value - write performed eprom.update(0, 88); Serial.println("Update with 88 - write performed"); // Verify final value Serial.print("Final value: "); Serial.println(eprom.read(0)); } // Output: Initial write of 77 complete // Update with 77 - no write needed // Update with 88 - write performed // Final value: 88 ``` -------------------------------- ### update() - Conditional Write Single Byte Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Writes a byte only if the new value differs from the current value. This reduces unnecessary writes to extend EEPROM cell life. ```APIDOC ## update() - Conditional Write Single Byte ### Description Writes a byte only if the new value differs from the current value. Reduces unnecessary writes to extend EEPROM cell life. ### Method N/A (Library function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); void setup() { Wire.begin(); // First write - will perform actual write eprom.write(0, 77); Serial.println("Initial write of 77 complete"); // Update with same value - no write performed (preserves cell life) eprom.update(0, 77); Serial.println("Update with 77 - no write needed"); // Update with different value - write performed eprom.update(0, 88); Serial.println("Update with 88 - write performed"); // Verify final value Serial.print("Final value: "); Serial.println(eprom.read(0)); } ``` ### Response #### Success Response (200) N/A (Update operation) #### Response Example ``` Initial write of 77 complete Update with 77 - no write needed Update with 88 - write performed Final value: 88 ``` ``` -------------------------------- ### getLastError() - Error Handling Source: https://context7.com/stefangs/arduino-library-at24cxxx/llms.txt Returns the error code from the last operation. This is crucial for identifying and handling communication failures or device issues. ```APIDOC ## getLastError() - Error Handling ### Description Returns the error code from the last operation. Essential for detecting communication failures with external memory. ### Method `getLastError()` ### Parameters None ### Request Example ```cpp #include AT24C256 eprom(AT24C_ADDRESS_0); eprom.write(0, 123); uint8_t error = eprom.getLastError(); ``` ### Response - **errorCode** (uint8_t) - The error code of the last operation. `0` indicates success. Other values represent specific errors (e.g., `2` for NACK on address). ### Response Example ``` 0 ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.