0
0
Embedded Cprogramming~20 mins

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

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