Introduction
In the world of embedded systems, choosing the right serial protocol is often the first design decision. SPI (Serial Peripheral Interface) and I2C (Inter-Integrated Circuit) are the two most common on-board communication standards for connecting microcontrollers to sensors, displays, and ADCs. While I2C shines with its two-wire bus and multi-master capability, SPI dominates when raw speed and full-duplex data transfer are required. According to the official datasheets from NXP (I2C-bus specification Rev. 7.0) and Motorola (SPI Block Guide V04.01), SPI can achieve clock rates exceeding 10 MHz, whereas standard I2C tops out at 400 kHz (Fast Mode) or 1 MHz (Fast Mode Plus). This speed advantage makes SPI the go-to protocol for high-speed data acquisition, real-time display updates, and precision measurement.
But what if you could connect SPI devices to an AI agent that writes the integration code for you? That’s exactly what ASI Biont does. Instead of spending hours debugging SPI clock polarity or writing custom Python wrappers, you describe your hardware setup in a chat conversation, and the AI agent generates the complete communication code using spidev (on Linux) or Adafruit_CircuitPython libraries. In this article, we’ll compare SPI and I2C, then walk through three concrete integration examples with the ASI Biont AI agent: an MCP3008 8-channel ADC, an ST7735 color TFT display, and a MAX31865 RTD temperature sensor. All examples use an ESP32 or Arduino connected via COM port (Hardware Bridge) or MQTT.
SPI vs I2C: A Technical Comparison
Before diving into code, let’s clarify the fundamental differences. The table below summarises the key parameters:
| Parameter | SPI | I2C |
|---|---|---|
| Number of wires | 4 (MOSI, MISO, SCLK, CS) | 2 (SDA, SCL) |
| Maximum speed | Typically 10–80 MHz | 100 kHz (Standard), 400 kHz (Fast), 1 MHz (Fast Mode+) |
| Communication type | Full-duplex | Half-duplex |
| Addressing | Chip select (CS) lines | 7-bit or 10-bit address |
| Number of devices | Limited by CS lines | Up to 112 (7-bit) or 1024 (10-bit) |
| Distance | Short (same PCB, <1 m) | Short (same PCB, <1 m) |
| Power consumption | Higher (constant clock) | Lower (clock stretched) |
From a practical standpoint, SPI is ideal for applications like:
- Reading multiple analog channels simultaneously (e.g., MCP3008)
- Driving small TFT displays with high refresh rates (e.g., ST7735)
- High-precision temperature measurement with RTDs (e.g., MAX31865)
I2C, on the other hand, is better for simple sensors (temperature, humidity) where wiring simplicity matters more than speed.
Connecting SPI Devices to ASI Biont: Three Real-World Scenarios
ASI Biont connects to microcontrollers and sensors via several channels. For SPI devices, the most practical setup is:
1. Arduino/ESP32 + SPI sensor → wired to the microcontroller’s hardware SPI pins.
2. Microcontroller → PC via USB (COM port) → the user runs bridge.py on their PC, which exposes the COM port to the AI agent.
3. AI agent (in cloud) → industrial_command tool → sends commands to the bridge, which reads/writes to the serial port.
Alternatively, if the microcontroller has Wi-Fi (ESP32), the AI can connect via MQTT: the microcontroller publishes sensor data to a topic, and the AI subscribes using a Python script with paho-mqtt.
Scenario 1: MCP3008 – 8-Channel Analog Data Logger
The MCP3008 is an 8-channel 10-bit ADC that communicates via SPI. It is commonly used to read analog sensors (potentiometers, light sensors, force sensors) with an ESP32. Here’s how to integrate it with ASI Biont.
Wiring diagram (ESP32):
- MCP3008 VDD → 3.3V
- MCP3008 VREF → 3.3V
- MCP3008 AGND → GND
- MCP3008 DGND → GND
- MCP3008 CS → GPIO5 (ESP32)
- MCP3008 DIN → GPIO23 (MOSI)
- MCP3008 DOUT → GPIO19 (MISO)
- MCP3008 CLK → GPIO18 (SCLK)
MicroPython code for ESP32 (uploaded via Thonny or esptool):
from machine import Pin, SPI
import time
spi = SPI(1, baudrate=1000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
cs = Pin(5, Pin.OUT)
def read_adc(channel):
if channel < 0 or channel > 7:
return -1
tx_data = bytearray([1, (8 + channel) << 4, 0])
rx_data = bytearray(3)
cs.off()
spi.write_readinto(tx_data, rx_data)
cs.on()
value = ((rx_data[1] & 3) << 8) | rx_data[2]
return value
while True:
for ch in range(8):
val = read_adc(ch)
print(f"CH{ch}: {val}")
time.sleep(1)
ASI Biont integration (user chat):
User: "Connect to my ESP32 on COM3 at 115200 baud. Read all 8 ADC channels from the MCP3008 every 5 seconds and log them to a CSV file. If any channel exceeds 900, send me a Telegram alert."
AI agent action: The AI uses industrial_command(protocol='serial://', command='open', port='COM3', baudrate=115200). Then it writes a Python script that runs in the sandbox (execute_python) to parse incoming serial data and perform the logic. The script uses pyserial (via bridge) to read lines, parses the CH0: 512 format, appends to CSV, and sends alerts via the telegram library.
Scenario 2: ST7735 TFT Display – Visualise Data on Voice Command
The ST7735 is a 1.8-inch color TFT display (160x128 pixels) that uses SPI. It is often used in handheld gadgets or dashboards. With ASI Biont, you can update the display content using natural language.
Wiring (ESP32):
- ST7735 VCC → 3.3V
- ST7735 GND → GND
- ST7735 CS → GPIO15
- ST7735 DC → GPIO2
- ST7735 RST → GPIO4
- ST7735 MOSI → GPIO23
- ST7735 SCLK → GPIO18
CircuitPython library code (for reference):
import board
import displayio
import adafruit_st7735
spi = board.SPI()
tft_cs = board.D15
tft_dc = board.D2
tft_rst = board.D4
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=tft_rst)
display = adafruit_st7735.ST7735(display_bus, width=160, height=128)
ASI Biont integration (user chat):
User: "Connect to my ESP32 on COM3. The display is an ST7735 on SPI. When I say 'show temperature 25.3°C', update the display with that text in white on blue background."
AI agent action: The AI uses the Hardware Bridge to send a pre-programmed command (e.g., a specific byte sequence) that the ESP32 firmware understands. Alternatively, if the ESP32 is running a custom firmware that accepts JSON commands via serial, the AI sends: {"cmd":"display","text":"Temperature 25.3°C","fg":"white","bg":"blue"}. The AI writes the parsing code and display driver integration.
Scenario 3: MAX31865 – Precision RTD Temperature Monitoring
The MAX31865 is a PT100/PT1000 RTD-to-digital converter with SPI interface. It is used in industrial temperature sensing with high accuracy (0.05% typical error).
Wiring (Arduino Uno):
- MAX31865 VIN → 5V
- MAX31865 GND → GND
- MAX31865 SCLK → Pin 13
- MAX31865 MOSI → Pin 11
- MAX31865 MISO → Pin 12
- MAX31865 CS → Pin 10
Arduino sketch (C++):
#include <Adafruit_MAX31865.h>
Adafruit_MAX31865 thermo = Adafruit_MAX31865(10);
void setup() { Serial.begin(115200); thermo.begin(MAX31865_3WIRE); }
void loop() { float temp = thermo.temperature(100.0, 430.0); Serial.print("TEMP:"); Serial.println(temp); delay(2000); }
ASI Biont integration (user chat):
User: "Connect to my Arduino on COM4 at 115200 baud. It has a MAX31865 temperature sensor. Read the temperature every 10 seconds. If it exceeds 80°C, turn on a relay on pin 7."
AI agent action: The AI opens the COM port via bridge, reads serial lines matching TEMP:xx.xx, parses the float, and writes a control script. When the threshold is exceeded, the AI sends a command to the bridge to write a byte to the Arduino (e.g., 'H' to turn relay on, 'L' to turn off).
Why Use ASI Biont Instead of Hand-Coding?
Traditionally, integrating an SPI device requires:
1. Reading the device datasheet to understand register maps
2. Writing low-level SPI read/write functions
3. Debugging timing issues with an oscilloscope
4. Writing a Python or C program to log/visualize data
With ASI Biont, you skip steps 2–4. The AI agent already knows the common SPI device libraries (Adafruit_CircuitPython, spidev, Arduino libraries). You simply describe your hardware and desired outcome in the chat. The AI writes the firmware code (if needed), generates the Python integration script, and even runs it in the sandbox to test connectivity.
Key advantages:
- Zero boilerplate: No need to write serial parsers or MQTT clients from scratch.
- Natural language control: “Log temperature every minute and email me a daily summary” – done.
- Multi-protocol support: The same AI agent can simultaneously talk to an SPI sensor, a Modbus PLC, and an HTTP smart plug.
How to Get Started
- Set up your hardware: Wire your SPI device to an Arduino or ESP32 as shown above.
- Upload firmware: Use the provided MicroPython or Arduino sketch to output data over serial or publish via MQTT.
- Run bridge.py: On your PC, run
bridge.py --token=YOUR_ASI_BIONT_TOKEN --ports=COM3 --default-baud=115200. - Chat with the AI: In the ASI Biont web interface, tell the AI what you want. Example: “Read the MCP3008 on COM3, channels 0 to 7, every 5 seconds. Save to CSV and alert if any channel > 900.”
The AI will respond with the code it generated and execute it. You can see the data flowing in real time.
Conclusion
SPI remains the protocol of choice for high-speed sensor and display interfacing, but its complexity often discourages hobbyists and engineers from rapid prototyping. ASI Biont eliminates that friction by acting as an AI-powered integration layer. Whether you’re logging eight analog channels, updating a tiny TFT dashboard, or monitoring an industrial RTD, the AI agent handles the SPI communication, data parsing, and automation logic – all through a simple chat conversation.
Ready to connect your SPI device to an AI agent? Try it today at asibiont.com. No coding required – just describe your hardware and let the AI do the rest.
Comments