#include <stdio.h> int main() { int i2c_speed = 400000; // 400 kHz int spi_speed = 10000000; // 10 MHz if (spi_speed > i2c_speed) { printf("SPI is faster\n"); } else { printf("I2C is faster\n"); } return 0; }
SPI typically runs at higher clock speeds than I2C. Here, 10 MHz (SPI) is greater than 400 kHz (I2C), so SPI is faster.
I2C uses two wires: SDA (data) and SCL (clock). SPI uses at least four wires: MOSI, MISO, SCLK, and SS (slave select).
#include <stdio.h> int main() { unsigned char master_tx = 0xA5; // Master sends 0xA5 unsigned char slave_tx = 0x3C; // Slave sends 0x3C unsigned char master_rx = slave_tx; // Master receives slave data unsigned char slave_rx = master_tx; // Slave receives master data printf("Master received: 0x%X\n", master_rx); printf("Slave received: 0x%X\n", slave_rx); return 0; }
In SPI, the master sends data to the slave while simultaneously receiving data from the slave. Here, master receives 0x3C from slave, and slave receives 0xA5 from master.
I2C devices can be addressed using either 7-bit or extended 10-bit addresses, allowing more devices on the bus.
I2C uses only two wires regardless of the number of devices and supports addressing each sensor uniquely, making it ideal for multiple sensors with minimal wiring.
