SPI multi slave with NFC PN532 and SD Card

Hi guys !

I encountered some problems with TinyDuinos. It's a platform based on arduino and ATMega328P. You can find it here : https://www.tiny-circuits.com/

Si I want to connect a PN532 from adafruit (Adafruit PN532 NFC/RFID Controller Shield for Arduino + Extras : ID 789 : $39.95 : Adafruit Industries, Unique & fun DIY electronics and kits) in SPI and a SD card from Tiny-Circuits (https://www.tiny-circuits.com/tiny-shield-micro-sd.html) which is already in SPI on pin 10.

I put the SS pin of the NFC board on IO4. When i use the NFC board or the SD Card separately it works fine.
However, when i try to initialize both, the SD Card failed and the NFC board too.
I suppose it's because SD use pins that NFC need or inverse.

Here is my code, very simple sketch :

#include "PN532_SPI.h"
#include "PN532.h"
#include "NfcAdapter.h"

#include <SPI.h>
#include <SD.h>

/** NFC ATTRIBUTES */
PN532_SPI interface(SPI, 4); // create a PN532 SPI interface with the SPI CS terminal located at digital pin 10
NfcAdapter nfc = NfcAdapter(interface); // create an NFC adapter object

void setup() {
  Serial.begin(9600);


 if (!SD.begin(10)) {
  Serial.println(F("failed"));
 }
 else {
  Serial.println(F("success"));
 }

  nfc.begin();
}

void loop() {
  // put your main code here, to run repeatedly:

}

I use this libraries :
NFC : Seed studio
SD : Adafruit SD

Thank you very much !

Maxouille

I suppose it's because SD use pins that NFC need or inverse.

They both use the SPI pins - 11, 12, and 13. But that is OK, since only the device who's slave select pin is low will get the message.

PN532_SPI interface(SPI, 4); // create a PN532 SPI interface with the SPI CS terminal located at digital pin 10

When useless comments do not agree with the code, you look like an idiot. You might want to fix one or the other. Or get rid of the useless comment.

Pin 10 MUST be declared as an OUTPUT.

Ahaha I did not see the comment :slight_smile:

I think I found a solution with this post : Two SPI devices not working [SOLVED] - Networking, Protocols, and Devices - Arduino Forum

It seems that I used Software SPI for NFC and hardware SPI for SD which are incompatible. MOSI, MISO and SCK cannot be declared on different pins.

For the moment, it seems to work. 5 times running and it works well