-1

Hello Guys I Am New To Embedded & Rasberry PI. So In Order To Understand SPI Connection I Made An PI-PI SPI Setup
PS. I am Using Two Rasbrry Pi Model 4 B with Raspi OS & i did the followings steps:

STEP 1:

First I enabled Spi In My Both Rasberry Pi 4 by going through sudo raspi-config & Enable 4. SPI Interface

STEP 2:

After Did I Made Physical Connection From 1st Rasberry Pi(Master) To 2nd Rasberry Pi(Slave) Through Female To Female Jumper To The Followings Pin Outs

24(CE0-Master) To 24 (CE0-Slave) // CHIP_SELECT 0 TO 0 (PIN 24)
23(S_CLK Master) To 23 (S_CLK Slave) // SERIAL CLOCK PIN 23
21(MISO Master) To 21 (MISO Slave) // MISO PIN 21
19(MOSI Master) To 19 (MOSI Slave) // MOSI PIN 19

I followed Pin Out Refrence From:
RasPi-PinOut

STEP 3:

After That I Written The Following Code To Respective Rasberry Pi 4

For MasterSPI.PY

import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)  # Open SPI bus 0, device 0

try:
    while True:
        send_data = [0x55, 0xAA, 0x33]  # Replace with the data you want to send
        received_data = spi.xfer2(send_data)
        print("Received:", received_data)
        time.sleep(12)

except KeyboardInterrupt:
    pass

spi.close()

For SlaveSPI.PY

import spidev

spi = spidev.SpiDev()
spi.open(0, 0)  # Open SPI bus 0, device 0
spi.mode = 0b01

try:
    while True:
        received_data = spi.xfer2([0x00, 0x00, 0x00])  # Receive data from the master
        # Process received_data as needed
        print("Received:", received_data)
        time.sleep(2)
except KeyboardInterrupt:
    pass

spi.close()

STEP 4:

Runned This Master & Slave Script In Thier Respective Terminal Using python3 MasterSPI.py &python3 SlaveSPI.py

NOTE::I Though After That MY Both Rasberry Pi 4 Will Communicate But On The Terminal It looks It Would Work But After Stopping MasterSPI Script Also on Slave Terminal It Was Showing Reieved 0,0,0 Only After Every 2 Seconds

I am Not aware If i am doing this correctly or not But There Was No Online Tutorial To Do Such Was Availiable so i did What Found From Various Platform , So If Some Body Wants To Give Suggestion About Tutorial Or The above Method Pls Feel Free To Comment Down

Thanks For Your Time

2
  • @Milliways Why it is difficult To Understand I Made It Pretty Clear Every Step I did & Second thing is what is meant by MISO TO MISO Would Nor Work i tried this reference online radiostud.io/understanding-spi-in-raspberry-pi
    – Its_MEDev
    Sep 28, 2023 at 16:01
  • the refence in the diagrammes shows to connet MISO to MISO and MOSI to MOSI
    – Its_MEDev
    Sep 28, 2023 at 16:07

1 Answer 1

1

To understand SPI, you need a master and a slave. The Raspberry Pi is a master. You have two master which creates many problems. There are two clock running which are tied together and the output will be a mess. To make a Raspberry Pi a slave would require a lots of hacking, there is no default way to make it a slave.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.