Concept Flow - I2C vs SPI decision matrix
Start: Need to choose communication protocol
Check number of devices
Check speed
Choose I2C
Start by checking device count, speed needs, and bus complexity to decide between I2C and SPI.
if (num_devices <= 2) { if (speed < 400000) { protocol = I2C; } else { protocol = SPI; } } else { protocol = I2C; }
| Step | num_devices | speed (Hz) | Condition Checked | Decision | Protocol Chosen |
|---|---|---|---|---|---|
| 1 | 1 | 100000 | num_devices <= 2? True | Check speed | None |
| 2 | 1 | 100000 | speed < 400000? True | Choose I2C | I2C |
| 3 | 3 | 100000 | num_devices <= 2? False | Choose I2C | I2C |
| 4 | 1 | 1000000 | num_devices <= 2? True | Check speed | None |
| 5 | 1 | 1000000 | speed < 400000? False | Choose SPI | SPI |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | After Step 5 |
|---|---|---|---|---|---|---|
| num_devices | undefined | 1 | 1 | 3 | 1 | 1 |
| speed | undefined | 100000 | 100000 | 100000 | 1000000 | 1000000 |
| protocol | undefined | undefined | I2C | I2C | undefined | SPI |
I2C vs SPI decision matrix: - Check number of devices first - If devices <= 2 and speed < 400kHz, choose I2C - If speed > 400kHz or bus complexity high, choose SPI - I2C supports multi-device easily - SPI is faster but needs more wires