Bird
0
0
Raspberry Piprogramming~10 mins

Multiple I2C devices on same bus 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 to communicate with I2C devices.

Raspberry Pi
from smbus2 import [1]
Drag options to blanks, or click blank then click option'
ABus
BI2C
CDevice
DSMBus
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a wrong class like I2C or Bus instead of SMBus.
2fill in blank
medium

Complete the code to open I2C bus number 1 on Raspberry Pi.

Raspberry Pi
bus = SMBus([1])
Drag options to blanks, or click blank then click option'
A0
B1
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using bus 0 or 2 which may not be enabled or available.
3fill in blank
hard

Fix the error in the code to read a byte from the device at address 0x40.

Raspberry Pi
data = bus.read_byte([1])
Drag options to blanks, or click blank then click option'
A0x40
B0x04
C40
D64
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect values like 40 (0x28 hex) or 0x04 which point to different devices.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps device addresses to their read byte values.

Raspberry Pi
device_data = {addr: bus.read_byte(addr) for addr in [1] if addr [2] 0x00}
Drag options to blanks, or click blank then click option'
A[0x20, 0x40, 0x60]
B[0x10, 0x30, 0x50]
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong address lists or incorrect comparison operators like '>=' which includes 0x00.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that stores device addresses as keys, their read byte values as values, and only includes devices with values less than 100.

Raspberry Pi
filtered_data = { [1] : bus.read_byte([2]) for [3] in [0x20, 0x40, 0x60] if bus.read_byte(_) < 100 }
Drag options to blanks, or click blank then click option'
Aaddr
Bdevice
C_
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently or not matching the loop variable in the condition.