Bird
0
0
Raspberry Piprogramming~10 mins

Reading sensor data over I2C in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reading sensor data over I2C
Start
Initialize I2C bus
Set sensor address
Send read command
Read data bytes
Process data
Use or display data
End or repeat
This flow shows how a Raspberry Pi talks to a sensor using I2C: start, set up bus, send command, read data, then use it.
Execution Sample
Raspberry Pi
import smbus
bus = smbus.SMBus(1)
sensor_addr = 0x48
data = bus.read_byte_data(sensor_addr, 0x00)
print(f"Sensor data: {data}")
This code reads one byte from a sensor at address 0x48 and prints it.
Execution Table
StepActionI2C Bus StateSensor AddressData ReadOutput
1Import smbus moduleNot initialized---
2Initialize I2C bus 1Bus 1 ready---
3Set sensor address to 0x48Bus 1 ready0x48--
4Send read command to register 0x00Bus 1 ready0x48--
5Read one byte from sensorBus 1 ready0x4872-
6Print sensor dataBus 1 ready0x4872Sensor data: 72
💡 Data read and printed; program ends or can repeat to read again.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
busNoneSMBus(1) objectSMBus(1) objectSMBus(1) objectSMBus(1) object
sensor_addrNoneNone0x480x480x48
dataNoneNoneNone7272
Key Moments - 3 Insights
Why do we initialize the bus with smbus.SMBus(1)?
The number 1 selects the I2C bus on Raspberry Pi (usually bus 1). This is shown in execution_table step 2 where the bus becomes ready.
What does the sensor address 0x48 mean?
It is the unique I2C address of the sensor device. In step 3, we set this address so the bus knows where to send commands.
Why do we read from register 0x00?
Register 0x00 is often the data register in sensors. Step 4 sends the read command to this register to get the sensor value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'data' after step 5?
A0x48
B72
CNone
D1
💡 Hint
Check the 'Data Read' column at step 5 in the execution_table.
At which step is the I2C bus initialized?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column for bus initialization in the execution_table.
If the sensor address changed to 0x49, which execution_table step would change?
AStep 3 only
BStep 5 only
CSteps 3 and 5
DNo steps change
💡 Hint
Sensor address is set at step 3 and used at step 5 in the execution_table.
Concept Snapshot
Reading sensor data over I2C on Raspberry Pi:
- Import smbus and create bus with smbus.SMBus(1)
- Set sensor I2C address (e.g., 0x48)
- Use bus.read_byte_data(address, register) to read data
- Process or print the data
- Repeat as needed for continuous reading
Full Transcript
This example shows how a Raspberry Pi reads data from a sensor using the I2C protocol. First, the smbus module is imported and the I2C bus 1 is initialized. Then, the sensor's I2C address is set to 0x48. A read command is sent to register 0x00 of the sensor. One byte of data is read from the sensor and stored in the variable 'data'. Finally, the data is printed to the screen. The execution table traces each step, showing the bus state, sensor address, data read, and output. Variables change as the program runs, with 'data' holding the sensor value after reading. Key moments clarify why bus 1 is used, what the sensor address means, and why register 0x00 is read. The visual quiz tests understanding of these steps. This process can be repeated to continuously read sensor data.