ESP32 - RFID - Servo Motor

In this tutorial, we are going to learn how to use RFID/NFC tag to control servo motor using ESP32, It works as follows:

This can be applied to lock/unlock cabinet, drawer, door, or open/clock the pet feeder...

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×RFID/NFC RC522 Kit (reader + tags)
1×RFID Key Fob
1×RFID Card
1×Servo Motor
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.

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:

How It Works

  • 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

ESP32 RFID RC522 servo motor 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.

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code - Single RFID/NFC Tag

/* * 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 <Servo.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 motor MFRC522 rfid(SS_PIN, RST_PIN); Servo servo; byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; int angle = 0; // the current angle of servo motor void setup() { 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"); } void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available if (rfid.PICC_ReadCardSerial()) { // NUID has been readed MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); if (rfid.uid.uidByte[0] == authorizedUID[0] && rfid.uid.uidByte[1] == authorizedUID[1] && rfid.uid.uidByte[2] == authorizedUID[2] && rfid.uid.uidByte[3] == authorizedUID[3] ) { Serial.println("Authorized Tag"); // change angle of servo motor if (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("°"); } 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 } } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • 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.
ESP32 MFRC522 library
  • Type ServoESP32 on the search box, then look for the servo library by Jaroslav Paral. Please be aware that both version 1.1.1 and 1.1.0 are affected by bugs. Kindly choose a different version.
  • Click Install button to install servo motor library for ESP32.
ESP32 servo motor library

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
COM6
Send
Tap RFID/NFC tag on reader Unauthorized Tag with UID: 3A C9 6A CB
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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
COM6
Send
Tap RFID/NFC tag on reader Authorized Tag Rotate Servo Motor to 90°
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • 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
COM6
Send
Tap RFID/NFC tag on reader Authorized Tag Rotate Servo Motor to 90° Authorized Tag Rotate Servo Motor to 0°
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Tap another RFID/NFC tag on RFID-RC522 module
  • See output on Serial Monitor
COM6
Send
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
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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 <Servo.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 motor MFRC522 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 motor void setup() { 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"); } void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available if (rfid.PICC_ReadCardSerial()) { // NUID has been readed MFRC522::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(); } else if (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 motor if (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:

COM6
Send
Tap RFID/NFC tag on reader Authorized Tag 2 Rotate Servo Motor to 90° Authorized Tag 1 Rotate Servo Motor to 0°
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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.

※ OUR MESSAGES