Using The Second SPI Port – SPI1

SPI1 Demo
In this post, we’re going to quickly tell you how to use the second hardware SPI port that’s present on the Pico/Nano boards. The board support package (BSP) already contains the required libraries so all that’s needed to do is call the appropriate functions in your sketch.

Wiring:

The default hardware SPI port uses the D10 (SS), D11 (MOSI), D12 (MISO) & D13 (SCK) pins for communication. This is very common across almost all the Arduino boards. The second hardware I2C port uses the A6 (SS), A7 (MOSI), A0 (MISO) & A1 (SCK) pins for communication.

For this example, we are going to interface a 3rd party 8×8 LED matrix board (MAX7219) that uses SPI for communication. The Pico and Nano are both 5V boards so please make sure the module you use is 5V compatible or else you will need to use a level shifter to convert the 5V signals to 3.3V and vice-versa.

Here’s what the connections look like:

  • Connect the CLK pin to A1
  • Connect the SS pin to A6
  • Connect the MOSI pin to A7
  • Connect the 5V and GND pins to the microcontroller or appropriate power source

SPI1 Wiring SPI1 Wiring

The Sketch:

Using the new SPI port is very simple. You simply need to use SPI1 instead of SPI. Here’s what the sketch looks like:

void setup()
{
  pinMode(slaveSelectPin, OUTPUT);

  SPI1.begin();
  initMAX7219();
}

void loop()
{
  for(int i=1; i<9; i++)          //Display B
      writeTwoBytes(i,bytesB[i-1]);
  delay(500);                    
  
  for(int i=1; i<9; i++)          //Display N
      writeTwoBytes(i,bytesN[i-1]);
  delay(500);                    

  for(int i=1; i<9; i++)          //Display B
      writeTwoBytes(i,bytesB[i-1]);
  delay(500);                    
  
  for(int i=1; i<9; i++)          //Display E
      writeTwoBytes(i,bytesE[i-1]);
  delay(500);     

  for(int i=1; i<9; i++)          //Clear display
      writeTwoBytes(i,0x00);
  delay(500);
}

We start by setting up the SPI port we then call the initialization function for the module which initializes the MAX7219 chip. We then repeatedly display the characters BNBE on the module.

Here’s what the function that sends two bytes to the MAX7219 looks like:

void writeTwoBytes(unsigned char byte1, unsigned char byte2)
{
  digitalWrite(slaveSelectPin, LOW);
  SPI1.transfer(byte1);
  SPI1.transfer(byte2);
  digitalWrite(slaveSelectPin, HIGH);
}

We have omitted the initialization function from this post as that’s dependent on the module that’s being interfaced. You can download the entire sketch using the link below if you need to use it.

Here’s what the output looks like:

SPI1 - Demo SPI1 Demo

Download the SPI1 demo sketch here.