Skip to main content

A useful i2c package for Pi

Project description

pyi2c logo

A useful i2c Python3 package for Pi

It is a simple I2C interface based on smbus2.

1. Installation

Via pip

pip3 install pyi2c

2. API and example

2.1 I2C(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 yours

2.1.1 status_code

Return

  • {StatusCode.success: 0}
  • {StatusCode.ready: 1}
  • {StatusCode.fail: 9}
print( i2c.status_code )
# => {StatusCode: 1}

To get the value, use .value

print( i2c.status_code.value )
# => 1

if i2c.status_code.value == 1:
    print('ready')

2.1.2 scan()

Scan all I2C devices on the same BUS.

Return list 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.3 write(ADDR, data)

Return nothing

  • data can 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.4 read(ADDR, byte_size=1)

Return integer (a byte), or list of integers (bytes) if byte_size >= 0

  • byte_size can 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) )
# 2

2.1.5 writeread(ADDR, data, byte_size=1)

Return integer (a byte), or list of integer (bytes) if byte_size >= 0

  • data can be a byte or list of bytes.
  • byte_size can 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.2 I2CDevice(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.1 status_code

As the same as status_code in I2C.

2.2.2 write(data)

As the same as write(addr, data) in I2C, but does not need address.

2.2.3 read(byte_size=1)

As the same as read(addr, byte_size) in I2C, but does not need address.

2.2.4 writeread(data, byte_size=1)

As the same as writeread(addr, data, byte_size=1) in I2C, but does not need address.

2.3 getBit(byte, bin_n, bin_m=-1)

Return integer (a bit or bits)

  • bin_n should be >= 0
  • bin_m can be empty (default is -1 but will overwrote with bin_n)
  • bin_n or bin_m can be larger than byte's size
from pyi2c import getBit

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

print( getBit(byte, 1) )
# 1
  • Get multi bits from #n to #m of byte
print( getBit(byte, 4, 3) )
# 3 = 0b10
print( getBit(byte, 3, 4) )
# 3 = 0b10, the same as previous

  • Recommend usage
if getBit(byte, 4) == 0b1:
    print('hoge')
# 'hoge'

For developers

Build

python3 -m build

Upload

python3 -m twine upload dist/*

Reference

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyi2c-0.3.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyi2c-0.3.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pyi2c-0.3.0.tar.gz.

File metadata

  • Download URL: pyi2c-0.3.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for pyi2c-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5bbef4df21fbf01bc3b6d09ce69dfcffd8550efbaceddbd056e9de2309446d16
MD5 3b4c8b98e6f08d106e3ec24bf3834933
BLAKE2b-256 78919dd5d5e6d6faa74205d555f6542ebc36761e594fa9020ace1ab40fd37710

See more details on using hashes here.

File details

Details for the file pyi2c-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pyi2c-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for pyi2c-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3e51b9d7c41bf9e5e1c3d078c5709ebd605c6b540e90cf374b04d65b94043c7
MD5 d14f8d023d78d62ebb2a969327a5d5e1
BLAKE2b-256 aba54e04bbec8fbae76c4e2d6a6de1668e9f69010fe6f8cbc8378ba5b27ac11a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page