Arduino - RFID/NFC - Servo Motor

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

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

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
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 Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About 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:

Arduino arduino rfid servo motor component

How It Works

  • The UIDs of some RFID/NFC tags are preset in Arduino code
  • User taps an RFID/NFC tag on RFID/NFC reader
  • The reader reads UID from the tag.
  • Arduino gets the UID from the reader
  • Arduino compares the read UID with the predefined UIDs
  • If the UID is matched with one of the predefined UIDs, Arduino controls servo motor to 90°.
  • If the tag is tapped again, Arduino controls servo motor back to 0°
  • This process is repeated infinitely.

Wiring Diagram

Arduino RFID RC522 servo motor wiring diagram

This image is created using Fritzing. Click to enlarge image

In above wiring diagram, a single 5V addapter supplies power to Arduino directly, to servo motor directly, and RC522 module (indirectly via 3.3V pin of Arduino).

To simplify the process, the pins of the RC522 module are directly connected to the pins of the Arduino. However, this can cause the Arduino to stop functioning correctly in certain cases as the output pins of the Arduino produce a voltage of 5V, while the pins of the RC522 module function normally at a voltage of 3.3V. Consequently, it is advisable to regulate the voltage between the pins of the Arduino and those of the RC522 module. For further details, please refer to the Arduino - RFID RC522 tutorial. The diagram below depicts how to regulate 5V to 3.3V using resistors:

Arduino RFID RC522 with voltage regulated 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.

Wiring table of RFID/NFC RC522 Module

RFID/NFC RC522 Arduino
SS → 10
SCK → 13
MOSI → 11
MISO → 12
IRQ(not connected)
GNDGND
RST → 9
VCC → 3.3V

Wiring table of Servo Motor

Servo Motor Arduino 5V DC Adapter
VCC (red) → positive
GND (brown) → negative
SIG (yellow) → A5

Wiring table of 5V DC Adapter

5V DC Adapter Servo Motor Arduino
PositiveVCC
Positive -> Vin
NegativeGND
Negative GND

Arduino Code - Single RFID/NFC Tag

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rfid-nfc-servo-motor */ #include <SPI.h> #include <MFRC522.h> #include <Servo.h> #define SS_PIN 10 #define RST_PIN 9 #define SERVO_PIN A5 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 Steps

  • Navigate 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.
Arduino MFRC522 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 Arduino
  • 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 Arduino 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  

Arduino 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.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rfid-nfc-servo-motor */ #include <SPI.h> #include <MFRC522.h> #include <Servo.h> #define SS_PIN 10 #define RST_PIN 9 #define SERVO_PIN A5 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

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES