I2C vs SPI decision matrix in Embedded C - Performance Comparison
When choosing between I2C and SPI communication in embedded C, it's important to understand how the time to send data grows as the amount of data increases.
We want to know how the communication time changes when sending more bytes over these protocols.
Analyze the time complexity of sending data using I2C and SPI protocols.
// Example: Sending data over I2C
void send_i2c_data(uint8_t* data, int length) {
for (int i = 0; i < length; i++) {
i2c_write_byte(data[i]);
}
}
// Example: Sending data over SPI
void send_spi_data(uint8_t* data, int length) {
for (int i = 0; i < length; i++) {
spi_write_byte(data[i]);
}
}
Both functions send each byte one by one over their respective protocols.
Both functions use a loop to send bytes one at a time.
- Primary operation: Sending one byte over I2C or SPI.
- How many times: Once for each byte in the data array (length times).
As the number of bytes to send increases, the total time grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 byte sends |
| 100 | 100 byte sends |
| 1000 | 1000 byte sends |
Pattern observation: Doubling the data doubles the sending time.
Time Complexity: O(n)
This means the time to send data grows linearly with the number of bytes.
[X] Wrong: "SPI is always faster, so its time complexity is better than I2C."
[OK] Correct: Both protocols send bytes one by one, so their time complexity is the same. Speed differences come from hardware and clock rates, not complexity.
Understanding how communication time grows with data size helps you explain design choices clearly and shows you grasp practical embedded system trade-offs.
"What if we used DMA (Direct Memory Access) to send data instead of sending byte-by-byte in a loop? How would the time complexity change?"
Practice
Solution
Step 1: Recall wire count for I2C
I2C uses 2 wires: SDA (data) and SCL (clock).Step 2: Compare with SPI wiring
SPI uses 4 wires, so it is not the protocol with only 2 wires.Final Answer:
I2C -> Option DQuick Check:
2 wires = I2C [OK]
- Confusing SPI as 2-wire protocol
- Thinking UART or CAN use 2 wires for multi-device
- Mixing up data and clock lines
Solution
Step 1: Identify SPI signal lines
SPI uses 4 wires: MOSI (Master Out Slave In), MISO (Master In Slave Out), SCLK (clock), and SS (slave select).Step 2: Eliminate incorrect options
Options B, C, and D do not list the correct 4 SPI wires.Final Answer:
SPI uses 4 wires: MOSI, MISO, SCLK, and SS -> Option CQuick Check:
SPI = 4 wires (MOSI, MISO, SCLK, SS) [OK]
- Confusing SPI with I2C wire names
- Forgetting slave select (SS) wire
- Assuming SPI uses only 2 or 3 wires
Solution
Step 1: Analyze device count and wiring complexity
Connecting 8 sensors means many devices; I2C supports multiple devices on 2 wires easily.Step 2: Consider speed and wiring
I2C is moderate speed but simpler wiring; SPI is faster but wiring gets complex with many devices.Final Answer:
I2C, because it supports multiple devices with simple wiring -> Option AQuick Check:
Many devices + simple wiring = I2C [OK]
- Choosing SPI for many devices ignoring wiring complexity
- Assuming SPI uses 2 wires
- Thinking I2C is faster than SPI
Solution
Step 1: Check wiring requirements for SPI
SPI requires 4 wires; using only 2 wires can cause data corruption.Step 2: Evaluate other options
Options A and C relate to I2C or speed mismatch, less likely cause of SPI data corruption; pull-ups on SPI lines are usually not needed.Final Answer:
Using only 2 wires instead of 4 for SPI -> Option AQuick Check:
SPI needs 4 wires, missing wires cause errors [OK]
- Using I2C settings for SPI communication
- Ignoring missing wires in SPI
- Adding pull-up resistors unnecessarily on SPI
Solution
Step 1: Analyze device count and speed needs
3 devices is a small number; SPI is better for fast data transfer with fewer devices.Step 2: Consider wiring complexity
SPI uses 4 wires but with fewer devices wiring is manageable; I2C is slower though uses 2 wires.Final Answer:
SPI, because it supports fast data transfer with fewer devices -> Option BQuick Check:
Few devices + fast speed = SPI [OK]
- Choosing I2C for fast speed needs
- Assuming SPI uses only 2 wires
- Ignoring device count impact on wiring
Ready to prove it?
Pass the test and this pattern moves unknown
