October 29, 2024, 20:59
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 GNDI 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