Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The 24AA16/24LC16B are I2C EEPROM memory chips from Microchip Technology Inc. You can use the Wire library to interface these chips with an Arduino. Here's an example code to read and write data to the 24LC16B EEPROM chip:
- This code writes a byte of data (42 in this case) to the EEPROM memory and then reads the data back from the memory. The read data is printed to the Serial Monitor.
- Connect the 24LC16B EEPROM chip to the Arduino as follows:
- VCC pin to 5V (or 3.3V, depending on your chip's voltage level)
- GND pin to GND
- SDA pin to the SDA pin on your Arduino (A4 on the Uno, Leonardo, or Nano; pin 20 on the Mega)
- SCL pin to the SCL pin on your Arduino (A5 on the Uno, Leonardo, or Nano; pin 21 on the Mega)
- A0, A1, and A2 pins to GND or VCC, depending on the desired I2C address (0x50-0x57)
- Please note that different 24XX16 EEPROM chips might have slightly different pin arrangements, so verify the pin labels on your specific chip before making connections.
- */
- #include <Wire.h>
- #include <Arduino.h>
- const byte eepromAddress = 0x50; // Address of the 24LC16B EEPROM chip (0x50-0x57 depending on the A0, A1, and A2 pins)
- void setup() {
- Wire.begin();
- Serial.begin(9600);
- // Write data to the EEPROM
- byte memoryAddress = 0x00; // Memory address to write data to (0x00-0xFF for each of the 8 blocks)
- byte dataToWrite = 42;
- writeEEPROM(eepromAddress, memoryAddress, dataToWrite);
- // Read data from the EEPROM
- byte readData = readEEPROM(eepromAddress, memoryAddress);
- Serial.print("Read data from memory address 0x");
- Serial.print(memoryAddress, HEX);
- Serial.print(": ");
- Serial.println(readData);
- }
- void loop() {
- // Nothing to do in the loop
- }
- void writeEEPROM(byte deviceAddress, byte memoryAddress, byte data) {
- Wire.beginTransmission(deviceAddress);
- Wire.write(memoryAddress);
- Wire.write(data);
- Wire.endTransmission();
- delay(5); // Allow time for the EEPROM to complete the write operation
- }
- byte readEEPROM(byte deviceAddress, byte memoryAddress) {
- byte readData;
- Wire.beginTransmission(deviceAddress);
- Wire.write(memoryAddress);
- Wire.endTransmission();
- Wire.requestFrom(deviceAddress, (byte)1);
- if (Wire.available()) {
- readData = Wire.read();
- }
- return readData;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement