Table of Contents
Introduction
Raspberry Pi supports both I2C and SPI protocols, but combining them in a single project often confuses developers. In this guide, we introduce the Hybrid-Bus Control Method, which allows you to use I2C and SPI devices together in Python without conflicts.
Hardware Setup
- Raspberry Pi (any model)
- 1 I2C device (e.g., BMP280 sensor)
- 1 SPI device (e.g., MCP3008 ADC)
- Breadboard + jumper wires
Connections:
- I2C: SDA (GPIO2), SCL (GPIO3)
- SPI: MISO, MOSI, SCLK, CE0
- Ground and 3.3V shared
Enable Interfaces
sudo raspi-config
# Enable I2C and SPI under Interface Options
sudo reboot
Python Code (Hybrid-Bus Control Method)
import smbus2
import spidev
import time
# I2C Setup (BMP280 example)
i2c = smbus2.SMBus(1)
BMP280_ADDR = 0x76
# SPI Setup (MCP3008 example)
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1000000
def read_i2c_temp():
# Example: read chip id register
chip_id = i2c.read_byte_data(BMP280_ADDR, 0xD0)
return chip_id
def read_spi_channel(channel=0):
adc = spi.xfer2([1, (8+channel)<<4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data
try:
while True:
temp_sensor = read_i2c_temp()
adc_value = read_spi_channel(0)
print(f"I2C Temp Sensor ID: {temp_sensor}, SPI ADC Value: {adc_value}")
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
spi.close()
i2c.close()
Why Use the Hybrid-Bus Control Method?
- Works with both I2C and SPI devices on the same Pi
- Avoids bus conflicts by separating function calls
- Scales for multiple sensors (mix of I2C + SPI)
- Fully Python-compatible
Conclusion
With the Hybrid-Bus Control Method, you can easily run both I2C and SPI devices in Python on your Raspberry Pi. This approach is lightweight, flexible, and requires no extra hardware.