Arduino UNO and EEPROM

Schematic.

list of products from Amazon (Not mine, i just link them!) Scroll Down For Article! Note: Elegoo boards are not Arduino, but they are compatible with Arduino IDE and are mutch more affordable!


Project Description: EEPROM SPI Communication with Arduino

In this quick tutorial, we'll explore how to communicate with an EEPROM (Electrically Erasable Programmable Read-Only Memory) using SPI (Serial Peripheral Interface) with an Arduino. The project utilizes the SPI library to enable writing and reading data to and from the EEPROM, demonstrating a practical use of non-volatile memory.

Requirements

  1. Arduino Uno.
  2. 25AA160A EEPROM.
  3. Breadboard and jumper wires.
  4. SPI communication enabled on the Arduino.

Features

  1. Write data to the EEPROM.
  2. Read data back from the EEPROM.
  3. Display the data in the Serial Monitor.

The 25AA160A EEPROM has both a Write Protect (WP) and Hold (HOLD) pin. Here's how these pins are used and connected:

  1. Write Protect (WP):

    • The WP pin is used to prevent accidental writes to the EEPROM.
    • When WP is connected to GND, the write protect feature is disabled, and writing to the EEPROM is allowed.
    • When WP is connected to VCC, the write protect feature is enabled, blocking write operations to the EEPROM.
  2. Hold (HOLD):

    • The HOLD pin is used to pause communication without terminating the current SPI transaction.
    • When HOLD is connected to GND, the communication is paused.
    • When HOLD is connected to VCC, the communication operates normally.

EEPROMPin   Arduino Uno PinDescription




CSPin 10Chip Select (Active Low)
SCKPin 13Serial Clock
SIPin 11Serial Input (MOSI)
SOPin 12Serial Output (MISO)

How to Manipulate the EEPROM:

  • #define WRITE_ENABLE 0x06: This command enables write operations on the EEPROM. Before writing any data, this command must be sent to allow the EEPROM to accept write instructions.

  • #define WRITE_DISABLE 0x04: This command disables write operations. It prevents further write commands from being executed, ensuring that no accidental overwrites occur.

  • #define READ_DATA 0x03: This command is used to read data from the EEPROM. When sent, the EEPROM returns the data stored at the specified address.

  • #define WRITE_DATA 0x02: This command initiates the writing of data to the EEPROM. It must be followed by the target address and the data to be written.

  • #define RDSR 0x05: This command reads the status register of the EEPROM. The status register contains information about the current state of the EEPROM, such as whether it is busy or ready for the next operation.

  • #define WRSR 0x01: This command writes to the status register. It allows modifications to specific settings within the EEPROM, such as configuring write protection


  • CODE:

    / File: EEPROM_SPI_with_WP_HOLD.ino

    #include <SPI.h>

    // Define EEPROM pins
    const int CS_PIN = 10;

    // EEPROM Instructions
    #define WRITE_ENABLE  0x06
    #define WRITE_DISABLE 0x04
    #define READ_DATA     0x03
    #define WRITE_DATA    0x02
    #define RDSR          0x05
    #define WRSR          0x01

    void setup() {
      Serial.begin(9600);
      pinMode(CS_PIN, OUTPUT);
      digitalWrite(CS_PIN, HIGH);

      SPI.begin();

      // Check if the Write Protect and Hold pins are configured correctly
      Serial.println("EEPROM Initialized with WP disabled and HOLD enabled.");

      enableWrite();
      writeEEPROM(0x00, "Hello EEPROM");
      delay(10);

      char buffer[20];
      readEEPROM(0x00, buffer, 12);
      Serial.println("Read from EEPROM:");
      Serial.println(buffer);
    }

    void loop() {
      // Loop intentionally left empty
    }

    // Enable write operations on the EEPROM
    void enableWrite() {
      digitalWrite(CS_PIN, LOW);
      SPI.transfer(WRITE_ENABLE);
      digitalWrite(CS_PIN, HIGH);
    }

    // Write data to the EEPROM
    void writeEEPROM(uint16_t address, const char* data) {
      digitalWrite(CS_PIN, LOW);
      SPI.transfer(WRITE_DATA);
      SPI.transfer((address >> 8) & 0xFF); // Address high byte
      SPI.transfer(address & 0xFF);       // Address low byte

      for (size_t i = 0; i < strlen(data); i++) {
        SPI.transfer(data[i]);
      }

      digitalWrite(CS_PIN, HIGH);
      delay(5); // Wait for the write cycle to complete
    }

    // Read data from the EEPROM
    void readEEPROM(uint16_t address, char* buffer, size_t length) {
      digitalWrite(CS_PIN, LOW);
      SPI.transfer(READ_DATA);
      SPI.transfer((address >> 8) & 0xFF); // Address high byte
      SPI.transfer(address & 0xFF);       // Address low byte

      for (size_t i = 0; i < length; i++) {
        buffer[i] = SPI.transfer(0xFF); // Dummy byte to clock data out
      }
      buffer[length] = '\0'; // Null-terminate the string

      digitalWrite(CS_PIN, HIGH);
    }



    SCHEMATIC:
    Schematic


    DOCUMENTATION:

    https://www.microchip.com/en-us/product/25aa160a

    Project Link:

    https://drive.google.com/file/d/1WdUdJJU4RJfS9iXkPDs96S0zFGiT03Bq/view?usp=drive_link

    Post a Comment

    0 Comments