0
0
Embedded Cprogramming~20 mins

Why I2C is used in Embedded C - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
I2C Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is I2C preferred for connecting multiple sensors?

Imagine you want to connect several sensors to a microcontroller using a communication protocol. Why is I2C often chosen for this task?

ABecause I2C uses wireless signals to connect devices without wires.
BBecause I2C requires a separate wire for each device, making it easier to isolate signals.
CBecause I2C uses only two wires to connect multiple devices, saving pins and wiring complexity.
DBecause I2C can only connect two devices, making it simpler to manage.
Attempts:
2 left
💡 Hint

Think about how many wires are needed to connect many devices and how I2C handles addressing.

🧠 Conceptual
intermediate
2:00remaining
What is a key advantage of I2C over SPI in embedded systems?

Both I2C and SPI are common communication protocols. What is a main advantage of I2C compared to SPI?

AI2C requires fewer wires and supports multiple devices on the same bus.
BI2C is faster than SPI in all cases.
CI2C uses separate chip select lines for each device.
DI2C does not require a clock signal.
Attempts:
2 left
💡 Hint

Consider how many wires each protocol uses and how devices are selected.

Predict Output
advanced
2:30remaining
What is the output of this I2C device scanning code snippet?

Consider this embedded C code snippet that scans for I2C devices on the bus and prints their addresses:

Embedded C
for (uint8_t addr = 1; addr < 128; addr++) {
    if (i2c_start(addr << 1 | I2C_WRITE)) {
        printf("Device found at 0x%02X\n", addr);
        i2c_stop();
    }
}
APrints device addresses in decimal format.
BPrints all device addresses found on the I2C bus in hexadecimal format.
CPrints nothing because the loop starts at 0.
DCauses a runtime error due to invalid shift operation.
Attempts:
2 left
💡 Hint

Look at how the address is shifted and printed.

Predict Output
advanced
2:30remaining
What error does this I2C initialization code produce?

Analyze this embedded C code snippet for I2C initialization:

Embedded C
void i2c_init() {
    TWBR = 0x48; // Set bit rate
    TWSR = 0x02; // Set prescaler bits
    TWCR = (1 << TWEN); // Enable TWI
    TWDR = 0xFF; // Load data register
}
ARuntime error due to writing to TWDR during initialization.
BNo error; code initializes I2C correctly.
CCompilation error because TWBR is not declared.
DLogical error: TWDR should not be set during initialization.
Attempts:
2 left
💡 Hint

Consider the purpose of TWDR during initialization.

🧠 Conceptual
expert
3:00remaining
Why does I2C use open-drain/open-collector lines for SDA and SCL?

In I2C communication, the data (SDA) and clock (SCL) lines are open-drain or open-collector. Why is this design used?

ATo allow multiple devices to pull the line low without causing damage, enabling safe bus sharing.
BTo increase the speed of data transfer by actively driving the line high and low.
CTo reduce power consumption by keeping the lines always high.
DTo prevent any device from pulling the line low, ensuring only the master controls the bus.
Attempts:
2 left
💡 Hint

Think about how multiple devices communicate on the same wire safely.