Bird
0
0
Raspberry Piprogramming~10 mins

Writing commands to I2C device in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the SMBus module needed for I2C communication.

Raspberry Pi
from smbus2 import [1]
Drag options to blanks, or click blank then click option'
AI2C
BSMBus
CBus
DDevice
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'I2C' instead of 'SMBus' will cause an import error.
Using 'Bus' alone is not correct because the full class name is SMBus.
2fill in blank
medium

Complete the code to create an SMBus object for I2C bus number 1.

Raspberry Pi
bus = SMBus([1])
Drag options to blanks, or click blank then click option'
A1
B0
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using bus 0 may cause errors if the bus is not enabled.
Using bus 2 or 3 is uncommon and may not exist on the device.
3fill in blank
hard

Fix the error in the code to write a single byte command 0x20 to the device at address 0x40.

Raspberry Pi
bus.write_byte([1], 0x20)
Drag options to blanks, or click blank then click option'
A64
B0x20
C32
D0x40
Attempts:
3 left
💡 Hint
Common Mistakes
Using the command byte as the device address causes communication failure.
Using decimal 32 or 64 instead of hex 0x40 may cause confusion but 0x40 and 64 are equivalent; however, the code expects hex.
4fill in blank
hard

Fill both blanks to write a block of data [0x01, 0x02, 0x03] to register 0x10 of device 0x50.

Raspberry Pi
bus.write_i2c_block_data([1], [2], [0x01, 0x02, 0x03])
Drag options to blanks, or click blank then click option'
A0x50
B0x10
C0x20
D0x40
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping device and register addresses causes wrong data writes.
Using incorrect addresses will cause the device not to respond.
5fill in blank
hard

Fill all three blanks to write the value 0xFF to register 0x01 of device 0x60 using write_byte_data.

Raspberry Pi
bus.write_byte_data([1], [2], [3])
Drag options to blanks, or click blank then click option'
A0x60
B0x01
C0xFF
D0x10
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up register and data values.
Using wrong device address causes no communication.