Raspberry Pi Zero 2 W to Raspberry Pico W SPI Problem

October 29, 2024, 20:59

sociopathchick

Hello everyone! I want to communicate SPI between Raspberry Zero 2 W and Raspberry Pi Pico W, but I have not been able to send even a simple test data. "I may make grammatical or word errors when explaining the problem, sorry I am not very good at technical explanations or English" Before I wrote a help article here, I checked a lot to make sure that it was a very simple error and I didn't overlook it. I looked at many videos and sources. I also asked AIs ChatGPT Claude etc. but I couldn't find a clear solution. If anyone has made SPI connection between RPi and Pico before, or if you want to share any different ideas or code, I am always ready to try. In short, I will refer to the model names as Pico(Raspberry Pico W) and Pi(Raspberry Pi 5). Since RPIZERO2W is soldered, I do my tests on RPI5. For the test I wrote a simple SPI code between Raspberry Pi 5 and Pico. For the Pi (Master)
import spidev
import time

# Initialize SPI
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 100000

# Function to send data and read response
def send_and_receive():
    send_data = [0x01, 0x02, 0x03]
    print("Sending:", send_data)
    received_data = spi.xfer2(send_data)
    print("Received:", received_data)

try:
    while True:
        send_and_receive()
        time.sleep(1)

except KeyboardInterrupt:
    print("Exiting...")

finally:
    spi.close()
and for the Pico(Slave)
from machine import Pin, SPI
import time

# Configure SPI as Slave on the Pico W
spi = SPI(0, baudrate=100000, polarity=0, phase=0, bits=8,
          sck=Pin(18), mosi=Pin(16), miso=Pin(17))
cs = Pin(19, Pin.IN, Pin.PULL_UP)

response_data = [0xAA, 0xBB, 0xCC]

while True:
    if cs.value() == 0:
        received_data = spi.read(3)  # Read 3 bytes from master
        print("Received from Master:", received_data)
        spi.write(bytearray(response_data))
        print("Sent response data:", response_data)
        time.sleep(0.1)
I used the codes but it did not work.
PIN     RPI5        PICO
__________________________
MOSI    GPIO 10     GPIO 16<--
MISO    GPIO 9      GPIO 17<--
SCK     GPIO 11     GPIO 18
CS0     GPIO 8      GPIO 19
GND     GND         GND
__
__
PIN     RPI5        PICO
___________________________
MOSI    GPIO 10     GPIO 17<--
MISO    GPIO 9      GPIO 16<--
SCK     GPIO 11     GPIO 18
CS0     GPIO 8      GPIO 19
GND     GND         GND
I also changed the location of the MOSI and MISO pins, but again it did not work. I also tried with CS1(CE1) on Pi, but it still did not work. I also tested an SPI connection between Pico W and Pico W. However, I could not get it to work this way either. Pico(Master)
from machine import Pin, SPI
import time

spi = SPI(0, baudrate=10000, polarity=0, phase=0, bits=8,
          sck=Pin(18), mosi=Pin(19), miso=Pin(16))
cs = Pin(17, Pin.OUT)  # Chip Select (CS) pin

def send_and_receive():
    cs.value(0)
    time.sleep(0.01)
    received_data = spi.xfer([0x55])
    print("Received data:", received_data)

    cs.value(1)
    time.sleep(1)

try:
    while True:
        send_and_receive()

except KeyboardInterrupt:
    print("Exiting...")

Pico(Slave)
from machine import Pin, SPI
import time

spi = SPI(0, baudrate=10000, polarity=0, phase=0, bits=8,
          sck=Pin(18), mosi=Pin(19), miso=Pin(16))
cs = Pin(17, Pin.IN, Pin.PULL_UP)  # CS as input with pull-up
response_data = [0xAA, 0xBB, 0xCC]

def receive_and_respond():
    if cs.value() == 0:
        received_data = spi.read(3)
        print("Received data from Master:", received_data)
        spi.write(bytearray(response_data))
        print("Sent response data:", response_data)
        time.sleep(0.1)

while True:
    receive_and_respond()
    time.sleep(0.1)
I have tried several different baudrate speeds, but I could not get it to work at any of them. For Ex: 1000000 / 500000 / 50000 / 100000

sociopathchick

Also, I am still not sure what is the right Pi library I should use in this project? I would like your opinion on this subject. GPIOD, GPIOZERO or RPI.GPIO, which one is more up to date and has better documentation? I'm not asking about the SPI problem.