The smbus2 library helps your Raspberry Pi talk to other devices using the I2C communication method. It makes reading and writing data easy and clear.
When you want to read sensor data like temperature or light from an I2C device.
When you need to control small devices like displays or motors connected via I2C.
When you want to send commands to an I2C device to change its settings.
When you want to build a project that uses multiple devices communicating over I2C.
Syntax
Raspberry Pi
from smbus2 import SMBus
with SMBus(bus_number) as bus:
# Read or write commands here
data = bus.read_byte_data(device_address, register)
bus.write_byte_data(device_address, register, value)
bus_number is usually 1 on Raspberry Pi models.
Use with SMBus(bus_number) as bus: to open and close the connection safely.
Examples
This reads one byte from register 0x00 of the device at address 0x48.
Raspberry Pi
from smbus2 import SMBus
with SMBus(1) as bus:
device_address = 0x48
register = 0x00
value = bus.read_byte_data(device_address, register)
print(f"Value read: {value}")
This writes the value 255 (0xFF) to register 0x01 of the device at address 0x40.
Raspberry Pi
from smbus2 import SMBus
with SMBus(1) as bus:
device_address = 0x40
register = 0x01
bus.write_byte_data(device_address, register, 0xFF)
This reads 4 bytes starting from register 0x00 from the device at address 0x50.
Raspberry Pi
from smbus2 import SMBus
with SMBus(1) as bus:
device_address = 0x50
block = bus.read_i2c_block_data(device_address, 0x00, 4)
print(f"Block data: {block}")
Sample Program
This program opens the I2C bus 1, reads one byte from register 0x00 of the device at address 0x48, and prints the value.
Raspberry Pi
from smbus2 import SMBus
# This example reads a byte from a device at address 0x48# and prints the value.def read_sensor():
device_address = 0x48# Example device address
register = 0x00# Register to read fromwith SMBus(1) as bus:
value = bus.read_byte_data(device_address, register)
print(f"Sensor value: {value}")
if __name__ == "__main__":
read_sensor()
OutputSuccess
Important Notes
Make sure I2C is enabled on your Raspberry Pi using raspi-config.
Check the device address with i2cdetect -y 1 before running your program.
Always use with SMBus() to avoid leaving the bus open.
Summary
smbus2 helps your Raspberry Pi communicate with I2C devices easily.
Use it to read and write bytes or blocks of data to device registers.
Always open the bus safely with with SMBus(bus_number) as bus:.
Practice
(1/5)
1. What is the main purpose of the smbus2 library when used with a Raspberry Pi?
easy
A. To control the Raspberry Pi's GPIO pins directly
B. To connect the Raspberry Pi to Wi-Fi networks
C. To communicate with I2C devices by reading and writing data
D. To manage file storage on the Raspberry Pi
Solution
Step 1: Understand the role of smbus2
The smbus2 library is designed to help the Raspberry Pi communicate with I2C devices by sending and receiving data.
Step 2: Compare with other options
Options A, B, and D relate to GPIO pins, Wi-Fi networks, and file storage, which are not the functions of smbus2.
Final Answer:
To communicate with I2C devices by reading and writing data -> Option C
Quick Check:
smbus2 = I2C communication [OK]
Hint: Remember smbus2 is for I2C device communication only [OK]
Common Mistakes:
Confusing smbus2 with Wi-Fi or GPIO libraries
Thinking smbus2 manages files or storage
Assuming smbus2 controls hardware pins directly
2. Which of the following is the correct way to open an I2C bus safely using smbus2 in Python?
easy
A. open(SMBus(1))
# use bus
B. bus = SMBus(1)
# use bus
bus.close()
C. bus = SMBus()
# use bus
D. with SMBus(1) as bus:
# use bus
Solution
Step 1: Identify the recommended safe usage
The recommended way is to use a context manager: with SMBus(1) as bus: which automatically opens and closes the bus safely.
Step 2: Check other options for correctness
bus = SMBus(1)
# use bus
bus.close() manually opens and closes but is less safe. bus = SMBus()
# use bus misses the bus number. open(SMBus(1))
# use bus uses invalid syntax.
Final Answer:
with SMBus(1) as bus:
# use bus -> Option D
Quick Check:
Use 'with' to open SMBus safely [OK]
Hint: Use 'with SMBus(bus_number) as bus:' for safe bus handling [OK]
Common Mistakes:
Not specifying the bus number when creating SMBus
Forgetting to close the bus after use
Using invalid syntax like open() with SMBus
3. What will be the output of this code snippet using smbus2?
from smbus2 import SMBus
with SMBus(1) as bus:
data = bus.read_byte_data(0x20, 0x01)
print(data)
Assuming device at address 0x20 returns 42 for register 0x01.
medium
A. 42
B. 0x20
C. Error: Device not found
D. None
Solution
Step 1: Understand the read_byte_data function
The function reads a byte from the device at address 0x20, register 0x01. The problem states it returns 42.
Step 2: Check the print output
The variable 'data' will hold the value 42, so printing it outputs 42.
Final Answer:
42 -> Option A
Quick Check:
read_byte_data returns device register value [OK]
Hint: read_byte_data returns the byte value from device register [OK]
Common Mistakes:
Confusing device address with returned data
Expecting hexadecimal output instead of decimal
Assuming None or error without device connection
4. Identify the error in this code snippet using smbus2:
from smbus2 import SMBus
bus = SMBus(1)
data = bus.read_byte_data(0x50)
bus.close()
medium
A. Missing register address in read_byte_data call
B. Bus number 1 is invalid
C. SMBus class not imported correctly
D. bus.close() should be called before reading data
Solution
Step 1: Check read_byte_data parameters
The method requires two arguments: device address and register address. Here only device address 0x50 is given.
Step 2: Verify other parts
Bus number 1 is valid, SMBus is imported correctly, and closing after reading is correct.
Final Answer:
Missing register address in read_byte_data call -> Option A
Quick Check:
read_byte_data needs device and register addresses [OK]
Hint: read_byte_data needs two arguments: device and register [OK]
Common Mistakes:
Passing only device address to read_byte_data
Closing bus before reading data
Using wrong bus number without checking hardware
5. You want to write the byte value 0xAB to register 0x10 of an I2C device at address 0x30 using smbus2. Which code snippet correctly does this inside a safe bus context?
hard
A. with SMBus(1) as bus:
bus.write_byte_data(0x10, 0x30, 0xAB)
B. with SMBus(1) as bus:
bus.write_byte_data(0x30, 0x10, 0xAB)
C. with SMBus(1) as bus:
bus.write_byte(0x30, 0x10, 0xAB)
D. bus = SMBus(1)
bus.write_byte(0x30, 0x10, 0xAB)
bus.close()
Solution
Step 1: Identify correct method and parameters
The method to write a byte to a device register is write_byte_data(device_address, register, value). with SMBus(1) as bus:
bus.write_byte_data(0x30, 0x10, 0xAB) uses this correctly.
Step 2: Check safe bus usage and parameter order
with SMBus(1) as bus:
bus.write_byte_data(0x30, 0x10, 0xAB) uses 'with' for safe bus handling and correct parameter order. bus = SMBus(1)
bus.write_byte(0x30, 0x10, 0xAB)
bus.close() uses wrong method name and manual close. with SMBus(1) as bus:
bus.write_byte(0x30, 0x10, 0xAB) uses wrong method. with SMBus(1) as bus:
bus.write_byte_data(0x10, 0x30, 0xAB) swaps register and device address.
Final Answer:
with SMBus(1) as bus:
bus.write_byte_data(0x30, 0x10, 0xAB) -> Option B