How to use the SPI on the Zero

Hi,

I'm trying to connect my zero to a memory using the spi interface. I saw that I can't use the pins 10, 11, 12 and 13 like i used to do with the Uno board. I was wondering if someone could explain how to use the SPI interface on the Zero.

Thanks.

Hi @pedrohenriquefguerra,

You'll have to use the pins on the ICSP header for SPI, as mentioned on the product page Arduino Official Store | Boards Shields Kits Accessories

A diagram of the header pin layout can be found here:

You can continue to use pin 10 for SS.

Hi @sandeepmistry,

Thank you for your quick response, but i have another question.
Which pin I need to define for MISO, MOSI and SCK?

I found this in variants.cpp:

|SPI (Legacy ICSP) |
22 | 1 | PA12 | MISO | EIC/EXTINT[12] SERCOM2/PAD[0] *SERCOM4/PAD[0] TCC2/WO[0] TCC0/WO[6]
| 2 | | 5V0 |
23 | 4 | PB10 | MOSI | EIC/EXTINT[10] *SERCOM4/PAD[2] TC5/WO[0] TCC0/WO[4]
24 | 3 | PB11 | SCK | EIC/EXTINT[11] *SERCOM4/PAD[3] TC5/WO[1] TCC0/WO[5]
| 5 | | RESET |
| 6 | | GND

and I used this code to check with an oscilloscope the signals:

#include <SPI.h>

#define DATAOUT 23//MOSI
#define DATAIN 22//MISO
#define SPICLOCK 24//sck
#define SLAVESELECT 10//ss

void setup()
{

pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
delay(10);

SPI.beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE3));
digitalWrite(SLAVESELECT,HIGH); //release chip
}

void loop()
{
digitalWrite(SLAVESELECT, LOW);
SPI.transfer(0b00000110); //write enable
digitalWrite(SLAVESELECT, HIGH);
//delay(1);
digitalWrite(SLAVESELECT, LOW);
SPI.transfer(0b00000010); //write command

// Transfer memory address
SPI.transfer(0b00000000);
SPI.transfer(0b00000000);
SPI.transfer(0b00000000);

// Transfer value (byte)
SPI.transfer(0b01010101);
SPI.transfer(0b01010101);
SPI.transfer(0b11110000);
SPI.transfer(0b00001111);

digitalWrite(SLAVESELECT,HIGH);
//delay(1);
//SPI.endTransaction();
}

but i’m only able to see the slave select, do you have any idea why?

Thanks.

The following is not needed:

  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);

As the SPI library will set this up for you, if you call "SPI.begin()" instead :slight_smile:

hi,

i am trying to interfacing SD card with Arduino zero with same MOSI, MISO and SCK pin, as ICSP Header

but my SS pin is PA13–> EDBG_GPIO0.

i don’t know how to use this pin and how to configure on sketch.

can anyone guide me, please.

Are there any simple SPI master/slave demo sketches specifically for the Arduino Zero? The Internet has plenty of short simple sketches for UNO class boards (without an embedded ESP8266), but the sample IDE examples for SPI compile only for the newer NodeMCU, Wemos, and Adafruit Huzzah boards. The IDE doesn't even show any sample SPI sketches when the IDE is configured for the Zero. Thanks!!