Bird
0
0
Raspberry Piprogramming~10 mins

smbus2 library for I2C 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 smbus2 library.

Raspberry Pi
from smbus2 import [1]
Drag options to blanks, or click blank then click option'
ADevice
BI2C
CBus
DSMBus
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'I2C' instead of 'SMBus' as the import.
Trying to import 'Bus' which does not exist in smbus2.
2fill in blank
medium

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

Raspberry Pi
bus = [1](1)
Drag options to blanks, or click blank then click option'
ASMBus
BI2CBus
CBus
DDevice
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'I2CBus' which is not part of smbus2.
Using 'Bus' which is undefined.
3fill in blank
hard

Fix the error in the code to write a byte value 0x10 to device address 0x20 at register 0x01.

Raspberry Pi
bus.write_byte_data([1], 0x01, 0x10)
Drag options to blanks, or click blank then click option'
A0x10
B0x01
C0x20
D0x00
Attempts:
3 left
💡 Hint
Common Mistakes
Using the register or value as the device address.
Using 0x00 which is usually not the device address.
4fill in blank
hard

Fill both blanks to read a byte from register 0x05 of device address 0x30.

Raspberry Pi
data = bus.[1]([2], 0x05)
Drag options to blanks, or click blank then click option'
Aread_byte_data
B0x30
C0x20
Dwrite_byte_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write_byte_data' instead of 'read_byte_data'.
Using wrong device address like 0x20.
5fill in blank
hard

Fill all three blanks to write a block of data [0x01, 0x02, 0x03] to register 0x10 of device address 0x40.

Raspberry Pi
bus.[1]([2], 0x10, [3])
Drag options to blanks, or click blank then click option'
Awrite_i2c_block_data
B0x40
C[0x01, 0x02, 0x03]
Dread_i2c_block_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'read_i2c_block_data' instead of write.
Using wrong device address or data format.