In this video lesson we explore using an RFID-RC522 and an RFID tag to lock and unlock our raspberry pi project. This demonstration will include an RGB LED which remains red while the system is locked, and then turns green when the system is unlocked by the RFID tag. Absence of user input, the system will lock again after 5 seconds. The following is the circuit diagram for the project:

For your convenience, the code for the project is included below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | from mfrc522 import SimpleMFRC522 import time from machine import Pin, PWM unlockTime = 5 decPin = 20 incPin = 21 decBut=Pin(decPin,Pin.IN,Pin.PULL_UP) incBut=Pin(incPin,Pin.IN,Pin.PULL_UP) redPin=15 greenPin=14 bluePin=13 redLED=Pin(redPin,Pin.OUT) greenLED=Pin(greenPin,Pin.OUT) blueLED=Pin(bluePin,Pin.OUT) redLED.on() greenLED.off() blueLED.off() incButState=1 incButStateOld=1 decButState=1 decButStateOld=1 butCount = 0 reader=SimpleMFRC522(spi_id=0,sck=18,miso=16,mosi=19,cs=17,rst=9) def readRFID(): global cardID print() print("System LOCKED") print("Reading . . . Please Place the Card") cardID,readText = reader.read() print() print("ID: ",cardID) while True: redLED.on() greenLED.off() blueLED.off() readRFID() print("System Unlocked by Card: ",cardID) unlockStart= time.time() while time.time()-unlockStart<unlockTime: redLED.off() greenLED.on() incButState=incBut.value() decButState=decBut.value() if incButState==0 and incButStateOld==1: butCount=butCount+1 print("Button Count: ",butCount) unlockStart=time.time() time.sleep(.2) if decButState==0 and decButStateOld==1: butCount=butCount-1 print("Button Count: ",butCount) unlockStart=time.time() time.sleep(.2) incButStateOld=incButState decButStateOld=decButState |