Establishing SPI between XIAO nrf52840 and Arduino nano

i think new posters are not allowed to post pictures… sorry

Hi there,
New folks only after 3 or 4 posts, can you attach stuff.
HTH
GL :slight_smile: PJ
:v:

So how do I share the pictures of my outputs and connections here?

Hi there,
Ok let’s assume you have it wired like
8-13 SCK
11-10 MOSI
12-9 MISO
10-7 <—OUTput to—> INPUT here…Master/Slave

FYI the Xiao is Slave so it’s SS(CS) should be an Input. and rename to CS (chip select) do a define as such as well.
You’re close… you’ll get it :+1: fix the direction and besure to DE-assert the pin (SS ) when done and Assert it when Starting transfer.
Look at how some of the Flash SPI chips do it… ie. Xiao Grove expansion board demo.
HTH
GL :slight_smile: PJ
:v:

Yes, I have done as mentioned.

Now the code looks this:
// Master (Arduino NANO)

#include <SPI.h>
#define SS 10
#define MOSI 11
#define MISO 12
#define SCK 13

byte Send_Data;
byte Receive_Data;
void setup() {
pinMode(MOSI,OUTPUT);
pinMode(MISO,INPUT);
pinMode(SCK, OUTPUT);
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);

Serial.begin(115200);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
}

void loop()
{
digitalWrite(SS, LOW); //Assertion
Send_Data = 10;
SPI.transfer(Send_Data);
Receive_Data = SPI.transfer(Send_Data);
digitalWrite(SS, HIGH); //De-Assertion
Serial.print("Sent: ");
Serial.println(Send_Data);
Serial.print("Received: ");
Serial.println(Receive_Data);
delay(1000);
}

I am still not getting the required output :smiling_face_with_tear:

When the master and the slave devices are connected but the master is not powered my outputs are 0’s but when it is powered, the outputs are 255.

Hi there,
Keep in mind the Xiao outputs are active LOW. so you ay need to invert your logic?
and the XIao is SLAVE so it 's SS needs to be input, right?

pinMode(D6, INPUT_PULLUP);  // /WP
  //pinMode(D7, INPUT_PULLUP);  // /HOLD
  pinMode(SS_SPI0, OUTPUT);          // CS for flash
  digitalWrite(SS_SPI0, HIGH);       // <-- Set CS pin HIGH to deselect
  pinMode(buttonPin, INPUT_PULLDOWN);  // initialize the pushbutton pin as an input: for GROVE PB

from this example, it is a little confusing and is a reverse logic, see comments
So close You are…LOL :+1:

HTH
GL :slight_smile: PJ
:v:

Pardon me for not mentioning, but I provided the code for the master microcontroller since I have triggered the SS signal from the master itself. Here I am providing the code for slave as well

// Slave (Seeed Studio nRF52840)

#include <SPI.h>
#include <Adafruit_TinyUSB.h>
#define CS 7
#define SCK 8
#define MISO 9
#define MOSI 10

int data_receive = 0;

void setup()
{
pinMode(MOSI,INPUT);
pinMode(MISO,OUTPUT);
pinMode(SCK, INPUT);
pinMode(CS, INPUT);

Serial.begin(115200);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8); // SPI speed
SPI.setDataMode(SPI_MODE0); // SPI mode
SPI.setBitOrder(MSBFIRST); // Most significant bit first
}

void loop()
{
if(digitalRead(CS)==LOW)
{
data_receive = SPI.transfer(0);
while(digitalRead(CS) == LOW);
Serial.print("Received: ");
Serial.println(data_receive);
data_receive = 0;
}
}

Hi there,
Great , yes my bad, you add the print of the pins for SPI at the end to verify, use the SS nomenclature.
Sorry, for CS
Also add some delays in between the read and writes of SPI. that usually does the trick
HTH
GL :slight_smile: PJ
:v:

Hi!
Please find the snippet of the Master code where I have added the delays. Kindly let me know if I have done it right.

void loop()
{
digitalWrite(SS, LOW);
Send_Data = 10;
SPI.transfer(Send_Data);
delay(1000);
Receive_Data = SPI.transfer(Send_Data);
delay(1000);
digitalWrite(SS, HIGH);
}

I am still not able to get the output.

Hi there,
Can you switch the (SS) logic to opposite of what you have and try again.

SS = D7;
void loop()
{
digitalWrite(SS, HIGH);
Send_Data = 10;
SPI.transfer(Send_Data);
delay(1000);
Receive_Data = SPI.transfer(Send_Data);
delay(1000);
digitalWrite(SS, LOW);
Serial.println("Master sent");
}

Which BSP are you using for the Nrf52840 Xiao suggesting 1.1.1
HTH
GL :slight_smile: PJ
:v:

Hey, I have made the changes.

Output from the master.
Master sent
Sent: 10
Received: 0
Master sent
Sent: 10
Received: 0

This is the output from the slave:
Received: 0
Received: 255
Received: 255

Now what to do? The BSP I have installed is 1.1.8.

Hi there,
What kind of variable is Send and received data I don’t see any assignment of is being hex or ascii or decimal. BSP 1.1.8 should be ok, Check and make sure your libraries are the correct ones Look at you compiler output to see they are what you think version wise.
You can go to the board tab and roll it back to 1.1.1 if just to test. if you get the same output then go with the latest. The SPI may be different for each MCU.
back out the delay’s reducs them and also what SPI clock divider are you setting if any, for the SPi buss speed.
HTH
GL :slight_smile: PJ
:v:
keep going. :+1:

Arduino_nano’s input/output is 5V logic and XIAO’s input/output is 3.3V logic. Normally, you need a level shifter to connect them. Has this been discussed anywhere in this thread?

1 Like

Hi there,
as always , you spot the issue easily over looked…
OP may want to look at that as he makes NO mention of it anywhere.
I completely missed that myself. for some reason I was thinking it was a Arduino Due’
GL :slight_smile: PJ
:v:

… Duo… Tripo… What’s the diffo?..lol

Hi there,
One is a 5v signal and the others Are 3vsignal compatible…
Nano is a 5V hence the level shifter and why he’s getting 255’s
HTH
GL :slight_smile: PJ
:v:

Hi,

I apologize for the late response. I have tried the same codes after connecting both the devices with a level converter. I am unable to see any output on the receiver’s end.

But, the outputs from the master’s end has been showing some variation.

When the level converter has a 5V to 3V connection:
Master sent
Sent: 10
Received: 255

When the level converter does not have a 5V to 3V connection:
Master sent
Sent: 10
Received: 0

Hi user_rk,
I assume you need a bi-directional level converter, what level converter did you use?

1 Like

Just upgrade to newer hardware boss…

It is bi-directional