How to connect multiple SPI sensors/devices with Arduino?

I want to connect two SPI devices to Arduino (as slave), How can I do it?
Answer

You can connect not only two SPI devices but also three or more SPI devices to Arduino.

If you takes a look to SPI reference, you will know that SPI requires four pins: MOSI, MISO, SCK, and SS . Among them:

If we use only one SPI slave device, we usually use the default SS pin (ex pin 10 on Arduino Uno). When we use multipe SPI slave devices we need to do the following.

Wiring Connection

Assign any digital pin as SS pin to connect to each devices. Each selected pins should be used for only one deivces and not used for other purposes. Avoid using pin 0 and pin 1 because these pins are used for Serial communication.

Arduino Code

Look at to the library or example code of each slave device. Find the way to set SS pin according to the assigned SS pin. For example RC522 RFID/NFC Module, we will see below code:

#include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 5 MFRC522 rfid(SS_PIN, RST_PIN);

We can change SS pin to the new SS pin. For example, using pin 4 as SS pin.

#include <SPI.h> #include <MFRC522.h> #define SS_PIN 4 #define RST_PIN 5 MFRC522 rfid(SS_PIN, RST_PIN);

Buy Arduino

1 × Arduino UNO Buy on Amazon
1 × USB 2.0 cable type A/B Buy on Amazon
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

The Best Arduino Starter Kit

※ OUR MESSAGES