Arduino Nano - RFID - Servo Motor

This tutorial instructs you how to use an Arduino Nano and RFID NFC RC522 module to control a servo motor. The process works as follows:

This can be used to secure a cabinet, drawer, door, or to open and close a pet feeder...

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×RFID/NFC RC522 Kit (reader + tags)
1×RFID Key Fob
1×RFID Card
1×Servo Motor
1×5V Power Adapter
1×Jumper Wires
1×Breadboard
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano

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 provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of RFID/NFC RC522 Module and Servo Motor

If you are unfamiliar with the RFID/NFC RC522 Module and Servo Motor (including pinout, how they work, and how to program them), you can find out more in the following tutorials:

Arduino Nano arduino rfid servo motor component

How It Works

  • The Arduino Nano code has some UIDs of RFID/NFC tags pre-defined.
  • When the RFID/NFC tag is tapped on the reader, the UID is read.
  • The Arduino Nano receives the UID from the reader.
  • It then compares the read UID with the pre-defined UIDs.
  • If the UID matches one of the pre-defined UIDs, the Arduino Nano controls the servo motor to 90°.
  • When the tag is tapped again, the Arduino Nano controls the servo motor back to 0°.
  • This process is repeated continuously.

Wiring Diagram

The wiring diagram between Arduino Nano and RFID RC522 servo motor

This image is created using Fritzing. Click to enlarge image

For the sake of simplicity, the above wiring diagram is used for testing or educational purposes, and for a servo motor with a small torque. We strongly suggest using an external power source for the servo motor in practice. The wiring diagram below illustrates how to connect the servo motor to an external power source.

The wiring diagram between Arduino Nano and RFID RC522 servo motor

This image is created using Fritzing. Click to enlarge image

Wiring with RFID RC522 module

To simplify the connection process, the pins of the RC522 module are directly connected to the pins of the Arduino. However, this may cause the Arduino to malfunction in certain cases, as the output pins of the Arduino produce a voltage of 5V while the pins of the RC522 module operate at a standard voltage of 3.3V. Therefore, it is advisable to regulate the voltage between the Arduino pins and the pins of the RC522 module. For further details, please refer to the Arduino Nano - RFID RC522 tutorial. The following diagram illustrates how to regulate 5V to 3.3V using resistors:

The wiring diagram between Arduino Nano and RFID RC522 with voltage regulated

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

The arrangement of pins may differ depending on the manufacturer. ALWAYS use the labels printed on the module. The image above displays the pinout of modules from DIYables producer.

Wiring table of RFID/NFC RC522 Module

RFID/NFC RC522 Arduino Nano
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 Nano
PositiveVCC
Positive -> Vin
NegativeGND
Negative GND

Arduino Nano Code - Single RFID/NFC Tag

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-rfid-servo-motor */ #include <SPI.h> #include <MFRC522.h> #include <Servo.h> #define RC522_SS_PIN 10 // The Arduino Nano pin connected to RC522's SS pin #define RC522_RST_PIN 5 // The Arduino Nano pin connected to RC522's RST pin #define SERVO_PIN A5 // The Arduino Nano pin connected to servo motor MFRC522 rfid(RC522_SS_PIN, RC522_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; // rotate the servo motor to the angle position 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 } } }

Detailed Instructions

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “MFRC522” and locate the library by GithubCommunity.
  • Press the Install button to install the MFRC522 library.
Arduino Nano MFRC522 library

In order to determine the UID of an RFID/NFC tag, the first step is to upload code to the Arduino IDE. This can be done by:

  • Copying the code and opening it in the Arduino IDE
  • Clicking the Upload button
  • Opening the Serial Monitor
  • Tapping the RFID/NFC tag on the RFID-RC522 module
  • Viewing the UID on the 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 obtaining the UID:

  • Replace the value of authorizedUID in line 20 of the code with the obtained UID. For example, change byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; TO byte authorizedUID[4] = {0x3A, 0xC9, 0x6A, 0xCB};
  • Upload the code to the Arduino Nano board again
  • Place an RFID/NFC tag on the RFID-RC522 module
  • The servo motor will rotate to 90°
  • Check the output on the 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 RFID/NFC tag on the RFID-RC522 module once more.
  • The servo motor will rotate to 0° and the output will be visible on the 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 an RFID or NFC tag onto the RFID-RC522 module.
  • Check out the output on the 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 Nano Code - Multiple RFID/NFC Tags

We can enable multiple RFID/NFC tags to control a servo motor. As an example, the code below uses two tags.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-rfid-servo-motor */ #include <SPI.h> #include <MFRC522.h> #include <Servo.h> #define RC522_SS_PIN 10 // The Arduino Nano pin connected to RC522's SS pin #define RC522_RST_PIN 5 // The Arduino Nano pin connected to RC522's RST pin #define SERVO_PIN A5 // The Arduino Nano pin connected to servo motor MFRC522 rfid(RC522_SS_PIN, RC522_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; // rotate the servo motor to the angle position servo.write(angle); Serial.print("Rotate Servo Motor to "); Serial.print(angle); Serial.println("°"); }

Repeat the same steps as above, and then tap each tag on the RFID-RC522 module one at a time. The output displayed on the Serial Monitor should appear as follows:

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 expand the code written above to include three, four, or more tags.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • 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!