Bird
Raised Fist0
Embedded Cprogramming~20 mins

Why I2C is used in Embedded C - Challenge Your Understanding

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

Practice

(1/5)
1. Why is I2C commonly used in embedded systems?
easy
A. It uses wireless signals for communication.
B. It requires a separate wire for each device.
C. It only supports one device at a time.
D. It allows multiple devices to communicate using only two wires.

Solution

  1. Step 1: Understand I2C wiring

    I2C uses two wires: SDA (data) and SCL (clock) to connect devices.
  2. Step 2: Recognize multi-device communication

    Multiple devices share these two wires, communicating by unique addresses.
  3. Final Answer:

    It allows multiple devices to communicate using only two wires. -> Option D
  4. Quick Check:

    I2C uses 2 wires for many devices [OK]
Hint: I2C uses 2 wires for many devices, not one per device [OK]
Common Mistakes:
  • Thinking each device needs its own wire
  • Confusing I2C with wireless communication
  • Believing only one device can connect
2. Which of the following is the correct way to describe I2C communication lines?
easy
A. Separate data and clock lines for each device
B. Only one wire for both data and clock
C. Single data line and single clock line shared by all devices
D. Wireless data and wired clock lines

Solution

  1. Step 1: Identify I2C lines

    I2C uses two lines: SDA for data and SCL for clock.
  2. Step 2: Understand line sharing

    Both lines are shared by all devices on the bus, not separate per device.
  3. Final Answer:

    Single data line and single clock line shared by all devices -> Option C
  4. Quick Check:

    I2C shares SDA and SCL lines [OK]
Hint: I2C shares one data and one clock line for all devices [OK]
Common Mistakes:
  • Assuming separate lines per device
  • Thinking data and clock share one wire
  • Confusing wireless with wired lines
3. You want to connect 5 sensors to a microcontroller using I2C. What is the main advantage of using I2C here?
easy
A. Each sensor needs its own data and clock wires
B. You can use only two wires total for all sensors
C. Sensors communicate wirelessly with microcontroller
D. I2C requires no addressing for devices

Solution

  1. Step 1: Understand wiring for multiple devices

    I2C allows multiple devices to share the same two wires: SDA and SCL.
  2. Step 2: Recognize wiring advantage

    This saves pins and wiring complexity compared to separate wires per sensor.
  3. Final Answer:

    You can use only two wires total for all sensors -> Option B
  4. Quick Check:

    Two wires for many devices saves pins [OK]
Hint: I2C uses 2 wires total regardless of device count [OK]
Common Mistakes:
  • Thinking each sensor needs separate wires
  • Assuming I2C is wireless
  • Believing devices don't need addresses
4. Consider this I2C communication snippet in embedded C:
i2c_start();
i2c_write(0x50); // device address
uint8_t data = i2c_read();
i2c_stop();
printf("%d", data);
What does this code do?
medium
A. Starts I2C, writes device address, reads one byte, stops, prints data
B. Starts I2C, reads device address, writes one byte, stops, prints data
C. Starts I2C, writes data, reads address, stops, prints data
D. Starts I2C, stops immediately, prints zero

Solution

  1. Step 1: Analyze I2C function calls

    i2c_start() begins communication, i2c_write(0x50) sends device address.
  2. Step 2: Understand data flow

    i2c_read() reads one byte from the device, i2c_stop() ends communication, printf prints the data.
  3. Final Answer:

    Starts I2C, writes device address, reads one byte, stops, prints data -> Option A
  4. Quick Check:

    Write address, read data, stop, print [OK]
Hint: Write address before reading data in I2C [OK]
Common Mistakes:
  • Mixing read and write order
  • Assuming stop happens before read
  • Confusing address with data
5. This embedded C code tries to communicate over I2C but fails:
i2c_write(0x50);
i2c_start();
i2c_read();
i2c_stop();
What is the main error?
medium
A. Calling i2c_write before i2c_start
B. Missing device address in i2c_write
C. Not calling i2c_stop before i2c_start
D. Reading data before writing device address

Solution

  1. Step 1: Check function call order

    i2c_start() must be called before i2c_write() to begin communication.
  2. Step 2: Identify error

    Here, i2c_write() is called before i2c_start(), which is incorrect and causes failure.
  3. Final Answer:

    Calling i2c_write before i2c_start -> Option A
  4. Quick Check:

    Start communication before writing [OK]
Hint: Always call i2c_start() before i2c_write() [OK]
Common Mistakes:
  • Writing before starting communication
  • Ignoring function call order
  • Assuming stop must come before start
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