Skip to main content

spi

High-speed serial communication with SPI devices like displays, ADCs, and flash memory.

Overview

The spi node enables high-speed serial communication with SPI (Serial Peripheral Interface) devices. SPI is faster than I2C and ideal for displays, ADCs, DACs, flash memory, and high-bandwidth sensors.

125MHz
Max Speed (Pi 4)
4
Wires (MOSI/MISO/CLK/CS)
2
CE Pins (CS0/CS1)
Full
Duplex

Raspberry Pi SPI Pins

SPI0 (Default)

MOSI (Master Out) GPIO 10 (Pin 19)
MISO (Master In) GPIO 9 (Pin 21)
SCLK (Clock) GPIO 11 (Pin 23)
CE0 (Chip Select 0) GPIO 8 (Pin 24)
CE1 (Chip Select 1) GPIO 7 (Pin 26)

Enable SPI

sudo raspi-config
# Interface Options → SPI → Enable

# Or add to /boot/config.txt:
dtparam=spi=on

Properties

Property Type Required Default Description
device string No "/dev/spidev0.0" SPI device path
speed number No 1000000 Clock speed in Hz
mode number No 0 SPI mode (0-3)
bitsPerWord number No 8 Bits per word (usually 8)
lsbFirst boolean No false Send LSB first (most devices use MSB)

SPI Modes

SPI mode defines clock polarity (CPOL) and phase (CPHA). Check your device datasheet.

Mode CPOL CPHA Description
0 0 0 Clock idle LOW, sample on rising edge (most common)
1 0 1 Clock idle LOW, sample on falling edge
2 1 0 Clock idle HIGH, sample on falling edge
3 1 1 Clock idle HIGH, sample on rising edge

Inputs

msg.payload

Data to send as array or buffer. Receive buffer of same length.

// Send and receive 3 bytes
msg.payload = [0x01, 0x80, 0x00];
return msg;

Outputs

Response
{
  "payload": [0x00, 0x07, 0xFF],
  "buffer": "<Buffer 00 07 ff>",
  "_spi": {
    "device": "/dev/spidev0.0",
    "speed": 1000000
  }
}

Common SPI Devices

MCP3008 - ADC

10-bit, 8-channel ADC

Speed: 1MHz max

Mode: 0

MAX7219 - LED Driver

8x8 LED matrix driver

Speed: 10MHz max

Mode: 0

ST7789 - TFT Display

240x240 color LCD

Speed: 62.5MHz max

Mode: 0

W25Q - Flash Memory

NOR flash storage

Speed: 80MHz max

Mode: 0 or 3

Example: Read MCP3008 ADC

Read analog value from channel 0 of MCP3008.

// Function node to read MCP3008 channel 0
var channel = 0;

// Start bit + single-ended + channel
msg.payload = [
  0x01,                    // Start bit
  (0x80 | (channel << 4)), // Single-ended, channel select
  0x00                     // Don't care
];

return msg;
// Function node to parse MCP3008 response
var bytes = msg.payload;

// Extract 10-bit value from response
var value = ((bytes[1] & 0x03) << 8) | bytes[2];

// Convert to voltage (assuming 3.3V reference)
msg.payload = {
  raw: value,
  voltage: (value / 1023) * 3.3
};

return msg;

Related Nodes