Search code examples
arduinodisplayesp8266rfidpn532

ESP8266 (Nodemcu) + PN532 (RFID) + ST7735 (Display) in one setup possible?


I am trying to get an RFID-Reader (PN532) to work with a display, so it is shown there, who has scanned his RFID-Card. The problem I ran into was, that 2 pins (D7 HMOSI) and (D5 HSLCK) are used by both devices. Thus I simply put both connections on those. (wrong?)

Now when initializing either of both devices, the other one gets disabled.

I use Adafruit to initialize both devices.

In addition to this, the ESP8266 does not start when the RFID-Reader is connected. Removing the Pin from 3.3Volt VCC and waiting for init, then Adding the Pin, only then the RFID-Reader gets recognized and the ESP8266 runs. (bad case for crashes, as it would never reboot)

This is my cable setup:

enter image description here

Also here is my code:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

#define PN532_SCK  (14)
#define PN532_MOSI (13)
#define PN532_SS   (15)
#define PN532_MISO (12)

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789

#define TFT_CS         5
#define TFT_RST        16                                            
#define TFT_DC         4

Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

void testdrawtext(char *text, uint16_t color) {
  tft.setCursor(0, 0);
  tft.setTextColor(color);
  tft.setTextWrap(true);
  tft.print(text);
}

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

  Serial.print(F("Hello! ST7735 TFT Init"));
  tft.initR(INITR_BLACKTAB);      // Init ST7735 chip, black tab
  Serial.println(F("Initialized"));
  tft.fillScreen(ST77XX_BLACK);

  while (!Serial) delay(10); 
  Serial.println("Hello! PN532 RFID Init");

  nfc.begin();   // Init PN532 chip

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN532 board");
    while (1); // halt
  }
  
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  nfc.setPassiveActivationRetries(0xFF);
  nfc.SAMConfig();
  
  Serial.println("Waiting for a Card");
}

Solution

  • The constructor

     Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
    

    creates a 'driver' which uses software SPI. But you supply pins of hardware SPI as parameters. Hardware SPI is used by the Adafruit_ST7735 library over the SPI library to access the display so hardware SPI conflicts with the software SPI of the PN532 library.

    Use

    Adafruit_PN532 nfc(PN532_SS);
    

    constructor which uses the hardware SPI over the SPI library. The SPI library 'knows' the pin numbers of the SPI pins. (SPI library is part of the boards package. It can't be installed separately.)

    And don't use SS (io 15) as CS. Use a different pin. io 15 is a boot configuration pin and must be LOW at boot.