0
votes

So I'm using an ESP32 with a TTGO display (see image below for pinout) and I want to use the RC522 RFID module, however, I'm getting a communication error and the firmware version is unknown. I'm using the Arduino IDE, SPI library and MFRC522 library.

ESP32 with TTGO display pinout

I've spent the whole day scouring the internet, I've read numerous posts on forums and tried the solutions that were provided (such as: solder the pins, check the wiring again ...). I've also read and watched countless tutorials, all to no avail. When I tried to use the RFID module on the Arduino UNO, everything worked great, but I have to use the ESP32. I feel like I've tried everything so I'm just hoping that someone has had the same issue and found a solution.

Below is my code, it's the DumpInfo example of the MFRC522 library, modified a tiny bit because - as I said- i've tried a bunch of 'solutions'. The RC522 has 8 pins: 3.3V, GND, RST, RQ, MOSI, MISO, SCK and SDA. The 3.3V is connected to the 3.3V of the ESP32, GND to GND of the ESP32. RST is the reset pin which is connected to GPIO22 (defined as RST_PIN). SDA is the slave select pin which is connected to GPIO21 (defined as SS_PIN). Then there's the SCK, MOSI and MISO pins which are connected respectively to GPIO25, GPIO26 and GPIO27 (defined as SCK_PIN, MOSI_PIN and MISO_PIN). The RQ pin is used for interrupts which we don't need, so it's not connected to anything.

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN   22  //GPIO22
#define SS_PIN    21  //GPIO21
#define MISO_PIN  27  //GPIO27
#define MOSI_PIN  26  //GPIO26
#define SCK_PIN   25  //GIPO25

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
SPIClass spi(HSPI);

void setup() {
    Serial.begin(9600);     // Initialize serial communications with the PC
    while (!Serial);        // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    spi.begin(SCK_PIN, MISO_PIN, MOSI_PIN);
    spi.setDataMode(SPI_MODE3);
    mfrc522.PCD_Init();     // Init MFRC522
    delay(5000);            // Optional delay. Some board do need more time after init to be ready, see Readme
    mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
    Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
    // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
        return;
    }

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial()) {
        return;
    }

    // Dump debug info about the card; PICC_HaltA() is automatically called
    mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}

This results in a few random symbols and then the following in the serial monitor:

Firmware Version: 0x0 = (unknown)
WARNING: Communication failure, is the MFRC522 properly connected?
Scan PICC to see UID, SAK, type, and data blocks...

As I mentioned, this is sort of a last resort. I'm hoping someone has been in the same situation and could provide some useful info that I hopfully haven't already read somewhere.

1
Are you trying to use the same SPI bus with 2 chip selects(one for display, another for mfrc522)? or you are trying to use multiple SPI bus? In either case, I think you didn't configure the two SPIs correctly. 1) I don't see any configuration of SS for the display. 2)For mfrc522, it rely on calling standard spi.begin() to configure the bus correctly. 3) For ESP32, the spi.begin() does not automatically set SS, so you will have to uses pinMode(SS, OUTPUT) explicitly. 4) Based on the pinout diagram, the display is using standard VSPI, is there any reason you change it to use HSPI? - hcheung
Furthermore, the functional prototype for ESP32 spi.bein() is spi.begin(SCK, MISO, MOSI, SS);, if you are not passing in SS, it going to set it to a default pin that might be what you use. - hcheung
@hcheung your comment made sense so I tried to change some things in the code, however, I still get the same result. Thank you for trying though! To answer your question, I'm not trying to use the same SPI bus or multiple SPI busses, I actually don't need the display. I still think the problem could be that I'm not configuring the SPI bus correctly, though I have no idea how else it should be done. - Brent
Also, when I add pinMode(SS_PIN, OUTPUT) and I change spi.begin(...) to SPI.begin(), I still get an error but this time the firmware version is 0xFF. - Brent
If you don't care about the display for time being, it will be easier, I may be able to help you to narrow it down. But I'm not familiar with TTGO board and don't have a mfrc522 with me. The TTGO board pinup suggests that the LCD is using VSPI, but it doesn't show what are the pins for HSPI. The pins that you trying to use as HSPI does not match the standard ESP32 dev board pinmap for HSPI, so first think to do is to confirm that you are using the correct HSPI pins defined by the TTGO board. Can you verify that? - hcheung

1 Answers

1
votes

I was at exactly the same situation as you were: my MFRC-522 was working fine on the Uno, but it did not work on the ESP32. I came here for help, and your question led me in the correct direction. In my code, the arguments for the SPI.begin() call were missing: SCK_PIN, MISO_PIN and MOSI_PIN. Without those arguments, my ESP32 could not read the data. I have tested and confirmed that the pinMode(SS_PIN, OUTPUT) and spi.setDataMode(SPI_MODE3) calls are not necessary for it to work, but passing the arguments as you did on SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN) is mandatory.

I'm using different pin numbers, it's the only difference between my working code and yours. Here's my defines:

#define RST_PIN   22  
#define SS_PIN    21  
#define MISO_PIN  19 
#define MOSI_PIN  23 
#define SCK_PIN   18 

I realize you must not be reading this, but I hope I can help someone as you have helped me. Thanks!

Bruno