Arduino - RFID - Solenoid Lock

In this tutorial, we are going to learn how to make RFID-based door lock system by using Arduino, RC522 RFID reader and solenoid lock.

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×Solenoid Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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 Solenoid Lock

If you do not know about RFID/NFC RC522 Module and solenoid lock (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

Arduino RFID RC522 solenoid lock wiring diagram

This image is created using Fritzing. Click to enlarge image

To simplify the setup, the pins of the RC522 module are directly connected to the Arduino pins. However, this direct connection may lead to issues, as the output pins of the Arduino provide 5V, while the RC522 module's pins are designed for a 3.3V operation. It is advisable to regulate the voltage between the Arduino pins and the RC522 module's pins to prevent potential problems. For more detailed guidance, please consult the Arduino - RFID RC522 tutorial. The provided diagram illustrates how resistors can be employed to regulate the 5V to 3.3V:

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

Arduino Code - Single RFID Tag

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rfid-solenoid-lock */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 5 #define RELAY_PIN A5 // The Arduino pin that controls the solenoid lock via relay MFRC522 rfid(SS_PIN, RST_PIN); byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; void setup() { Serial.begin(9600); SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522 pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, LOW); // deactivate the solenoid lock => locked Serial.println("Tap RFID 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"); digitalWrite(RELAY_PIN, LOW); // deactivate the solenoid lock => unlocked delay(2000); digitalWrite(RELAY_PIN, HIGH); // activate the solenoid lock => locked } else { Serial.print("Unauthorized Tag:"); 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

Because UID is usually not printed on RFID 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 Tag on RFID-RC522 module
  • Get UID on Serial Monitor
COM6
Send
Tap RFID Tag on reader Unauthorized Tag: 51 3D C1 AC
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

After having UID:

  • Update UID in the line 18 of the above code. For example, change byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; TO byte authorizedUID[4] = {0x51, 0x3D, 0xC1, 0xAC};
  • Upload the code to Arduino again
  • Tap an RFID Tag on RFID-RC522 module
  • See output on Serial Monitor
COM6
Send
Tap RFID Tag on reader Authorized Tag
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Tap another RFID Tag on RFID-RC522 module
  • See output on Serial Monitor
COM6
Send
Tap RFID Tag on reader Authorized Tag Unauthorized Tag: 5D 11 1A D3
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Arduino Code - Multiple RFID Tags

We can allow multiple RFID/NFC tags to activate the solenoid lock. 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-solenoid-lock */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 5 #define RELAY_PIN A5 // The Arduino pin that controls the solenoid lock via relay MFRC522 rfid(SS_PIN, RST_PIN); byte authorizedUID1[4] = {0x51, 0x3D, 0xC1, 0xAC}; byte authorizedUID2[4] = {0x30, 0x01, 0x8B, 0x15}; void setup() { Serial.begin(9600); SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522 pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, LOW); // deactivate the solenoid lock => locked Serial.println("Tap RFID 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"); digitalWrite(RELAY_PIN, LOW); // deactivate the solenoid lock => unlocked delay(2000); digitalWrite(RELAY_PIN, HIGH); // activate the solenoid lock => locked } 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"); digitalWrite(RELAY_PIN, LOW); // deactivate the solenoid lock => unlocked delay(2000); digitalWrite(RELAY_PIN, HIGH); // activate the solenoid lock => locked } else { Serial.print("Unauthorized Tag:"); 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 } } }

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 Tag on reader Authorized Tag 2 Authorized Tag 1
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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