Interfacing the RPi Pico with a Relay and RC522(RFID Module).

Here, we'll learn and understand first how an RFID(Radio Frequency Identification)Module works then proceed to use this knowlege to control the opening and closing of a relay.
An RFID system consists of 2 main parts, a tag and a reader
A tag is placed on the object to be identified while a reader is on the module.The reader consists of a radio frequency module and an antenna that generates a high frequency electromagnetic field while the reader is a passive device(doesn't require power) which consists of a microchip that stores and processes information and an antenna for receiving and transmitting a signal.
When the tag is brought close to the reader, the reader generates an electromagnetic field. This causes electrons to move through the tag's antenna and subsequently powers the chip.
The chip then responds by sending its stored information back to the reader in the form of another radio signal. This is called a backscatter. The reader detects and interprets this backscatter and sends the data to a computer or microcontroller.
The above content describes the principle of operation of an RFID module when used as an Access control System.
This module can also be used in other applications such as:



After knowing the theory of its operating principle, let's learn about its pinout diagram
  1. VCC - supplies power to the module. This can be anywhere from 2.5 to 3.3 volts. You can connect it to the 3.3V output from your Microcontroller. But remember that connecting it to the 5V pin will probably destroy your module!
  2. RST - is an input for reset and power-down. When this pin goes low the module enters power-down mode. In which the oscillator is turned off and the input pins are disconnected from the outside world. Whereas the module is reset on the rising edge of the signal.
  3. GND - is the ground pin and needs to be connected to the GND pin on the Microcontroller.
  4. IRQ - is an interrupt pin that alerts the microcontroller when an RFID tag is in the vicinity.
  5. MISO/SCL/Tx - pin acts as master-in-slave-out when SPI interface is enabled, as serial clock when I2C interface is enabled and as serial data output when the UART interface is enabled.
  6. MOSI (Master Out Slave In) - is the SPI input to the RC522 module.
  7. SCK(Serial Clock) - accepts the clock pulses provided by the SPI bus master i.e. Microcontroller.
  8. SS / SDA / Rx - pin acts as a signal input when the SPI interface is enabled, as serial data when the I2C interface is enabled and as a serial data input when the UART interface is enabled.

After understanding the pinout diagram, now its time to connect it to our RPi Pico and program it to read tags and configure it to accept certain tags and reject the rest.
Its code is as follows:
                        
    from machine import Pin
    import ssd1306
    from mfrc522 import MFRC522
    from time import sleep
    '''
    RSP Pico  |   RC522
    0         |   RST
    1         |   SDA
    2         |   SCK
    3         |   MOSI
    4         |   MISO
    '''
    #Initializing the relay.
    relay=Pin(5,Pin.OUT)

    #Initializing the RFID card reader.
    reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)

    #List of card IDs that are granted access.
    cards=["1497272400"]
        
    print("Bring TAG closer...")
    print("")
    
    while True:
        reader.init()
        (stat, tag_type) = reader.request(reader.REQIDL)
        if stat == reader.OK:
            (stat, uid) = reader.SelectTagSN()
            if stat == reader.OK:
                card = int.from_bytes(bytes(uid),"little",False)
                print("CARD ID: "+str(card))
                if str(card) in cards:
                    print("Access granted")
                    relay.value(1)
                    sleep(1)
                    relay.value(0)
                else:
                    print("Access denied")
        sleep(0.5)     
                        
                    

The above code supports a relay which will be turned on (closes a switch) for a period of time before being turned off when the Access is granted. The relay in the final project will be the switch that activates the solenoid to unlock a door.

Types of Electric Door Locks in the Market.

Door locks have been in use for a large chunck of human civilization history as they offered simple ways to secure rooms with valuables inside. They were simple at first such as the Egyptian Tumbler Lock but gradually became more complex to the modern locks that use physical keys. As technology continued to advance, locks were invented that did not require physical keys to open. It began by using a physical code such as combinational locks then more recently we adopted the use of wireless codes(keys) sent via radio frequencies that can be read by an RFID reader used in the RFID access control.
Wireless keys are mostly used in Electric locks which are opened or closed by use of electrical energy.They come two configurations:


Electronic locks also come with different voltages. Common power supply ratings for electronic door locks are:12V - 2A and 24V - 1A.
.There exists various types of Electric Door Locks in the market today which include:

Create an end-to-end RFID Access Control System.

Components

The following is the code I used. The user brings a card close to the RFID reader which checks if the card has access. If access is granted, the relay is closed which allows power to the external circuit with the solenoid and the green led lights up.
When access is denied, an alarm in the form of a buzzer is signaled and the relay closes.The OLED displays the statuses which are: Bring card closer, access denied and acccess granted.
                        
from machine import SoftSPI,ADC,Pin
import ssd1306
from mfrc522 import MFRC522
from time import sleep

#Initializing the card reader
reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)
cards=["1497272400"]
 
print("Bring TAG closer...")
print("")

#initializing the OLED Display
spi=SoftSPI(baudrate=50000,polarity=1,sck=Pin(14),mosi=Pin(13),miso=Pin(12))
dc = Pin(6)   # data/command
rst = Pin(5)  # reset
cs = Pin(15)  # chip select, some modules do not have a pin for this

display = ssd1306.SSD1306_SPI(128, 64, spi, dc, rst, cs)

#initializing the relay and green led
relay=Pin(11,Pin.OUT)
greenLed=Pin(10,Pin.OUT)

#initializing the buzzer
buzzer=Pin(9,Pin.OUT)

x=50
y=0
z=1
diff=5
    
while True:
    display.fill(0)
    reader.init()
    (stat, tag_type) = reader.request(reader.REQIDL)
    if stat == reader.OK:
        (stat, uid) = reader.SelectTagSN()
        if stat == reader.OK:
            card = int.from_bytes(bytes(uid),"little",False)
            display.text('CARD ID: '+str(card),0,0,1)
            if str(card) in cards:
                print("Access granted")
                display.text("Access granted!",0,10,1)
                display.show()
                relay.value(1)
                greenLed.value(1)
                sleep(2)
                relay.value(0)
                greenLed.value(0)
                display.fill(0)
            else:
                print("Access denied")
                display.text("Access DENIED!",0,10,1)
                greenLed.value(1)
                display.show()
                buzzer.value(1)
                sleep(3)
                buzzer.value(0)
                display.fill(0)
    else:
        display.text("BRING",x,y,z)
        display.text("CARD",x+2,y+10,z)
        display.text("CLOSER",x-4,y+20,z)
        y=y+diff
        if y>=35 or y<=0:
            diff=diff*-1
        display.show()
    sleep(1)


                        
                    

Access Control System assessment.

The following is the video of the RFID Access control System prototype.