Bird
0
0
Raspberry Piprogramming~10 mins

smbus2 library for I2C in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - smbus2 library for I2C
Import smbus2
Open I2C bus
Create SMBus object
Send/Receive data
Close SMBus
End communication
This flow shows how the smbus2 library opens the I2C bus, communicates by sending or receiving data, then closes the bus.
Execution Sample
Raspberry Pi
from smbus2 import SMBus

with SMBus(1) as bus:
    data = bus.read_byte_data(0x20, 0x00)
    print(data)
This code reads one byte from device address 0x20 register 0x00 and prints it.
Execution Table
StepActionBus NumberDevice AddressRegisterData ReadOutput
1Import SMBus from smbus2-----
2Open SMBus(1)1----
3Read byte from device 0x20 register 0x0010x200x000x3A-
4Print data---0x3A58
5Close SMBus-----
💡 SMBus closed after reading and printing data
Variable Tracker
VariableStartAfter Step 3After Step 4Final
busNoneSMBus(1) objectSMBus(1) objectClosed
dataNone0x3A0x3A0x3A
Key Moments - 3 Insights
Why do we use 'with SMBus(1) as bus' instead of just 'bus = SMBus(1)'?
Using 'with' ensures the bus is automatically closed after use, preventing resource leaks, as shown in execution_table step 5.
What does 'read_byte_data(0x20, 0x00)' do exactly?
It reads one byte from device at address 0x20 from register 0x00, shown in execution_table step 3 where data 0x3A is read.
Why is the printed output 58 when data is 0x3A?
0x3A is hexadecimal for decimal 58, so print(data) outputs 58 as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'data' after step 3?
A0x20
B0x3A
C0x00
DNone
💡 Hint
Check the 'Data Read' column in execution_table row for step 3
At which step is the SMBus closed?
AStep 5
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for when bus closes
If we change device address to 0x30, which column in the execution table changes?
ABus Number
BData Read
CDevice Address
DOutput
💡 Hint
Device Address column shows the I2C device address used
Concept Snapshot
smbus2 library lets Raspberry Pi talk to I2C devices.
Use 'with SMBus(bus_number) as bus:' to open and auto-close.
Use bus.read_byte_data(addr, reg) to read a byte.
Device address and register are hex numbers.
Print outputs decimal values.
Always close bus to free resources.
Full Transcript
This lesson shows how to use the smbus2 library on Raspberry Pi to communicate with I2C devices. First, we import SMBus from smbus2. Then we open the I2C bus number 1 using 'with SMBus(1) as bus:', which ensures the bus closes automatically after use. We read one byte from device address 0x20 at register 0x00 using bus.read_byte_data(0x20, 0x00). The data read is 0x3A in hexadecimal, which prints as 58 in decimal. Finally, the bus closes automatically. Key points include using 'with' to manage resources, understanding device address and register parameters, and knowing that printed output is decimal. This step-by-step trace helps beginners see how variables change and when actions happen.