Bird
0
0
Arduinoprogramming~10 mins

Reading I2C sensor data in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reading I2C sensor data
Start
Initialize I2C bus
Request data from sensor
Wait for sensor response
Read bytes from sensor
Process data
Use or display data
Repeat or End
The program starts by setting up the I2C bus, then requests data from the sensor, waits for the response, reads the data bytes, processes them, and finally uses or shows the data.
Execution Sample
Arduino
Wire.begin();
Wire.requestFrom(0x68, 2);
if (Wire.available() >= 2) {
  int data = (Wire.read() << 8) | Wire.read();
}
This code initializes I2C, requests 2 bytes from sensor at address 0x68, and reads the data if available.
Execution Table
StepActionI2C Bus StateData RequestedData AvailableData ReadResult
1Wire.begin()I2C bus initialized----
2Wire.requestFrom(0x68, 2)Bus active2 bytes from 0x68Waiting--
3Check Wire.available()Bus active2 bytes requested2 bytes available-Ready to read
4Wire.read() first byteBus active-2 bytes availableFirst byte read (e.g. 0x1A)-
5Wire.read() second byteBus active-1 byte leftSecond byte read (e.g. 0xCF)-
6Combine bytes----Data = 0x1ACF (6863 decimal)
7Use data----Data processed or displayed
8End or repeat----Loop or stop
💡 Execution stops after reading and processing requested bytes from sensor.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6Final
dataundefined0x1A00 (first byte shifted)0x1ACF (combined two bytes)0x1ACF0x1ACF
Key Moments - 3 Insights
Why do we check Wire.available() before reading?
Because Wire.available() tells us how many bytes the sensor has sent and are ready to read. Reading without checking might cause errors or wrong data. See step 3 in execution_table.
Why do we shift the first byte by 8 bits before combining?
Because the sensor sends data in two bytes: high byte and low byte. Shifting the first byte left by 8 bits moves it to the high position, so when combined with the second byte, it forms the full 16-bit value. See step 6 in execution_table.
What happens if the sensor sends fewer bytes than requested?
Wire.available() will return fewer bytes, so the if condition fails and we don't read incomplete data. This prevents errors. See step 3 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, how many bytes are available to read?
A1 byte
B0 bytes
C2 bytes
D3 bytes
💡 Hint
Check the 'Data Available' column at step 3 in execution_table.
At which step is the data combined into a single integer?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look for the step where bytes are shifted and combined in execution_table.
If Wire.available() returned 1 instead of 2 at step 3, what would happen?
AData would be read correctly
BThe if condition would fail and no data read
CThe program would crash immediately
DThe sensor would send more data automatically
💡 Hint
Refer to the if condition check in step 3 of execution_table and key_moments.
Concept Snapshot
Reading I2C sensor data:
1. Initialize I2C with Wire.begin()
2. Request bytes from sensor with Wire.requestFrom(address, bytes)
3. Check Wire.available() to confirm bytes ready
4. Read bytes with Wire.read()
5. Combine bytes if needed (e.g., shift high byte)
6. Use or display the processed data
Full Transcript
This visual execution shows how to read data from an I2C sensor using Arduino's Wire library. First, the I2C bus is initialized with Wire.begin(). Then, the program requests 2 bytes from the sensor at address 0x68 using Wire.requestFrom(). It waits until Wire.available() confirms 2 bytes are ready to read. The program reads the first byte, shifts it left by 8 bits, then reads the second byte and combines both to form a 16-bit integer. Finally, the data is processed or displayed. The execution table tracks each step, showing bus state, data availability, and variable changes. Key moments clarify why checking Wire.available() is important and why bytes are combined with shifting. The quiz tests understanding of these steps. This step-by-step trace helps beginners see exactly how I2C sensor data is read and handled in Arduino code.