Bird
0
0
Raspberry Piprogramming~10 mins

Writing commands to I2C device in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Writing commands to I2C device
Start
Open I2C bus
Set device address
Prepare command/data
Write command to device
Check for errors
Close I2C bus
End
This flow shows how a Raspberry Pi opens the I2C bus, sets the device address, writes a command, checks for errors, and closes the bus.
Execution Sample
Raspberry Pi
import smbus
bus = smbus.SMBus(1)
address = 0x20
bus.write_byte(address, 0xFF)
# smbus.SMBus object does not have a close() method
# so bus.close() is removed
This code opens I2C bus 1, writes the byte 0xFF to device at address 0x20.
Execution Table
StepActionDetailsResult
1Import smbusLoad I2C librarysmbus module ready
2Open I2C busbus = smbus.SMBus(1)bus object created for bus 1
3Set device addressaddress = 0x20address stored as 32 decimal
4Write bytebus.write_byte(0x20, 0xFF)0xFF sent to device at 0x20
5EndNo more commandsProgram ends
💡 All commands executed successfully
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
busNoneSMBus object for bus 1SMBus object for bus 1SMBus object for bus 1SMBus object for bus 1
addressNoneNone0x20 (32 decimal)0x20 (32 decimal)0x20 (32 decimal)
Key Moments - 3 Insights
Why do we need to open the I2C bus before writing?
The bus must be opened to create a communication channel; see Step 2 in execution_table where bus object is created before any write.
What happens if the device address is wrong?
The write command will fail or no device will respond; Step 4 shows writing to address 0x20, if incorrect, an error or no effect occurs.
Why close the bus after writing?
The smbus.SMBus object does not require explicit closing; resources are managed by the OS. Closing is not needed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'address' after Step 3?
ANone
B0xFF
C0x20 (32 decimal)
DSMBus object
💡 Hint
Check variable_tracker row for 'address' after Step 3
At which step is the byte 0xFF sent to the device?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at execution_table action column for 'Write byte'
If we forget to close the bus, what changes in the execution_table?
ANo change at all
BStep 4 would fail
CStep 3 would change address
DStep 5 would be missing or different
💡 Hint
smbus.SMBus does not require closing, so no change
Concept Snapshot
Open I2C bus with smbus.SMBus(bus_number)
Set device address as hex (e.g., 0x20)
Write commands using write_byte(address, data)
No need to close bus explicitly
Always check for errors during write
Full Transcript
This example shows how to write commands to an I2C device using a Raspberry Pi. First, the smbus library is imported to access I2C functions. Then, the I2C bus 1 is opened by creating an SMBus object. The device address is set to 0x20. Using the write_byte method, the byte 0xFF is sent to the device at that address. The smbus.SMBus object does not require explicit closing; resources are managed by the OS. Each step is important: opening the bus creates the communication channel, setting the correct address targets the right device, and writing sends the command. If the address is wrong, the device won't respond. Forgetting to close the bus does not cause resource conflicts because closing is not needed. This step-by-step flow helps beginners see how commands are sent to I2C devices on Raspberry Pi.