Why I2C is used in Embedded C - Performance Analysis
We want to understand how the time to communicate using I2C changes as we add more devices.
How does the communication time grow when more devices share the same I2C bus?
Analyze the time complexity of the following code snippet.
// Send data to multiple I2C devices
for (int i = 0; i < num_devices; i++) {
i2c_start();
i2c_write(device_addresses[i]);
i2c_write(data[i]);
i2c_stop();
}
This code sends data to each device on the I2C bus one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each device to send data.
- How many times: Once per device, so num_devices times.
As the number of devices increases, the total communication time grows linearly.
| Input Size (num_devices) | Approx. Operations |
|---|---|
| 10 | About 10 communication sequences |
| 100 | About 100 communication sequences |
| 1000 | About 1000 communication sequences |
Pattern observation: Doubling the devices roughly doubles the communication time.
Time Complexity: O(n)
This means the time to communicate grows directly with the number of devices on the I2C bus.
[X] Wrong: "Adding more devices won't affect communication time much because I2C is fast."
[OK] Correct: Each device requires its own communication sequence, so more devices mean more total time.
Understanding how communication time grows with devices helps you design efficient embedded systems and explain your choices clearly.
"What if we used a faster bus speed or combined data for multiple devices in one transmission? How would the time complexity change?"
Practice
Solution
Step 1: Understand I2C wiring
I2C uses two wires: SDA (data) and SCL (clock) to connect devices.Step 2: Recognize multi-device communication
Multiple devices share these two wires, communicating by unique addresses.Final Answer:
It allows multiple devices to communicate using only two wires. -> Option DQuick Check:
I2C uses 2 wires for many devices [OK]
- Thinking each device needs its own wire
- Confusing I2C with wireless communication
- Believing only one device can connect
Solution
Step 1: Identify I2C lines
I2C uses two lines: SDA for data and SCL for clock.Step 2: Understand line sharing
Both lines are shared by all devices on the bus, not separate per device.Final Answer:
Single data line and single clock line shared by all devices -> Option CQuick Check:
I2C shares SDA and SCL lines [OK]
- Assuming separate lines per device
- Thinking data and clock share one wire
- Confusing wireless with wired lines
Solution
Step 1: Understand wiring for multiple devices
I2C allows multiple devices to share the same two wires: SDA and SCL.Step 2: Recognize wiring advantage
This saves pins and wiring complexity compared to separate wires per sensor.Final Answer:
You can use only two wires total for all sensors -> Option BQuick Check:
Two wires for many devices saves pins [OK]
- Thinking each sensor needs separate wires
- Assuming I2C is wireless
- Believing devices don't need addresses
i2c_start();
i2c_write(0x50); // device address
uint8_t data = i2c_read();
i2c_stop();
printf("%d", data);
What does this code do?Solution
Step 1: Analyze I2C function calls
i2c_start() begins communication, i2c_write(0x50) sends device address.Step 2: Understand data flow
i2c_read() reads one byte from the device, i2c_stop() ends communication, printf prints the data.Final Answer:
Starts I2C, writes device address, reads one byte, stops, prints data -> Option AQuick Check:
Write address, read data, stop, print [OK]
- Mixing read and write order
- Assuming stop happens before read
- Confusing address with data
i2c_write(0x50); i2c_start(); i2c_read(); i2c_stop();What is the main error?
Solution
Step 1: Check function call order
i2c_start() must be called before i2c_write() to begin communication.Step 2: Identify error
Here, i2c_write() is called before i2c_start(), which is incorrect and causes failure.Final Answer:
Calling i2c_write before i2c_start -> Option AQuick Check:
Start communication before writing [OK]
- Writing before starting communication
- Ignoring function call order
- Assuming stop must come before start
Ready to prove it?
Pass the test and this pattern moves unknown
