Skip to content

Python package to support multi platform I2C bus integrations

License

Notifications You must be signed in to change notification settings

sparkfun/Qwiic_I2C_Py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Qwiic I2C Python Package

Sparkfun Qwiic I2C - Python Package

PyPi Version GitHub issues License X

Python package to support multi platform I2C bus integrations for the SparkFun qwiic ecosystem

This package can be used in conjunction with the overall SparkFun qwiic Python Package

New to qwiic? Take a look at the entire SparkFun qwiic ecosystem.

Contents

Supported Platforms

See the MicroPython Downloads Page for more boards compatible with MicroPython.

Python Platform Boards
Python Linux Raspberry Pi , NVIDIA Jetson Orin Nano via the SparkFun Qwiic SHIM
MicroPython Raspberry Pi - RP2, ESP32 SparkFun RP2040 Thing+, SparkFun RP2350 Thing+, SparkFun ESP32 Thing+
CircuitPython Raspberry Pi - RP2, ESP32 SparkFun RP2040 Thing+, SparkFun RP2350 Thing+, SparkFun ESP32 Thing+

Note

The listed supported platforms and boards are the primary platform targets tested. It is fully expected that this package will work across a wide variety of Python enabled systems.

Dependencies

The Raspberry Pi/Single Board Computer Linux driver of this package is dependent on smbus

Documentation

The SparkFun qwiic I2C module documentation is hosted at ReadTheDocs

Installation

The first step to using this package is installing it on your system. The install method depends on the python platform. The following sections outline installation on Python, MicroPython and CircuitPython.

Python

PyPi Installation

The package is primarily installed using the pip3 command, downloading the package from the Python Index - "PyPi".

Note - the below instructions outline installation on a Linux-based (Raspberry Pi) system.

First, setup a virtual environment from a specific directory using venv:

python3 -m venv ~/sparkfun_venv

You can pass any path instead of ~/sparkfun_venv, just make sure you use the same one for all future steps. For more information on venv click here.

Next, install the qwiic package with:

~/sparkfun_venv/bin/pip3 install sparkfun-qwiic-i2c

Now you should be able to run any example or custom python scripts that have import qwiic_i2c by running e.g.:

~/sparkfun_venv/bin/python3 example_script.py

MicroPython Installation

If not already installed, follow the instructions here to install mpremote on your computer.

Connect a device with MicroPython installed to your computer and then install the package directly to your device with mpremote mip.

mpremote mip install github:sparkfun/qwiic_i2c_py

CircuitPython Installation

If not already installed, follow the instructions here to install CircUp on your computer.

Ensure that you have the latest version of the SparkFun Qwiic CircuitPython bundle.

circup bundle-add sparkfun/qwiic_py

Finally, connect a device with CircuitPython installed to your computer and then install the package directly to your device with circup.

circup install --py qwiic_i2c

Examples

This package is used extensively by the python modules for the SparkFun qwiic ecosystem. References to the modules can be found in the sparkfun-python github topic or in the drivers directories of Qwiic Py.

General package use examples:

# Import the package
import qwiic_i2c

# Get the default I2C bus
my_bus = qwiic_i2c.get_i2c_driver()

# Linux (Raspberry Pi) - Specify I2C bus index
my_bus = qwiic_i2c.get_i2c_driver(iBus = 1)

# MicroPython and CircuitPython - Specify SDA and SCL pins, and frequency
my_bus = qwiic_i2c.get_i2c_driver(sda=0, scl=1, freq=100000)

# Perform scan of I2C bus
scan_list = my_bus.scan()
print("Bus scan:", scan_list)

# Check if a device with the specified address is connected
ping_result = my_bus.ping(device_address)
print("Device is connected:", ping_result)

# Read one byte from the specified address
read_data = my_bus.read_byte(device_address, register_address)
print("Read byte:", read_data)

# Read one word (2 bytes) from the specified address
read_data = my_bus.read_word(device_address, register_address)
print("Read word:", read_data)

# Read several bytes from the specified address
read_data = my_bus.read_block(device_address, register_address, num_bytes_to_read)
print("Read block:", read_data)

# Write one byte to the specified address
my_bus.write_byte(device_address, register_address, write_data)

# Write one word (2 bytes) to the specified address
my_bus.write_word(device_address, register_address, write_data)

# Write several bytes to the specified address
my_bus.write_block(device_address, register_address, write_data)

SparkFun - Start Something