To detect and interact with an EEPROM using a Raspberry Pi 3B with Python, you'll typically use the I²C interface. This is commonly done with libraries like smbus or smbus2, which provide access to I²C devices from Python. Below is a general guide and example program to help you detect an EEPROM on the I²C bus and perform basic read/write operations.
Prerequisites
-
Enable I²C on Your Raspberry Pi:
You need to enable I²C on your Raspberry Pi. You can do this using the raspi-config tool:
sudo raspi-config
Go to Interfacing Options -> I2C and enable it. You might need to reboot your Raspberry Pi for the changes to take effect.
-
Install Required Packages:
Install the smbus library. You might also need the i2c-tools for debugging.
sudo apt-get update
sudo apt-get install python3-smbus i2c-tools
-
Locate the EEPROM on the I²C Bus:
Use the following command to find the address of your EEPROM:
i2cdetect -y 1
This will show a grid of addresses. Any addresses with a number (not --) indicate a device is present. Note the address of your EEPROM.
Example Python Program to Interact with EEPROM
Here is an example Python script to interact with an EEPROM on the I²C bus:
import smbus2
import time
# Constants for the EEPROM
EEPROM_I2C_BUS = 1 # I2C bus number, usually 1 on Raspberry Pi
EEPROM_ADDRESS = 0x50 # I2C address of EEPROM, replace with actual address
EEPROM_REGISTER = 0x00 # Register address to start reading/writing
def read_eeprom(bus, address, register, length):
"""Reads data from the EEPROM."""
data = bus.read_i2c_block_data(address, register, length)
return data
def write_eeprom(bus, address, register, data):
"""Writes a list of data bytes to the EEPROM."""
bus.write_i2c_block_data(address, register, data)
time.sleep(0.1) # Wait for EEPROM write cycle to complete
def main():
bus = smbus2.SMBus(EEPROM_I2C_BUS)
# Data to write (must not exceed page size of EEPROM, usually 16 or 32 bytes)
data_to_write = [0xDE, 0xAD, 0xBE, 0xEF]
# Write data to EEPROM
print(f"Writing data: {data_to_write}")
write_eeprom(bus, EEPROM_ADDRESS, EEPROM_REGISTER, data_to_write)
# Read back the data from EEPROM
read_data = read_eeprom(bus, EEPROM_ADDRESS, EEPROM_REGISTER, len(data_to_write))
print(f"Read data: {read_data}")
bus.close()
if __name__ == "__main__":
main()
Key Notes:
- EEPROM_ADDRESS: Replace with the actual I²C address of your EEPROM as found by
i2cdetect.
- EEPROM_REGISTER: Replace with the starting register address you want to read from/write to.
- Data Length: Be mindful of EEPROM page size limits when writing (common sizes are 8, 16, or 32 bytes max per write).
- Write Delay: EEPROMs require a short delay after a write operation to complete the cycle, typically around 5ms to 10ms, but 100ms is often used for simplicity.
This script provides a basic framework for interacting with an EEPROM using I²C on a Raspberry Pi. Depending on your EEPROM's specifications, you might need to adjust the register addresses, page sizes, and timing. Always refer to your EEPROM's datasheet for specific details.