Bird
Raised Fist0
Embedded Cprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the first step when writing data to an I2C device in embedded C?
easy
A. Read the device address
B. Start the I2C communication
C. Stop the I2C communication
D. Send the data byte

Solution

  1. Step 1: Begin communication with the device

    Writing data to an I2C device always starts with initiating the communication by sending a start condition. After starting, the device address and data can be sent, but the first step must be the start condition.
  2. Final Answer:

    Start the I2C communication -> Option B
  3. Quick Check:

    Start communication = first step [OK]
Hint: Always start communication before sending data [OK]
Common Mistakes:
  • Sending data before starting communication
  • Stopping communication too early
  • Skipping the start condition
2. Which of the following is the correct way to specify the 7-bit I2C device address for writing in embedded C?
easy
A. device_address << 1 | 0
B. device_address >> 1
C. device_address << 1 | 1
D. device_address << 1 & ~1

Solution

  1. Step 1: Understand I2C address format

    The 7-bit device address is shifted left by one bit to make room for the read/write bit as the least significant bit. For writing, the least significant bit must be 0, so OR with 0 is correct.
  2. Final Answer:

    device_address << 1 | 0 -> Option A
  3. Quick Check:

    Write bit = 0, address shifted left = correct [OK]
Hint: Shift address left and set LSB to 0 for write [OK]
Common Mistakes:
  • Setting LSB to 1 for write operation
  • Shifting address right instead of left
  • Not shifting address at all
3. Given the following code snippet, what will be the result of the I2C write operation?
uint8_t device_addr = 0x50;
uint8_t reg_addr = 0x10;
uint8_t data = 0xAB;

I2C_Start();
I2C_Write(device_addr << 1 | 0);
I2C_Write(reg_addr);
I2C_Write(data);
I2C_Stop();
medium
A. Only register address is sent, data is missing
B. Data 0x10 is written to register 0xAB of device 0x50
C. Write operation fails due to wrong device address
D. Data 0xAB is written to register 0x10 of device 0x50

Solution

  1. Step 1: Analyze the address and data sent

    The device address is correctly shifted and write bit set to 0. Then register address 0x10 and data 0xAB are sent in order. The sequence Start -> Address+Write -> Register -> Data -> Stop is correct for writing data to a register.
  2. Final Answer:

    Data 0xAB is written to register 0x10 of device 0x50 -> Option D
  3. Quick Check:

    Correct sequence = data written properly [OK]
Hint: Check address shift and write bit, then data order [OK]
Common Mistakes:
  • Mixing register and data order
  • Not shifting device address
  • Forgetting to send stop condition
4. Identify the error in the following I2C write code snippet:
uint8_t device_addr = 0x68;
uint8_t reg_addr = 0x20;
uint8_t data = 0x55;

I2C_Start();
I2C_Write(device_addr >> 1 | 0);
I2C_Write(reg_addr);
I2C_Write(data);
I2C_Stop();
medium
A. Device address is shifted right instead of left
B. Data byte is sent before register address
C. Stop condition is missing
D. Write bit is set to 1 instead of 0

Solution

  1. Step 1: Check device address shifting

    The device address is incorrectly shifted right instead of left, which corrupts the address sent on the bus. Register and data are sent in correct order, and stop condition is present, so no other errors.
  2. Final Answer:

    Device address is shifted right instead of left -> Option A
  3. Quick Check:

    Address must be shifted left for I2C write [OK]
Hint: Always shift device address left by 1 bit [OK]
Common Mistakes:
  • Shifting address right instead of left
  • Sending data before register address
  • Omitting stop condition
5. You need to write multiple bytes to consecutive registers starting at 0x30 on an I2C device with address 0x60. Which code snippet correctly performs this operation in embedded C?
uint8_t device_addr = 0x60;
uint8_t start_reg = 0x30;
uint8_t data[] = {0x01, 0x02, 0x03};

// Which snippet is correct?
hard
A. I2C_Start(); I2C_Write(device_addr << 1 | 0); for (int i = 0; i < 3; i++) { I2C_Write(start_reg + i); I2C_Write(data[i]); } I2C_Stop();
B. I2C_Start(); I2C_Write(device_addr << 1 | 0); for (int i = 0; i < 3; i++) { I2C_Write(data[i]); } I2C_Stop();
C. I2C_Start(); I2C_Write(device_addr << 1 | 0); I2C_Write(start_reg); for (int i = 0; i < 3; i++) { I2C_Write(data[i]); } I2C_Stop();
D. I2C_Start(); I2C_Write(device_addr << 1 | 1); I2C_Write(start_reg); for (int i = 0; i < 3; i++) { I2C_Write(data[i]); } I2C_Stop();

Solution

  1. Step 1: Start communication and send device address with write bit

    The device address is shifted left and write bit set to 0, then start register address is sent once. After sending the start register, consecutive data bytes are sent to consecutive registers automatically by the device.
  2. Final Answer:

    Start register sent once, then data bytes sent in loop -> Option C
  3. Quick Check:

    Send start reg once, then data bytes [OK]
Hint: Send start register once, then all data bytes [OK]
Common Mistakes:
  • Sending register address before each data byte
  • Using read bit instead of write bit
  • Not sending start register at all
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard