spi communication between arduino and raspberry pi

i want send data from my arduino to raspberry pi. Serial transfer through is not working. So I decided to transfer using spi communication, does anyone know how to perform this task.

If the serial transfer is not working, then you are doing something wrong. Passing data to and from Arduinos via serial is a common occurance.

Should be easier to fix that than get involved in the complications of setting up either the Arduino or the Pi as an SPI slave.

thanks for the reply, will check again serial interface.

Hi there, I have used both the communications between Arduino and RPi, but as per your question, I am going to answer the Problem related to SPI. Heres the code which is in AVR and here RPI is the Master and Arduino is the slave. Change the pins as per the board you use. The is code is BASIC 8-bit data transfer in SPI communication. I hope you have the RPI’s Master code, easily available in Tutorial on youtube.

Hardware Connections

ARDUNIO - RPI
MISO - MISO
MOSI - MOSI
SCK - SCK
SS - CS0

CODE (IN AVR)

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#define SS 2  // Mega = 0,2,3,1  Nano/Uno = 2,3,4,5
#define MOSI 3  //
#define MISO 4  // 
#define SCK 5  // 

char data;

void SPI_S_init()
{
  // Enable only One mode at time
  DDRB = (1 << MISO); // I/P
  SPCR = (1 << SPE) | (1 << SPIE) | (0 << CPOL); // EN, Mode 0 for Multiple and Singular
  //  SPCR = (1 << SPE) | (1 << SPIE) | (0 << CPOL); // EN, Mode 1
  //  SPCR = (1 << SPE) | (1 << SPIE) | (1 << CPOL); // EN, Mode 2
  //  SPCR = (1 << SPE) | (1 << SPIE) | (1 << CPOL); // EN, Mode 3 for Single slave
  SPDR = 0;
}

int main(void)
{
  SPI_S_init();
  sei();
  Serial.begin(230400);  // 
  while (1)
  {
    Serial.println(data);
  }
}

ISR(SPI_STC_vect)
{
  data = SPDR;
}

I hope this Helps !!! :slight_smile: