Connecting 2 Photon via SPI

Hi everybody. I’m trying to connect two Photons via SPI. Photon1 is the master and Photon2 the slave. I have read the SPI Firmware and there’s an example of how to use it, but I want to do a much simplier example… I’m just sending a 0 or 1 with the Master and I’m not sure if my code is OK…

const int dataLED = A1;
int slaveSelected;                           //Flag
uint8_t transferData;


void onSelect(uint8_t state) {           //Slave Handler
    if (state == 0) {                           //Slave selected
        slaveSelected = 0;
    }
    else {                                        //Slave not selected
        slaveSelected = 1;
    }
}


void setup() {
    Serial.begin(9600);
    SPI.onSelect(onSelect);                   //Register a Handler to get called when SS changes state
    SPI.begin(SPI_MODE_SLAVE);       //Configures SS, SCK and MOSI as inputs and MISO as output
    pinMode(dataLED, OUTPUT);
}


void loop() {
    if (slaveSelected == 0) {                   
        SPI.transfer(transferData);          //Transfer data from Master   
        if (transferData >= 1) {                //Master sent "1"
            digitalWrite(dataLED, HIGH);        
        }
        else {                                        //Master sent "0"
            digitalWrite(dataLED, LOW);        
        }
    }
}

If it does what you want it is.
If not, tell us what isn’t working.

Have you tried the original sample code?
Did it do what it should?
If so, look at the differences.
If not, check your wiring.

Hi @Anglix

I wrote and tested some code over here to test i2c between two Photons:

The thing that everyone seems to forget is that you need a common ground between the devices. So for SPI, you would need MOSI, MISO, SCK and GND between the devices. You technically don't need a chip select unless you have other SPI devices on the same lines, but it doesn't hurt to add the SS wire.

Has anyone done more with SPI between two photons? I don’t really understand @Anglix code at the top of this topic. Not sure if the same code is flashed to both photons or if that code is only for one photon.

I am looking for a simple SPI Slave and Master code for two photons. Any suggestions @peekay123?

Thanks @bko your I2C code between two photons was really useful.

It took a lot of work to get this going. Here is my working code with the help of @rickkas7

The github is at

The 2 Photon SPI Serial breadboard:

The SPI Master

// SPI-Master.ino
// By Jeremy Ellis
// MIT license
// Use at your own risk!

const unsigned long SEND_PERIOD_MS = 10000;
const int SS_PIN = A2;
const size_t NUM_VALUES = 32;

unsigned long lastSend = 0;
uint32_t rcvdValues[NUM_VALUES];
uint32_t sendValues[NUM_VALUES];

String myMasterSent = "Hello to Slave";
String mySlaveSent ;

void setup() {

SPI.begin(SPI_MODE_MASTER, SS_PIN);
}

void loop() {
if (millis() - lastSend >= SEND_PERIOD_MS) {
lastSend = millis();

  digitalWrite(SS_PIN, LOW);
  for(size_t ii = 0; ii < NUM_VALUES; ii++) {
  	sendValues[ii] = (byte)myMasterSent[ii];
  }
  SPI.transfer(sendValues, rcvdValues, NUM_VALUES * sizeof(uint32_t), 0);
  digitalWrite(SS_PIN, HIGH);

}
mySlaveSent = "";
for(size_t ii = 0; ii < NUM_VALUES; ii++) {

  mySlaveSent  += 'H';   // for setCharAt to work the string needs a character to replace
    mySlaveSent.setCharAt(ii, (char)rcvdValues[ii] );    // update string

}

Particle.publish( "Master sent " + myMasterSent, "Getting from Slave "+ mySlaveSent  , 60, PRIVATE);

delay(9000);
}

The SPI-slave.ino

// SPI-Slave.ino
// By Jeremy Ellis
// MIT license
// Use at your own risk!

void slaveSelect(uint8_t state);
void slaveCallback();

const int SS_PIN = A2;
const size_t NUM_VALUES = 32;

bool gotValue = false;
String sentToMaster="Hi from slave";
String gotFromMaster;

uint32_t rcvdValues[NUM_VALUES];
uint32_t sendValues[NUM_VALUES];

void setup() {

for(size_t ii = 0; ii < NUM_VALUES; ii++) {
sendValues[ii] = (byte)sentToMaster[ii];
}

SPI.onSelect(slaveSelect);
SPI.begin(SPI_MODE_SLAVE, SS_PIN);
}

void loop() {
if (gotValue) {
gotValue = false;
gotFromMaster = "";

  for(size_t ii = 0; ii < NUM_VALUES; ii++) {
      gotFromMaster  += 'H';   // for setCharAt to work the string needs a character to replace
        gotFromMaster.setCharAt(ii, (char)rcvdValues[ii] );    // update string
  }
  Particle.publish( "Slave sent "  + sentToMaster, "Getting from Master " + gotFromMaster, 60, PRIVATE);
  delay(2000);   

}
}

void slaveSelect(uint8_t state) {
if (state) {
SPI.transfer(sendValues, rcvdValues, NUM_VALUES * sizeof(uint32_t), slaveCallback);
}
}

void slaveCallback() {
gotValue = true;
}

2 Likes