KevsRobots Learning Platform
By Kevin McAleer, 2 Minutes
In this lesson, you’ll learn about the SPI (Serial Peripheral Interface) bus. SPI is faster than I2C and is often used for colour displays and SD cards.
SPI is a synchronous bus that uses four wires:
SCK - the clock, driven by the PicoMOSI (also called TX or SDO) - data from the Pico to the deviceMISO (also called RX or SDI) - data from the device to the PicoCS (chip select, sometimes SS) - set low to tell one device “I’m talking to you”Several devices can share SCK, MOSI and MISO, but each needs its own CS pin.
The Pico has two SPI buses, SPI0 and SPI1. This example uses SPI0 on GP2 (SCK), GP3 (MOSI) and GP4 (MISO), with GP5 as chip select:
from machine import Pin, SPI
spi = SPI(0, baudrate=1_000_000, polarity=0, phase=0,
sck=Pin(2), mosi=Pin(3), miso=Pin(4))
cs = Pin(5, Pin.OUT, value=1) # high means "not selected"
polarity and phase describe how the clock behaves. Mode 0 (both 0) is the most common; your device’s datasheet will say which it needs.
cs.value(0) # select the device
spi.write(b'\x01\x02\x03') # send three bytes
data = spi.read(3) # read three bytes back
cs.value(1) # deselect
print(data)
SPI sends and receives at the same time. To send and capture the reply in one go, use write_readinto():
reply = bytearray(3)
cs.value(0)
spi.write_readinto(b'\x9f\x00\x00', reply)
cs.value(1)
Quick quiz 1 of 2
What is the chip select (CS) pin for?
Quick quiz 2 of 2
Which is usually faster?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments