I2C Library Guide

Definition:

The I2C library has 3 main functions :

  1. configure()

  2. write(data)

  3. read(data)

The 1st function must be executed one time at the begining of the code. The 2nd and 3rd functions take one argument (data) of type array:


1.When using the i2c_write function, the array must follow this form:

  • if there is a specific address of a register that you want to target:

data = [address_of_i2c_component, address_of_register, byte1, byte2, … as much as you need to write]

  • if not :

data = [address_of_i2c_component, byte1, byte2, … as much as you need to write]

this function returns the number of Acks sent back from the target.


2.When using the i2c_read function, the array must follow this form:

  • if there is a specific address of a register that you want to target:

data = [number_of_bytes_you_want_to_read, address_of_i2c_component, address_of_register]

  • if not :

data = [number_of_bytes_you_want_to_read, address_of_i2c_component]

this function returns an array of size = number_of_bytes_you_want_to_read.

Example:

from i2c_lib import I2cLib

i2c = I2cLib()
i2c.configure()

#write 0x32 to address 0x49 and register address 0x2:
data= [0x49, 0x2, 0x32]
ret = i2c.write(data)
print("Number of Acks is: ", ret)

#read 2 bytes from address 0x49 and register address 0x7:
data= [2, 0x49, 0x7]
ret = i2c.read(data)
print("We read: ", hex(ret[0]))
print("We read: ", hex(ret[1]))

© NanoXplore 2022