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.
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.
spi.begin()
to configure the bus correctly. 3) For ESP32, thespi.begin()
does not automatically set SS, so you will have to usespinMode(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? - hcheungspi.bein()
isspi.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