smbus2

0.6.1 · active · verified Sun Apr 12

smbus2 is a pure Python implementation of the `python-smbus` package, designed as a drop-in replacement for `smbus-cffi` or `smbus-python`. It provides an interface for I2C and SMBus communication on Linux-based systems, offering enhanced features like SMBus Packet Error Checking (PEC), support for context managers for robust resource handling, and improved error management. Actively maintained, the library is currently at version 0.6.1, with a regular release cadence addressing bug fixes and minor improvements, making it a recommended choice for embedded I2C communication, particularly on platforms like Raspberry Pi.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic I2C read and write operations using the `SMBus` class with a context manager, ensuring proper resource cleanup. It also shows how to set I2C bus, device address, and register address, with placeholders for actual values and environment variables for safer execution.

import os
from smbus2 import SMBus

# Example: Read a byte from an I2C device (e.g., at address 0x27, register 0x00)
# Replace `1` with the correct I2C bus number for your system (e.g., 0 for older Raspberry Pis, 1 for newer)
# Replace `0x27` with your device's I2C address
# Replace `0x00` with the register to read from

I2C_BUS_NUMBER = int(os.environ.get('I2C_BUS_NUMBER', '1')) # Default to bus 1
DEVICE_ADDRESS = int(os.environ.get('I2C_DEVICE_ADDRESS', '0x27'), 16)
REGISTER_ADDRESS = int(os.environ.get('I2C_REGISTER_ADDRESS', '0x00'), 16)

try:
    # Using 'with' statement ensures the bus is properly closed
    with SMBus(I2C_BUS_NUMBER) as bus:
        byte_data = bus.read_byte_data(DEVICE_ADDRESS, REGISTER_ADDRESS)
        print(f"Read byte 0x{byte_data:02X} from device 0x{DEVICE_ADDRESS:02X}, register 0x{REGISTER_ADDRESS:02X} on bus {I2C_BUS_NUMBER}.")

        # Example: Write a byte to the same device/register
        write_value = 0xAA # Example value to write
        bus.write_byte_data(DEVICE_ADDRESS, REGISTER_ADDRESS, write_value)
        print(f"Written byte 0x{write_value:02X} to device 0x{DEVICE_ADDRESS:02X}, register 0x{REGISTER_ADDRESS:02X} on bus {I2C_BUS_NUMBER}.")

except FileNotFoundError:
    print(f"Error: I2C bus {I2C_BUS_NUMBER} not found. Ensure I2C is enabled and the bus number is correct.")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →