Bird
Raised Fist0
Embedded Cprogramming~20 mins

Writing data to I2C device in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
I2C Writing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this I2C write function call?

Consider the following embedded C code snippet that writes a byte to an I2C device. What will be the return value of i2c_write_byte when called with device_addr = 0x50 and data = 0xA5?

Embedded C
int i2c_write_byte(uint8_t device_addr, uint8_t data) {
    // Simulated write: returns 0 on success, -1 on failure
    if (device_addr == 0x50) {
        // Device acknowledged
        return 0;
    } else {
        // No acknowledgment
        return -1;
    }
}

int main() {
    int result = i2c_write_byte(0x50, 0xA5);
    return result;
}
A0
B-1
C1
D255
Attempts:
2 left
💡 Hint

Think about what the function returns when the device address matches 0x50.

❓ Predict Output
intermediate
2:00remaining
What is the value of status after this I2C write sequence?

Given the following code that writes multiple bytes to an I2C device, what is the final value of status?

Embedded C
int i2c_write_bytes(uint8_t device_addr, uint8_t* data, int length) {
    for (int i = 0; i < length; i++) {
        if (data[i] == 0xFF) {
            return -2; // Error: invalid data
        }
    }
    return 0; // Success
}

int main() {
    uint8_t buffer[3] = {0x10, 0x20, 0x30};
    int status = i2c_write_bytes(0x60, buffer, 3);
    return status;
}
A-2
B-1
C3
D0
Attempts:
2 left
💡 Hint

Check if any byte in the buffer equals 0xFF.

🔧 Debug
advanced
2:00remaining
Identify the error in this I2C write function

Examine the following code intended to write a byte to an I2C device. Which option correctly identifies the error that will cause a compilation failure?

Embedded C
int i2c_write_byte(uint8_t device_addr, uint8_t data) {
    start_i2c();
    send_address(device_addr, WRITE);
    send_data(data);
    stop_i2c();
    return 0;
}
Astart_i2c() should return a value
BMissing semicolon after send_data(data)
Cstop_i2c() must be called before send_data()
DUndefined variable WRITE
Attempts:
2 left
💡 Hint

Look carefully at the line endings.

📝 Syntax
advanced
2:00remaining
Which option will cause a runtime error in this I2C write code?

Consider the following code snippet that writes data to an I2C device. Which option will cause a runtime error when executed?

Embedded C
int i2c_write_byte(uint8_t device_addr, uint8_t data) {
    if (device_addr == 0) {
        return -1; // Invalid address
    }
    // Simulate write
    return 0;
}

int main() {
    int result = i2c_write_byte(0, 0x55);
    return result;
}
ACalling i2c_write_byte(0, 0x55) returns -1, no runtime error
BCalling i2c_write_byte(0xFF, 0x55) causes segmentation fault
CCalling i2c_write_byte(0, 0x55) causes division by zero error
DCalling i2c_write_byte(0x50, 0x55) causes null pointer dereference
Attempts:
2 left
💡 Hint

Check if any option describes a runtime error actually caused by the code.

🚀 Application
expert
3:00remaining
How many bytes are written in this I2C multi-byte write function?

Given the following function that writes multiple bytes to an I2C device, how many bytes will be written if length is 5 and data contains no 0xFF values?

Embedded C
int i2c_write_bytes(uint8_t device_addr, uint8_t* data, int length) {
    int bytes_written = 0;
    for (int i = 0; i < length; i++) {
        if (data[i] == 0xFF) {
            break; // Stop writing on invalid data
        }
        // Simulate writing one byte
        bytes_written++;
    }
    return bytes_written;
}

int main() {
    uint8_t buffer[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
    int count = i2c_write_bytes(0x40, buffer, 5);
    return count;
}
A0
B4
C5
D1
Attempts:
2 left
💡 Hint

Since no data byte is 0xFF, the loop writes all bytes.

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