Bird
0
0
Raspberry Piprogramming~10 mins

Reading sensor data over 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 I2C library.

Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Asmbus2
Brandom
Ctime
Dos
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like random or os.
Forgetting to import the I2C communication library.
2fill in blank
medium

Complete the code to create an I2C bus object.

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

Fix the error in reading a byte from the sensor at address 0x48.

Raspberry Pi
data = bus.read_byte_data([1], 0x00)
Drag options to blanks, or click blank then click option'
A0x48
B0x00
C0x04
D0x49
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong I2C address causing no response or errors.
Confusing register address with device address.
4fill in blank
hard

Fill both blanks to read two bytes and combine them into a 16-bit value.

Raspberry Pi
high = bus.read_byte_data(0x48, [1])
low = bus.read_byte_data(0x48, [2])
value = (high << 8) | low
Drag options to blanks, or click blank then click option'
A0x00
B0x01
C0x02
D0x03
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect register addresses causing wrong data.
Mixing up high and low byte registers.
5fill in blank
hard

Fill all three blanks to convert the raw sensor value to temperature in Celsius.

Raspberry Pi
if value & [1]:
    value = value - [2]
temperature = value * [3]
Drag options to blanks, or click blank then click option'
A0x8000
B65536
C0.0078125
D0x7FFF
Attempts:
3 left
💡 Hint
Common Mistakes
Ignoring sign bit causing wrong negative temperature.
Using wrong scale factor leading to incorrect Celsius values.