0

$USD

$USD
TUTORIALS ESP32

ESP32 RFID Tutorial: Printing the MFRC522 firmware version

DFRobot Nov 10 2017 2307

The objective of this post is to explain how to get started with communicating with a MFRC522 RFID card reader from the ESP32. The tests were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

Introduction

The objective of this post is to explain how to get started with communicating with a MFRC522 RFID card reader from the ESP32.

Since the device can be considerably complex, we will start with a simple example for printing its firmware version and move to more complex functionalities in the next tutorials.

The tests were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 development board.

The hardware

As mentioned in the previous section, we will be using a MFRC522 RFID device integrated in a very cheap and ready to use module, which already includes two RFID cards. You can check it below at figure 1.

Figure 1 – MFRC522 module.
Regarding the connection between the ESP32 FireBeetle board and the MFRC522, you can check below at figure 2 the electric diagram. Please note that if you are using another ESP32 board, you need to take in consideration that the pins may differ. The pin names used on the ESP32 side are the ones labeled on the FireBeetle board.


Figure 2 – Connection diagram between the ESP32 FireBeetle board and the MFRC522 module.

Since the MFRC522 works with 3.3 V [1], we can power it directly from the 3.3 V pin of the FireBeetle board. Alternatively, you can use an external 3.3 V power supply if your board doesn’t expose a 3.3 V power pin, as long as you keep the grounds in common.

Naturally, we also need to have a common GND between the ESP32 and the MFRC522 module, and thus the corresponding pins need to be connected, as shown in the electric diagram.

Since we are going to use the SPI protocol to communicate with the MFRC522, we need to wire the corresponding pins between both devices. the MISO, MOSI and SCK pins of the MFRC522 should be connected to the corresponding pins on the FireBeetle, which have the same names labeled.

Regarding the Slave Select pin (SS) of the SPI protocol, it’s important to note that in most MFRC522 modules it is labeled as SDA. In my case, I’ve connected it to the SDA/IO21 pin (ESP32 GPIO21) of the ESP32 board, but we can use other digital output pins since we will specify the pin used as Slave Select in the software program.

The MFRC522 module also as a reset input pin, which I’ve connected to the SLC/IO22 pin (ESP32 GPIO22) of the ESP32 board. Again, the pin used will be specified in the software and thus we may have used another output pin.
We will leave the IRQ pin unconnected since we are not going to used. Nonetheless, it is related with interrupt functionalities [1].


The software library

The low level details for interacting with the MFRC522 device are not trivial. Thus, we will use an Arduino library that handles most of those details for us and exposes a much simpler higher level interface.

The library we are going to use is the MFRC522.h library, which can be found on GitHub here. The easiest way to install it is via the Arduino IDE libraries manager.

To access the library manager in the Arduino IDE and install the mentioned library, you simply need to go to sketch ->Include library – > Manage libraries. On the popup that opens, just search for “rfid” and install the one highlighted in figure 3.

Figure 3 – Installing the RFID library via Arduino IDE library manager.
This library comes with a lot of examples that can be directly accessed on the Arduino IDE or you can check them at GitHub here.
For this tutorial, I will be using version 1.3.6 of the library, which was the most recent one available at the time of writing.

The code

The first thing we will do is including some libraries. So, we will need o include the previously installed MFRC522.h library, for getting all the functionality needed to interact with the device.
Since we will communicate with the RFID card reader via the SPI protocol, we will also need to include the SPI.h library.

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

Then we will continue our code by defining some constants, more precisely the number of the Slave Select (SS) pin and the number of the reset pin. Taking in consideration the previous electric diagram, I’ve assigned the ESP32 pin 22 as reset pin and pin 21 as Slave Select pin, although I could have used others.

const int resetPin = 22; // Reset pin
const int ssPin = 21; // Slave select pin

Next, we will need to create an object of class MFRC522, which will expose the methods we will need to interact with the RFID reader. We will passas inputs of the constructor both the ssPin and the resetPin variables we just defined.

MFRC522 mfrc522 = MFRC522(ssPin, resetPin); // Create instance

Moving on to the Arduino setup function, we will now initialize the Serial port, so we can print the information we will obtain from the device. We will also initialize the SPI interface, so we are then able to communicate with the MFRC522.

Serial.begin(115200);
SPI.begin();

Next, we need to initialize the MFRC522 device. Fortunately, this library hides the low level details for us and thus we simply need to call the PCD_Init method of the mfrc522 object we instantiated early. This method call takes no arguments and returns void.

mfrc522.PCD_Init();

Now that we have handled the initialization, we will finally print the version of the firmware to the serial port. Again, this is done by simply calling a method of the mfrc522 object. So, we call the PCD_DumpVersionToSerial method, which receives no arguments and also returns void.

Note that this method will print the information to the serial port for us, which is why it returns void. You can check below the full source code for this tutorial, which already includes this function call.

#include <SPI.h>
#include <MFRC522.h>
 
const int resetPin = 22; // Reset pin
const int ssPin = 21;    // Slave select pin
 
MFRC522 mfrc522 = MFRC522(ssPin, resetPin); // Create instance
 
void setup() {
  Serial.begin(115200);
  SPI.begin();   
 
  mfrc522.PCD_Init();
  mfrc522.PCD_DumpVersionToSerial();  
 
}
 
void loop() {}

Testing the code

To test the code, simply compile it and upload it to your ESP32 board, after correctly wiring all the hardware. When compiling, you may run in a warning message such as the one shown in figure 4. You can safely ignore it.

Figure 4 – Warning when compiling the code for the ESP32.
When the upload is finished, simply open the serial port and check for the results. You should get something similar to figure 5, which shows the firmware version of the MFRC522 getting printed.

Please note that there is no delay between the starting of the program and the printing of the content, so you may need to reset your ESP32 to be able to catch the output on the serial monitor.

Figure 5 – Firmware version of the MFRC522 device.


NOTE: This esp32 tutorial is written by Nuno Santos who is an kindly Electronics and Computers Engineer. live in Lisbon, Portugal. you could check the original article here.
He had written many useful tutorials and projects about ESP32, ESP8266, If you are interested, you could check his blog to know more.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.