Advertisement
microrobotics

Microchip Technology Inc. 24AA16/24LC16B

Apr 4th, 2023
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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:
  3.  
  4. 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.
  5.  
  6. Connect the 24LC16B EEPROM chip to the Arduino as follows:
  7.  
  8. VCC pin to 5V (or 3.3V, depending on your chip's voltage level)
  9. GND pin to GND
  10. SDA pin to the SDA pin on your Arduino (A4 on the Uno, Leonardo, or Nano; pin 20 on the Mega)
  11. SCL pin to the SCL pin on your Arduino (A5 on the Uno, Leonardo, or Nano; pin 21 on the Mega)
  12. A0, A1, and A2 pins to GND or VCC, depending on the desired I2C address (0x50-0x57)
  13. 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.
  14. */
  15.  
  16. #include <Wire.h>
  17. #include <Arduino.h>
  18.  
  19. const byte eepromAddress = 0x50; // Address of the 24LC16B EEPROM chip (0x50-0x57 depending on the A0, A1, and A2 pins)
  20.  
  21. void setup() {
  22.   Wire.begin();
  23.   Serial.begin(9600);
  24.  
  25.   // Write data to the EEPROM
  26.   byte memoryAddress = 0x00; // Memory address to write data to (0x00-0xFF for each of the 8 blocks)
  27.   byte dataToWrite = 42;
  28.   writeEEPROM(eepromAddress, memoryAddress, dataToWrite);
  29.  
  30.   // Read data from the EEPROM
  31.   byte readData = readEEPROM(eepromAddress, memoryAddress);
  32.   Serial.print("Read data from memory address 0x");
  33.   Serial.print(memoryAddress, HEX);
  34.   Serial.print(": ");
  35.   Serial.println(readData);
  36. }
  37.  
  38. void loop() {
  39.   // Nothing to do in the loop
  40. }
  41.  
  42. void writeEEPROM(byte deviceAddress, byte memoryAddress, byte data) {
  43.   Wire.beginTransmission(deviceAddress);
  44.   Wire.write(memoryAddress);
  45.   Wire.write(data);
  46.   Wire.endTransmission();
  47.  
  48.   delay(5); // Allow time for the EEPROM to complete the write operation
  49. }
  50.  
  51. byte readEEPROM(byte deviceAddress, byte memoryAddress) {
  52.   byte readData;
  53.  
  54.   Wire.beginTransmission(deviceAddress);
  55.   Wire.write(memoryAddress);
  56.   Wire.endTransmission();
  57.  
  58.   Wire.requestFrom(deviceAddress, (byte)1);
  59.   if (Wire.available()) {
  60.     readData = Wire.read();
  61.   }
  62.  
  63.   return readData;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement