SPI between Arduino Master - Photon Slave

Hey Everyone,

I am quite new to use Photon and my goal with this SPI interface will be to lately transfer sensors data to the Cloud.

My sensors are being measured by an Arduino and to make sure I was using a correct code on this end I used the DigitalPotControl example in SPI library with some Serial notes to help debug.

On the other end, I have my Photon defined as slave.

In addition to this code I also added:
#include <Arduino.h>
#include <SPI.h>
In the beginning of my code.

As you can see I added some Particle.publish() to help me see how the code is running.
In my events page, I have both SPI: IN LOOP and SPI: SS is ON showing up.
This means the slave pin is being pulled down by the Arduino.

However, SPI.available() stays 0 all this time and doesn’t allow the transfer.
I also tried to replace SPI.available() condition by the state of Slave pin. However, it prints the rx_buffer as NULL.

Any ideas where this problem might arise from? My last two days have basically been focused on this one issue (even though a special important one).

I guess you have that digitalWrite(A2, HIGH) to apply a pull-up resistor, right?
But that’s not how that works with the Photon (and Arduino has also adopted this syntax a while ago IIRC)

  pinMode(A2, INPUT_PULLUP);

You also don’t want to have any unnecessary delays in your code in order to respond to any SPI requests quickly.
For debugging rather use Serial.print() via the USB port. This is much faster and won’t interfere half as much as Particle.publish() with the extra delays required.
Even faster visual feedback would be using D7 as SCLK or by just toggling the D7 LED in code conditionally.

If you want to share all your code you should use the SHARE THIS REVISION feature in Web IDE.


Thanks so much for the fast reply. Clearly didn’t expect that.

I changed it with your line of code, but the issue remains. SPI.available() is constantly 0.

The only reason I use my delay(1000) is because Reference said that Particle.publish can only upload one event per second and before I had these delays it would not work. After I added them it started working.

That’s why I had added these points

1 Like

I have installed it with my terminal, however it is not accepting any of my commands. When I execute it, it shows the below picture

But if I make a command I says command not found.

I guess you mean Particle CLI when you say

While it is always good to have CLI installed I’m not sure where that was mentioned.
Obviously it did work once, when you executed particle --help but after that you must have lost the reference to CLI. That error message does not mean that CLI doesn’t recognise the command you passed but that the OS doesn’t find particle

I guess you have not used the -g (global) switch when you installed CLI.
It should be npm install -g particle-cli

However, to catch the serial output of Serial.print() you won’t need CLI. Any serial terminal program will do.

Well, after some time dwelling on that I finally saw how to use the Serial in my terminal.

However the problem remains, SPI.available() is not changing when SS is 0.

That brings us back to what I said earlier


Sorry about that. As you can see I am clearly a new Particle User.

That does the trick.
You should also setup a callback for the slave to act when the CS pin gets pulled low
SPI.onSelect() and especially the sample code for it should be the key to your puzzle.