I2C & SPI Protocols
Complex components like OLED displays, IMUs, and external storage require high-speed communication protocols to transfer large amounts of data over just a few wires.
1. I2C (Inter-Integrated Circuit)
I2C is a 2-wire, address-based protocol. It uses SDA (Data) and SCL (Clock) lines. Because it is addressable, you can connect dozens of sensors to the same two pins.
Scanning the I2C Bus
Before communicating with a sensor, you can “scan” the bus to find its unique hexadecimal address.
import { I2C } from 'intarva';
const i2c = new I2C(0, { sda: 'GP4', scl: 'GP5' });
const devices = i2c.scan();
console.log('Found devices at:', devices.map(d => '0x' + d.toString(16)));
from machine import I2C, Pin
i2c = I2C(0, sda=Pin(4), scl=Pin(5))
devices = i2c.scan()
print('Found devices at:', [hex(d) for d in devices])
2. SPI (Serial Peripheral Interface)
SPI is a 4-wire, high-speed protocol used for displays and SD cards. It uses MOSI, MISO, SCK, and CS (Chip Select). Unlike I2C, it is significantly faster but requires a dedicated CS line for every device.
Full Duplex Communication
Intarva accurately simulates SPI’s full-duplex nature, allowing data to be sent and received simultaneously in a single clock cycle.
3. Choosing the Right Protocol
| Feature | I2C | SPI |
|---|---|---|
| Wire Count | 2 (plus GND) | 4 (plus GND) |
| Max Speed | ~1 MHz (Fast Mode+) | ~50+ MHz |
| Addressing | 7-bit Hex Address | Physical CS Line |
| Typical Use | Sensors, RTCs, EEPROM | Displays, SD Cards, Flash |