0
0
Embedded Cprogramming~10 mins

Reading data from I2C device in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reading data from I2C device
Start I2C communication
Send device address + write bit
Send register address to read
Restart I2C communication
Send device address + read bit
Read data byte(s) from device
Send NACK and stop condition
End communication
This flow shows the steps to read data from an I2C device: start communication, send device and register addresses, restart for reading, read data, then stop.
Execution Sample
Embedded C
uint8_t read_i2c_data(uint8_t dev_addr, uint8_t reg_addr) {
    i2c_start();
    i2c_write(dev_addr << 1 | 0); // write mode
    i2c_write(reg_addr);
    i2c_start();
    i2c_write(dev_addr << 1 | 1); // read mode
    uint8_t data = i2c_read_nack();
    i2c_stop();
    return data;
}
This function reads one byte from a register of an I2C device by following the I2C read protocol.
Execution Table
StepActionI2C Bus StateData Sent/ReceivedResult
1i2c_start()Start condition sentNoneBus ready for address
2i2c_write(dev_addr << 1 | 0)Address + Write bit sentDevice address + 0ACK received from device
3i2c_write(reg_addr)Register address sentRegister addressACK received from device
4i2c_start()Repeated start condition sentNoneBus ready for read address
5i2c_write(dev_addr << 1 | 1)Address + Read bit sentDevice address + 1ACK received from device
6i2c_read_nack()Reading data byteData byte receivedData stored in variable
7i2c_stop()Stop condition sentNoneCommunication ended
💡 Communication ends after stop condition; data byte successfully read.
Variable Tracker
VariableStartAfter Step 6Final
dev_addrinputunchangedunchanged
reg_addrinputunchangedunchanged
dataundefinedreceived byte from devicereturned value
Key Moments - 3 Insights
Why do we send a repeated start condition before reading?
The repeated start (step 4) tells the device we want to switch from writing the register address to reading data without releasing the bus, as shown in the execution_table.
What does the 'ACK received from device' mean after sending address or data?
It means the device acknowledged it received the byte correctly and is ready for the next step, ensuring communication continues smoothly (see steps 2, 3, and 5).
Why do we send a NACK after reading the data byte?
Sending a NACK after reading (step 6) tells the device we do not want more data, so it stops sending bytes before we send the stop condition.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the data variable value after step 6?
AThe data byte received from the device
BThe device address with read bit
CThe register address sent
DUndefined, no data read yet
💡 Hint
Check the 'Data Sent/Received' and 'Result' columns at step 6 in the execution_table.
At which step does the repeated start condition occur?
AStep 2
BStep 6
CStep 4
DStep 7
💡 Hint
Look for 'Repeated start condition sent' in the 'I2C Bus State' column of the execution_table.
If the device does not acknowledge the address at step 2, what happens?
AThe device sends data anyway
BThe communication should be stopped or retried
CThe communication continues normally
DThe register address is sent twice
💡 Hint
Recall that 'ACK received from device' means device is ready; no ACK means communication error (see step 2).
Concept Snapshot
I2C read sequence:
1. Start condition
2. Send device address + write bit
3. Send register address
4. Repeated start
5. Send device address + read bit
6. Read data byte + NACK
7. Stop condition
Always check ACK after each byte.
Full Transcript
This visual execution shows how to read a byte from an I2C device. First, the master sends a start condition, then the device address with the write bit to select the register. After sending the register address, a repeated start is sent to switch to read mode. The device address with the read bit is sent next, followed by reading the data byte. Finally, a NACK and stop condition end the communication. Variables track the device address, register address, and the data read. Key moments include why the repeated start is needed, the meaning of ACK, and why a NACK is sent after reading. The quizzes test understanding of data values, steps, and error handling.