How to Connect and Read Multiple SPI Devices in Python on Raspberry Pi (DualCE SPI-GPIO Bridge Technique)

Spread the love

DualCE SPI-GPIO Bridge Technique:

If you’ve ever tried to connect more than one SPI device to your Raspberry Pi, you’ve probably hit a roadblock: the default hardware SPI interface only supports two chip-select (CE) lines.

In this guide, we’ll show you how to connect multiple SPI devices using Python’s spidev library and a clever trick called the DualCE SPI-GPIO Bridge Technique — a method that lets you add unlimited chip select lines without extra hardware.

Why Multiple SPI Devices Can Be a Challenge

SPI is fast and efficient, but:

  • Each device needs its own Chip Select (CS) line.
  • Raspberry Pi has only CE0 and CE1 by default.
  • More than two devices require either extra hardware or manual GPIO control.

The DualCE SPI-GPIO Bridge Technique solves this by combining hardware and software chip select handling.

Hardware Needed

Raspberry Pi with SPI enabled

At least two SPI devices (e.g., MCP3008 ADC, MPU-9250 sensor, flash chips)

Jumper wires

Breadboard

Circuit Wiring

Shared connections:

  • MISO → All devices
  • MOSI → All devices
  • SCLK → All devices

Unique chip select:

  • Device 1 → CE0 (GPIO8)
  • Device 2 → GPIO17 (manual CS control)
  • More devices → Any free GPIO pins for extra CS lines.

Enabling SPI

sudo raspi-config
# Interface Options → SPI → Enable
sudo reboot

Python Code (DualCE SPI-GPIO Bridge Technique)

import spidev
import RPi.GPIO as GPIO
import time

# Setup GPIO for manual CS
GPIO.setmode(GPIO.BCM)
DEVICE2_CS = 17
GPIO.setup(DEVICE2_CS, GPIO.OUT, initial=GPIO.HIGH)

# SPI for CE0
spi0 = spidev.SpiDev()
spi0.open(0, 0)  # Bus 0, CE0
spi0.max_speed_hz = 50000

# SPI for manual CS device (shares same bus)
spi1 = spidev.SpiDev()
spi1.open(0, 0)
spi1.max_speed_hz = 50000

def read_device_0():
    adc = spi0.xfer2([0x01, 0x80, 0x00])
    value = ((adc[1] & 3) << 8) + adc[2]
    return value

def read_device_1():
    GPIO.output(DEVICE2_CS, GPIO.LOW)  # Activate device
    adc = spi1.xfer2([0x01, 0x80, 0x00])
    GPIO.output(DEVICE2_CS, GPIO.HIGH) # Deactivate device
    value = ((adc[1] & 3) << 8) + adc[2]
    return value

try:
    while True:
        val0 = read_device_0()
        val1 = read_device_1()
        print(f"Device 0: {val0}, Device 1: {val1}")
        time.sleep(1)
except KeyboardInterrupt:
    pass
finally:
    spi0.close()
    spi1.close()
    GPIO.cleanup()

Advantages of the DualCE SPI-GPIO Bridge Technique

Works with unlimited SPI devices

No special hardware multiplexer required

Easy to implement in Python

Compatible with any Raspberry Pi model

Conclusion

The DualCE SPI-GPIO Bridge Technique is a simple yet powerful way to expand your Raspberry Pi’s SPI capabilities. By mixing hardware and software chip select handling, you can connect as many devices as you need.

sachin Pagar

Mr. Sachin Pagar is an experienced Embedded Software Engineer and the visionary founder of pythonslearning.com. With a deep passion for education and technology, he combines technical expertise with a flair for clear, impactful writing.

Leave a Reply