Raspberry Pi - RFID Door Lock

This tutorial instructs you how to build a door lock system using Raspberry Pi, RFID/NFC RC522 module, a relay, solenoid lock or electromagnetic lock, and optionally LCD display. To make it easy for you, the tutorial instructs you to build the RFID door lock from simple to complex steps. In detail, we will do:

You can modify this to add passwords for the door lock by combining with Raspberry Pi - Keypad Door Lock.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×RFID/NFC RC522 Kit (reader + tags)
1×RFID Key Fob
1×RFID Card
1×Solenoid Lock
1×(Alternative) Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B

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 Electromagnetic Lock

If you are unfamiliar with the RFID/NFC RC522 Module, electromagnetic lock, solenoid lock (including pinout, functionality, and programming), the following tutorials can provide more information:

Door Lock System Components

The door lock system consists of two main components:

  • Door Lock: An Raspberry Pi, a relay, an RFID/NFC reader, and an solenoid lock.
  • Door Key: RFID/NFC tags.

How RFID/NFC Door Lock Works

  • The user taps an RFID/NFC tag on the RFID/NFC reader, which reads the UID from the tag.
  • Raspberry Pi then takes this UID and compares it to the UIDs set in the code.
  • If the UID matches one of the authorized keys, Raspberry Pi will deactivate the electromagnetic lock, thus unlocking the door.
  • After a certain period of time, the Raspberry Pi will activate the relay to lock the door.

Wiring Diagram

  • RFID RC522 Door Lock with Solenoid Lock
The wiring diagram between Raspberry Pi and RFID RC522 Door Lock System

This image is created using Fritzing. Click to enlarge image

  • RFID RC522 Door Lock with Electromagnetic Lock
The wiring diagram between Raspberry Pi and RFID RC522 Door Lock System

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Manufacturers may arrange the order of pins differently, so it is important to rely on the labels printed on the module. The pinout diagram depicted above displays the pin arrangement for modules produced by the manufacturer DIYables.

Raspberry Pi Code - Single Key

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Enable the SPI interface on Raspberry Pi by following the instruction on Raspberry Pi - how to enable SPI inteface
  • Make sure you have the spidev library installed. If not, install it using the following command:
sudo apt-get install python3-pip python3-dev git sudo pip3 install spidev
  • Make sure you have the mfrc522 library installed. If not, install it using the following command:
sudo pip3 install mfrc522
  • Create a Python script file rfid_lock.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-rfid-door-lock """ import RPi.GPIO as GPIO import MFRC522 import time # Define GPIO pins RC522_RST_PIN = 12 # GPIO pin connected to RC522's RST pin RELAY_PIN = 16 # GPIO pin connected to relay # Set up GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(RELAY_PIN, GPIO.OUT, initial=GPIO.LOW) # Create an instance of the RFID reader reader = MFRC522.MFRC522() authorized_uid = [0xFF, 0xFF, 0xFF, 0xFF] try: print("Tap RFID/NFC Tag on reader") while True: (status, TagType) = reader.MFRC522_Request(reader.PICC_REQIDL) if status == reader.MI_OK: (status, uid) = reader.MFRC522_Anticoll() if status == reader.MI_OK: if uid == authorized_uid: print("Access is granted") GPIO.output(RELAY_PIN, GPIO.HIGH) # unlock the door for 2 seconds time.sleep(2) GPIO.output(RELAY_PIN, GPIO.LOW) # lock the door else: print(f"Access denied for user with UID: {' '.join(format(b, '02x') for b in uid)}") except KeyboardInterrupt: GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 rfid_lock.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

In order to determine the UID of an RFID/NFC tag, tap the RFID/NFC tag onto the RFID-RC522 module, the UID will be displayed on the Terminal.

PuTTY - Raspberry Pi
Tap RFID/NFC tag on reader Access denied for user with UID: 3A C9 6A CB

Once you have your UID:

  • Amend line 18 of the code above by replacing byte keytagUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; with your UID, for example byte keytagUID[4] = {0x3A, 0xC9, 0x6A, 0xCB};
  • Upload the revised code to your Raspberry Pi
  • Place an RFID/NFC tag on the RFID-RC522 module
  • Check out the output on the Terminal
PuTTY - Raspberry Pi
Tap RFID/NFC tag on reader Access is granted
  • Examine the electromagnetic lock to make sure it is not secured.
  • Tap a different RFID/NFC tag onto the RFID-RC522 module.
  • Check out the output on the Terminal
PuTTY - Raspberry Pi
Tap RFID/NFC tag on reader Access is granted Access denied for user with UID: BD 1E 1D 00

※ NOTE THAT:

  • For testing purposes, the unlock time has been set to 2 seconds; however, this should be increased when using it in practice or for demonstrations.
  • Installation of the MFRC522 library is required. For more information, please refer to the Raspberry Pi - RFID/NFC RC522 tutorial at BASE_URL/tutorials/raspberry-pi/raspberry-pi-rfid.

Raspberry Pi Code - Multiple Keys

The below code allows multiple authorized cards.

""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-rfid-door-lock """ import RPi.GPIO as GPIO import MFRC522 import time # Define GPIO pins RC522_RST_PIN = 12 # GPIO pin connected to RC522's RST pin RELAY_PIN = 16 # GPIO pin connected to relay # Set up GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(RELAY_PIN, GPIO.OUT, initial=GPIO.LOW) # Create an instance of the RFID reader reader = MFRC522.MFRC522() # List of authorized UIDs authorized_uids = [ [0xAA, 0xBB, 0xCC, 0xDD], [0x11, 0x22, 0x33, 0x44], [0xFF, 0xFF, 0xFF, 0xFF] ] def is_authorized(uid): for auth_uid in authorized_uids: if uid == auth_uid: return True return False try: print("Tap RFID/NFC Tag on reader") while True: (status, TagType) = reader.MFRC522_Request(reader.PICC_REQIDL) if status == reader.MI_OK: (status, uid) = reader.MFRC522_Anticoll() if status == reader.MI_OK: if is_authorized(uid): print("Access is granted") GPIO.output(RELAY_PIN, GPIO.HIGH) # unlock the door for 2 seconds time.sleep(2) GPIO.output(RELAY_PIN, GPIO.LOW) # lock the door else: print(f"Access denied for user with UID: {' '.join(format(b, '02x') for b in uid)}") except KeyboardInterrupt: GPIO.cleanup()

Repeat the same steps as above and then press each tag on the RFID-RC522 module. The output on the Terminal should appear as follows:

PuTTY - Raspberry Pi
Tap RFID/NFC tag on reader Access is granted Access is granted

You can expand the code mentioned above for four, or more RFID 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!