0
0
Embedded Cprogramming~10 mins

Writing data to I2C device in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Writing data to I2C device
Start I2C Communication
Send Device Address + Write Bit
Wait for ACK from Device
Send Data Byte
Wait for ACK from Device
Repeat Send Data Byte and Wait for ACK if more data
Send Stop Condition
End
The process starts communication, sends the device address with write intent, sends data bytes waiting for acknowledgments, and ends with a stop signal.
Execution Sample
Embedded C
i2c_start();
i2c_write(0x50 << 1 | 0);
i2c_write(0xA5);
i2c_stop();
This code writes one byte (0xA5) to an I2C device at address 0x50.
Execution Table
StepActionData SentACK ReceivedNext Step
1Start I2C communicationStart conditionN/ASend device address with write bit
2Send device address + write bit0xA0 (0x50 << 1 | 0)YesSend data byte
3Send data byte0xA5YesSend stop condition
4Send stop conditionStop conditionN/AEnd communication
💡 Stop condition sent, communication ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
i2c_stateIdleAddress sentData sentStopped
ack_flagN/AReceivedReceivedN/A
Key Moments - 3 Insights
Why do we shift the device address left by 1 and add 0?
The device address is 7 bits; shifting left makes room for the read/write bit (0 for write). See execution_table step 2 where 0x50 is shifted and combined with 0.
What happens if the device does not send an ACK after address or data?
The communication should stop or retry because the device did not acknowledge. In the execution_table, ACK is 'Yes' at steps 2 and 3, meaning communication continues.
Why is the stop condition important?
It signals the end of communication on the I2C bus. Without it, the device might wait for more data. See execution_table step 4 where stop condition ends the process.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what data is sent at step 2?
A0xA0
B0xA5
C0x50
DStop condition
💡 Hint
Check the 'Data Sent' column at step 2 in the execution_table.
At which step does the device acknowledge the data byte?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'ACK Received' column for the data byte send step in the execution_table.
If the device does not acknowledge at step 3, what should happen?
AContinue sending more data bytes
BRestart communication immediately
CSend stop condition and end communication
DIgnore and wait
💡 Hint
Refer to key_moments about handling missing ACK after data byte.
Concept Snapshot
I2C write sequence:
1. Start condition
2. Send 7-bit device address + 0 (write bit)
3. Wait for ACK
4. Send data byte(s), wait for ACK each
5. Send stop condition
Use shifting for address + write bit.
Stop signals end of communication.
Full Transcript
Writing data to an I2C device involves starting communication, sending the device address with the write bit, waiting for acknowledgment, sending data bytes with acknowledgments, and ending with a stop condition. The device address is shifted left by one bit to make space for the read/write bit. Each step waits for the device to acknowledge before continuing. If acknowledgment is missing, communication should stop to avoid errors. The stop condition signals the end of the data transfer on the I2C bus.