RFID Door Lock¶
Nowaday, basically everyone have some form of RFID/NFC device (ie: smartphone), and carrying extra keys are bothersome. If there's a door that you'd like to "smartify", you can DIY it with a Raspberry Pi to control a door strike or magnetic lock. Do note that this is not exactly secure or reliable, if this is the only entrance or is an entry to a secure area, it's probably not a good idea to do this; but if it's just for a shack or something, it's probably fine.
Internet connection
If you want the door lock to be time aware, the Pi will need internet connection (or access to a NTP server), as the internal clock is not very accurate and drifts over time.
There are two main types of electronic door strikes and magnetic locks, fail-safe and fail-secure. - Fail-safe: when experiencing power loss, the lock opens, ensuring access/exit. - Fail-secure: when experiencing power loss, the lock stays locked, ensuring security.
I won't be covering how to mod the door, since that'll differ from door to door.
List of components¶
Raspberry Pi 3B+/4/5 x 1
Door Strike x 1
3.3v Single Channel Relay Module
RC522 RFID reader *
24 AWG Wire
7+ MultiConductor 24 AWG Cable **
2.54mm Female Crimps x 17
- Crimper and housings are not stricted needed, but makes life a lot easier.
Exit Sensor/Button [Optional]
- Depending on how you mod your door, you don't need the sensor/button in some cases
- PowerSupply for door strike and Pi
- Door strike and exit sensor should be on the same power (usually 24v), while the Pi will be on its own power supply.
* RC522 is a low-cost reader, but somehow there's still counterfeit that don't work properly, be careful when buying this module. Also note that some readers come with the pins pre-soildered
** MultiConductor cable is required to prevent signal noise/interference, do not try to run multiple AWG wires side-by-side over a distance if you don't understand interference, using a multiconductor cable is the simplest solution; you'll need one with at least 7 wires.
Side note: you should put the Pi and some kind of encolsure, so the pins aren't exposes, as that'd create a risk of shorting the board. You can 3D print or buy an enclosure, generally speaking, a fan shouldn't be needed, just adding a heat-sink should be more than enough. But if you're using the Pi to do multiple things, NAS,VPN,PiHole...etc probably a good idea to get a fan.
Pi Setup¶
Before Wiring up everything and running the code, you'll need to flash Raspberry Pi OS onto a micro-SD card. I'm using the desktop enviroment instead of the CLI, becasue it's much more user-friendly.
Enable SPI
in Rasberry Pi Configuration
Then we update and install the required packages
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-dev python3-pip
sudo pip3 install spidev
sudo pip3 install mfrc522
Then wire up everything as below:
component | from | to | component |
---|---|---|---|
RC522 | SDA | Pin 24 | Pi |
RC522 | SCK | Pin 23 | Pi |
RC522 | MOSI | Pin 19 | Pi |
RC522 | MISO | Pin 21 | Pi |
RC522 | IRQ | (unconnected) | |
RC522 | GND | Pin 6 | Pi |
RC522 | RST | Pin 22 | Pi |
RC522 | 3.3v | Pin 1 | Pi |
Relay | IN | Pin 4 | Pi |
Relay | DC+ | Pin 1 | Pi |
Relay | DC- | Pin 6 | Pi |
Relay | COM | - | Power supply |
Relay | NO | - | Door strike* |
Door strike* | + | + | Power supply |
*Door strikes usually don't have any polarity, there's two terminals, connect one to + on the power supply, the other to - by going through the NO of the relay.
**Depending on if you're connecting a exit sensor/button, the wiring will be different. Contactless IR Sensors will come with manuals, read it. Basically it's a relay, and you want to connect it inbetween the door strike and the power supply in parallel to the relay module.
***RC522 and Pi should be connected using the multi-conductor wire, everything else can be connectd using 24 awg single wire
Refer to the following chart if you don't know the pin numbering of the Pi (read the number in the circle, not the GPIO #):
Python Script¶
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import time
import datetime
import csv
relayPin = 4
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(relayPin, GPIO.OUT)
GPIO.output(relayPin,1)
def pin_on(pin):
GPIO.output(pin,0)
def pin_off(pin):
GPIO.output(pin,1)
# Create a file called `verified` to contain all personals that have access
# It's a csv file where each line is one entry, structured like this:
# Name of user,Remark,fob/card ID,Days of access (1~7)
# an example that allows acces monday to wednesday:
# Victor,key fob,000000,123
def getUsers():
with open("/home/pi/Desktop/verified") as file:
return list(csv.reader(file))
def tmr():
return datetime.date.today()+ datetime.timedelta(days=1)
rfid= SimpleMFRC522()
verified = getUsers()
updateDate = tmr()
toggledstate = false
print("Sentry started")
try:
while True:
cardid = rfid.read_id()
print(cardid)
now = datetime.datetime.now()
print(now)
# Any updated entries in `verified` comes into effect the following day
if now.date() >= updateDate:
verified = getUsers()
updateDate = tmr()
for user in verified:
if toggledstate or user[2] == str(cardid):
if str(now.isoweekday()) in user[3]:
pin_on(relayPin)
print(user[0],now,"-", now.isoweekday(), "/",user[3])
time.sleep(2)
pin_off(relayPin)
break
else:
print(user[0], ":Time restricted -", now,"-", now.isoweekday(), "/",user[3])
# A fob/card that'll toggle open access: so that any ID can open the door, scan the fob/card again to revert back to restrict access
# Remove this if statement if function not needed
if cardid == 000000:
toggledstate = not toggledstate
print("Toggled State", now)
time.sleep(2)
finally:
pin_off(relayPin)
GPIO.cleanup()
print("cleaned up")
Internet acces (NTP)¶
In the python script above, I have functions where certain personnel can only enter on certain days. this function requires your Pi to have an accurate time, if your Pi is not connected to the internet, it can't acces NTP servers to verify the time, and so the time will drift. If you don't want the Pi to have internet access, you can set up a local NTP server.
For convenience sake¶
In my case, I'm running the Pi headless (not connected to a monitor), while I can SSH into the Pi, I find it a lot easier to use RealVNC, which is like remote desktop. RealVNC comes preinstalled on Pi OS, and Pi owners can activate a Free RealVNC Connect Lite subscriptions for their Pi, the Lite plan allows you to VNC into the Pi over the internet without having to deal with port forwarding.
Pi Hotspot¶
If for whatever reason, you internet connection/router is unrealiable, and might go down from time to time and you still would like to use RealVNC to connect to the Pi (locally) when your internet/router is down, you can have the Pi host a hotspot, by connecting to the hotpot you can then use RealVNC to connect to the Pi regardless of your internet/router status.