SPI (Serial Peripheral Interface)#

SPI Overview#

SPI (Serial Peripheral Interface) is a synchronous, full-duplex, master-slave communication bus. It is commonly used to connect microcontrollers to peripherals such as sensors, displays, and memory devices. The PULSAR H2 development board features SPI communication capabilities, allowing you to interface with a wide range of SPI devices including microSD cards.

SPI Pin Configuration#

The ESP32-H2 PULSAR H2 board has the following SPI pin configuration:

Internal SPI Configuration

Fig. 19 PULSAR H2 Internal SPI Configuration#

External SPI Connection

Fig. 20 External SPI Device Connection#

MicroSD Card SPI Interface#

Warning

Ensure that the Micro SD contains data. We recommend saving multiple files beforehand to facilitate testing. Format the Micro SD card to FAT32 before using it with the ESP32-H2.

Micro SD Card Pinout

Fig. 21 Micro SD Card Pinout#

Micro SD Card reader

Fig. 22 Micro SD Card external reader#

The conections are as follows:

This table illustrates the connections between the SD card and the GPIO pins on the ESP32-H2 (PULSAR H2)

Table 6 MicroSD SPI Connections#

SD Card Pin

Function

ESP32-H2 GPIO

PULSAR H2 Pin

1

D2 (Not Connected)

N/C

2

D3/CS (Chip Select)

GPIO11

D10/SS

3

CMD/MOSI

GPIO5

D11/MOSI

4

VDD (3.3V)

3.3V

3.3V

5

CLK/SCK

GPIO4

D13/SCK

6

VSS (GND)

GND

GND

7

D0/MISO

GPIO0

D12/MISO

8

D1 (Not Connected)

N/C

SPI Pin Mapping Summary#

Table 7 ESP32-H2 SPI Pin Configuration#

SPI Function

ESP32-H2 GPIO

PULSAR H2 Pin

Description

MOSI (Master Out)

GPIO5

D11/MOSI

Data output from master

MISO (Master In)

GPIO0

D12/MISO

Data input to master

SCK (Clock)

GPIO4

D13/SCK

Serial clock signal

CS (Chip Select)

GPIO11

D10/SS

Device selection signal

Note

MicroSD Connection Notes:

  • The microSD card is connected via SPI interface using 4 wires (MOSI, MISO, SCK, CS)

  • CS (Chip Select) is connected to GPIO11 (D10/SS pin)

  • MOSI is connected to GPIO5 (D11/MOSI pin)

  • MISO is connected to GPIO0 (D12/MISO pin)

  • SCK is connected to GPIO4 (D13/SCK pin)

  • D2 and D1 pins of the SD card are not used in SPI mode

  • Make sure the SD card is formatted as FAT32 before use

  • The SD card operates at 3.3V - no level shifters needed

from machine import Pin, SPI
import os
import sdcard
import time

# --- Custom pin configuration ---
MOSI_PIN = 5
MISO_PIN = 0
SCK_PIN  = 4
CS_PIN   = 11

# --- Initialize SPI with custom pins ---
spi = SPI(
    1,                   # Bus SPI(1) = HSPI (can use remapped pins)
    baudrate=5_000_000,  # 5 MHz is more stable with long cables or sensitive SDs
    polarity=0,
    phase=0,
    sck=Pin(SCK_PIN),
    mosi=Pin(MOSI_PIN),
    miso=Pin(MISO_PIN)
)

# --- Chip Select pin ---
cs = Pin(CS_PIN, Pin.OUT)

# --- Initialize SD card ---
try:
    sd = sdcard.SDCard(spi, cs)
    vfs = os.VfsFat(sd)
    os.mount(vfs, "/sd")
    print("microSD mounted successfully at /sd\n")

    # --- List contents ---
    print("Contents of /sd:")
    for fname in os.listdir("/sd"):
        print(" -", fname)

    # --- Read/write test ---
    test_path = "/sd/test.txt"
    with open(test_path, "w") as f:
        f.write("Hello from ESP32 with custom SPI pins!\n")
    print(f"\nFile created: {test_path}")

    with open(test_path, "r") as f:
        print("\nFile contents:")
        print(f.read())

except Exception as e:
    print("Error initializing SD card:", e)

# --- Infinite loop ---
while True:
    time.sleep(1)

General SPI Device Connection#

Besides microSD cards, you can connect various SPI devices to the PULSAR H2:

Common SPI Devices#

Table 8 Compatible SPI Devices#

Device Type

Example Models

Applications

Displays

ST7735, ILI9341, SSD1306

Status displays, GUI interfaces

Sensors

BME280, MPU6050, MAX31855

Environmental monitoring, IMU

Memory

W25Q32, AT25DF641, microSD

Data storage, logging

ADC/DAC

MCP3008, MCP4725

Analog signal processing

RF Modules

nRF24L01, LoRa modules

Wireless communication

SPI Configuration Tips#

# General SPI device connection example
from machine import Pin, SPI

# Initialize SPI bus with standard pins
spi = SPI(1,
          sck=Pin(4),   # Clock
          mosi=Pin(5),  # Master Out, Slave In
          miso=Pin(0),  # Master In, Slave Out
          baudrate=1000000)  # 1MHz for most devices

# Individual chip select pins for multiple devices
cs_display = Pin(11, Pin.OUT)
cs_sensor = Pin(10, Pin.OUT)
cs_memory = Pin(9, Pin.OUT)

# Device selection example
cs_display.value(0)  # Select display
spi.write(b'display_data')
cs_display.value(1)  # Deselect display

Troubleshooting SPI#

Common Issues:

  1. Device not responding: Check wiring and power supply

  2. Data corruption: Reduce SPI clock frequency

  3. Multiple device conflicts: Ensure proper CS management

  4. Signal integrity: Use short wires, add pull-up resistors if needed

Best Practices:

  • Use appropriate SPI clock frequencies for each device

  • Implement proper chip select (CS) timing

  • Add decoupling capacitors near SPI devices

  • Keep wire lengths short for high-frequency signals

Resources and Documentation#