Für die Arbeitsweise mit dem Raspberry PI sind allerdings einige Vorbereitungen zu treffen.
sudo raspi-config
Aufrufen, um die Konfiguration des Raspberry PI zu starten. Dort muss dann der I2C Bus unter 'Interface Options' aktiviert werden.
Im nächsten Schritt müssen die Abhängigkeiten für Python aktualisiert werden.
sudo apt update
sudo apt-get install python3-smbus
Mit der Installation wird die benötigten Bibliotheken für Python zur Unterstützung des SMBUS geladen und installiert.
Der folgende Code ist nicht validiert, da ich das Beispiel des Herstellers hier einfach abgedruckt habe - um es zu archivieren.
import smbus
import time
# Method to write data into a specific location
def write(location, data):
device.write_i2c_block_data(i2c_address,location >> 8, [location & 0xFF, data] )
time.sleep(0.005)
# Method to read data from a specific location
def read(location):
device.write_i2c_block_data(i2c_address,location >> 8, [location & 0xFF])
time.sleep(0.001)
return device.read_byte(i2c_address)
i2c_address = 0x50
device = smbus.SMBus(1)
write(0,13) # write value 13 to location 0
print(read(0)) # prints the value saved at location 0