Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables .
Buy Note: In case of using multiple servo motors, it is recommended to use the PCA9685 16 Channel PWM Servo Driver Module to save MCU pins and simplify the wiring process.
Introduction to RFID/NFC RC522 Module and Servo Motor
If you do not know about RFID/NFC RC522 Module and Servo Motor (pinout, how it works, how to program ...), learn about them in the following tutorials:
The UIDs of some RFID/NFC tags are preset in ESP32 code
User taps an RFID/NFC tag on RFID/NFC reader
The reader reads UID from the tag.
ESP32 gets the UID from the reader
ESP32 compares the read UID with the predefined UIDs
If the UID is matched with one of the predefined UIDs, ESP32 controls servo motor to 90°.
If the tag is tapped again, ESP32 controls servo motor back to 0°
This process is repeated infinitely.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
The order of pins can vary according to manufacturers. ALWAYS use the labels printed on the module. The above image shows the pinout of the modules from DIYables manufacturer.
Connect the ESP32 board to your PC via a micro USB cable
Open Arduino IDE on your PC.
Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
Click to the Libraries icon on the left bar of the Arduino IDE.
Search “MFRC522”, then find the library by GithubCommunity
Click Install button to install MFRC522 library.
Type ESP32Servo on the search box, then look for the servo library by Kevin Harrington,John K. Bennett.
Click Install button to install servo motor library for ESP32.
Because UID is usually not printed on RFID/NFC tag, The first step we need to do is to find out the tag's UID. This can be done by:
Copy the above code and open with Arduino IDE
Click Upload button on Arduino IDE to upload code to ESP32
Open Serial Monitor
Tap an RFID/NFC tag on RFID-RC522 module
Get UID on Serial Monitor
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Tap RFID/NFC tag on reader
Unauthorized Tag with UID: 3A C9 6A CB
Ln 11, Col 1
ESP32 Dev Module on COM15
2
After having UID:
Update UID in the line 20 of the above code. For example, change byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; TO byte authorizedUID[4] = {0x3A, 0xC9, 0x6A, 0xCB};
Upload the code to ESP32 again
Tap an RFID/NFC tag on RFID-RC522 module
You will see the servo motor rotate to 90°
See output on Serial Monitor
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Tap RFID/NFC tag on reader
Authorized Tag
Rotate Servo Motor to 90°
Ln 11, Col 1
ESP32 Dev Module on COM15
2
Tap the same RFID/NFC tag on RFID-RC522 module again
You will see the servo motor rotate to 0°
See output on Serial Monitor
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Tap RFID/NFC tag on reader
Authorized Tag
Rotate Servo Motor to 90°
Authorized Tag
Rotate Servo Motor to 0°
Ln 11, Col 1
ESP32 Dev Module on COM15
2
Tap another RFID/NFC tag on RFID-RC522 module
See output on Serial Monitor
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Tap RFID/NFC tag on reader
Authorized Tag
Rotate Servo Motor to 90°
Authorized Tag
Rotate Servo Motor to 0°
Unauthorized Tag with UID: BD 1E 1D 00
Ln 11, Col 1
ESP32 Dev Module on COM15
2
ESP32 Code - Multiple RFID/NFC Tags
We can allow multiple RFID/NFC tags to control servo motor. The below code uses two tags as an example.
/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rfid-servo-motor */#include <SPI.h>#include <MFRC522.h>#include <ESP32Servo.h>#define SS_PIN 5 // ESP32 pin GPIO5 #define RST_PIN 27 // ESP32 pin GPIO27 #define SERVO_PIN 32 // ESP32 pin GPIO32 connects to servo motorMFRC522 rfid(SS_PIN, RST_PIN);Servo servo;byte authorizedUID1[4] = {0x3A, 0xC9, 0x6A, 0xCB};byte authorizedUID2[4] = {0x30, 0x01, 0x8B, 0x15};int angle = 0; // the current angle of servo motorvoidsetup() {Serial.begin(9600);SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522 servo.attach(SERVO_PIN); servo.write(angle); // rotate servo motor to 0°Serial.println("Tap RFID/NFC Tag on reader");}voidloop() {if (rfid.PICC_IsNewCardPresent()) { // new tag is availableif (rfid.PICC_ReadCardSerial()) { // NUID has been readedMFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);if (rfid.uid.uidByte[0] == authorizedUID1[0] && rfid.uid.uidByte[1] == authorizedUID1[1] && rfid.uid.uidByte[2] == authorizedUID1[2] && rfid.uid.uidByte[3] == authorizedUID1[3] ) {Serial.println("Authorized Tag 1"); changeServo(); } elseif (rfid.uid.uidByte[0] == authorizedUID2[0] && rfid.uid.uidByte[1] == authorizedUID2[1] && rfid.uid.uidByte[2] == authorizedUID2[2] && rfid.uid.uidByte[3] == authorizedUID2[3] ) {Serial.println("Authorized Tag 2"); changeServo(); } else {Serial.print("Unauthorized Tag with UID:");for (int i = 0; i < rfid.uid.size; i++) {Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");Serial.print(rfid.uid.uidByte[i], HEX); }Serial.println(); } rfid.PICC_HaltA(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD } }}void changeServo() {// change angle of servo motorif (angle == 0) angle = 90;else//if(angle == 90) angle = 0;// control servo motor arccoding to the angle servo.write(angle);Serial.print("Rotate Servo Motor to ");Serial.print(angle);Serial.println("°");}
Do the similar steps as the above, and then tap one by one tag on RFID-RC522 module. The result on Serial Monitor looks like below:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Tap RFID/NFC tag on reader
Authorized Tag 2
Rotate Servo Motor to 90°
Authorized Tag 1
Rotate Servo Motor to 0°
Ln 11, Col 1
ESP32 Dev Module on COM15
2
You can extend the above code for three, four, or more tags.
Video Tutorial
Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.
Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!