Package: pyi2c
A useful i2c Python3 package for PiIt is a simple I2C interface based on smbus2.1. InstallationViapippip3 install pyi2c2. API and example2.1I2C(bus_n)from pyi2c import I2C

# Create i2c
BUS_N = 0 # 0 or 1 or 2. Change this to yours
i2c = I2C(BUS_N)

ADDR = 0x38 # Change this to yours2.1.1status_codeReturn{StatusCode.success: 0}{StatusCode.ready: 1}{StatusCode.fail: 9}print( i2c.status_code )
# => {StatusCode: 1}To get the value, use.valueprint( i2c.status_code.value )
# => 1

if i2c.status_code.value == 1:
    print('ready')2.1.2scan()Scan all I2C devices on the same BUS.Returnlist of integer (address in byte)print( i2c.scan() )00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- 38 -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- 5a -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
80: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
[56, 90]2.1.3write(ADDR, data)Returnnothingdatacan be a byte or list of bytes.WRITE0 = 0x00 # Change this to yours
i2c.write(ADDR, WRITE0)

# or write multi bytes, up to 64 bytes
WRITE1 = 0x01 # Change this to yours
i2c.write(ADDR, [WRITE0, WRITE1])2.1.4read(ADDR, byte_size=1)Returninteger (a byte), or list of integers (bytes) ifbyte_size >= 0byte_sizecan be empty (default is 1)read_data = i2c.read(ADDR)

# or set length of reading bytes
byte_size = 2
read_data = i2c.read(ADDR, byte_size)
print( len(read_data) )
# 22.1.5writeread(ADDR, data, byte_size=1)Returninteger (a byte), or list of integer (bytes) ifbyte_size >= 0datacan be a byte or list of bytes.byte_sizecan be empty (default is 1)# First write and read rapidly one byte
read_data = i2c.writeread(ADDR, WRITE0)

# These also work
read_data = i2c.writeread(ADDR, [WRITE0, WRITE1])
read_data = i2c.writeread(ADDR, [WRITE0, WRITE1], byte_size)2.2I2CDevice(bus_n, addr)It is extension of I2C, but contains a I2C device's address. So it is not need to write address any more after declare.from pyi2c import I2CDevice

BUS_N = 0
ADDR = 0x38

aht10 = I2CDeivce(BUS_N, ADDR)2.2.1status_codeAs the same asstatus_codein I2C.2.2.2write(data)As the same aswrite(addr, data)in I2C, but does not need address.2.2.3read(byte_size=1)As the same asread(addr, byte_size)in I2C, but does not need address.2.2.4writeread(data, byte_size=1)As the same aswriteread(addr, data, byte_size=1)in I2C, but does not need address.2.3getBit(byte, bin_n, bin_m=-1)Returninteger (a bit or bits)bin_nshould be>= 0bin_mcan be empty (default is -1 but will overwrote withbin_n)bin_norbin_mcan be larger than byte's sizefrom pyi2c import getBit

byte = 0x5a # Any byte data
print( bin(byte) )
# '0b1011010'Get bit #n of byteprint( getBit(byte, 0) )
# 0

print( getBit(byte, 1) )
# 1Get multi bits from #n to #m of byteprint( getBit(byte, 4, 3) )
# 3 = 0b10
print( getBit(byte, 3, 4) )
# 3 = 0b10, the same as previousRecommend usageif getBit(byte, 4) == 0b1:
    print('hoge')
# 'hoge'For developersBuildpython3 -m buildUploadpython3 -m twine upload dist/*ReferencePackaging Python Projectssmbus2 GitHubPure Python I2C : access to I2C components through serial or parallel interface.