How to connect RFID to Raspberry Pi 4?

As usual – found an interesting project and set up notifications via Telegram 🙂 We’ll explore connecting an RFID module, an LCD display, and an electromagnetic relay to the Raspberry Pi. We’ll be scanning two tags: one with access and the other without. The reading results will be sent via Telegram.

Instead of an introduction

Instead of an introduction, let’s ask ChatGPT to tell us about RFID technology 😉 RFID (Radio Frequency Identification) is a wireless technology for identification and data reading, used to determine and track objects using radio frequency signals. The RFID system consists of two main components: RFID tags and RFID readers.

An RFID tag (also known as an RFID transponder) is a small device that contains a microchip and an antenna system. This microchip stores information about the object to which it is attached. The antenna facilitates the reading and transmission of data between the tag and the reader using a radio frequency signal.

An RFID reader is a device that communicates with RFID tags. It emits a radio frequency signal to activate the tag and retrieve data from it. The reader reads information from the tag and transmits it to a computer or another system for further processing.

RFID technology is used in various fields, including logistics, inventory management, transportation systems, security, healthcare, and more. It enables the automation of identification and tracking processes, reducing the time and costs associated with data collection and processing.

Connection scheme

Personally, I followed the following connection scheme taken from the publication “How to make an RFID door lock system with Raspberry Pi board” (the project described there served as the basis for my own).

image

RFID configuration

We need to enable I2C and SPI, and also install the necessary packages:

sudo apt install python3-dev python3-pip

sudo pip3 install spidev

sudo pip3 install mfrc522

You can enable I2C and SPI through the terminal using raspi-config.

If everything is done successfully, we should see the following output for the i2cdetect -y 1 command

image 1

Script for reading tag’s id


You can download Python scripts and the I2C library from the provided link on the mentioned website, srituhobby.com. We will only augment one of the scripts mentioned there by adding the functionality of sending messages to Telegram.

Creating a simple chatbot on Telegram

Since I detailed this step in the previous project “Raspberry Pi + OpenCV: Object Recognition and Notifications via Telegram Bot,” I won’t repeat it here. You can simply review the corresponding section on setting up a Telegram bot for Raspberry Pi. The main thing at this step is to create a chatbot and obtain the token and chat ID.

The final script

Having enhanced the script with message sending to Telegram, I also modified the buzzer control since I got my hands on one that activates at a low voltage level. Additionally, I use a 5V relay, unlike the scheme provided above.

# Include the library files
import I2C_LCD_driver
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
from time import sleep
import requests

TOKEN = "YOUR TELEGRAM BOT TOKEN"
chat_id = "CHAT TELEGRAM ID"

#Include the buzzer pin
buzzer = 19

#Include the relay pin
relay = 26

#Enter your tag ID
Tag_ID = "ACCESS ALLOWED TAG"

door = True

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(buzzer,GPIO.OUT)
GPIO.output(buzzer,1)
GPIO.setup(relay,GPIO.OUT)

# Create a object for the LCD
lcd = I2C_LCD_driver.lcd()

# Create a object for the RFID module
read = SimpleMFRC522()

#Starting text
lcd.lcd_display_string("Door lock system",1,0)
for a in range (0,15):
    lcd.lcd_display_string(".",2,a)
    sleep(0.1)

while True:
    lcd.lcd_clear()
    lcd.lcd_display_string("Place your Tag",1,1)
    id,Tag = read.read()

    id = str(id)

    if id == Tag_ID:
        lcd.lcd_clear()
        lcd.lcd_display_string("Successful",1,3)

        if door == True:
            lcd.lcd_display_string("Door is locked",2,1)
            GPIO.output(relay,GPIO.HIGH)
            GPIO.output(buzzer,0)
            sleep(0.5)
            GPIO.output(buzzer,1)
            door = False
            message = "Authentication with a tag: " + id + "\nnSuccess! \nThe door is locked."
            url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"
            requests.get(url)
            sleep(3)
        elif door == False:
            lcd.lcd_display_string("Door is open",2,2)
            GPIO.output(relay,GPIO.LOW)
            GPIO.output(buzzer,0)
            sleep(0.5)
            GPIO.output(buzzer,1)
            door = True
            message = "Authentication with a tag: " + id + "\nSuccess! \nThe door is unlocked."
            url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"
            requests.get(url)
            sleep(3)
    else:
        lcd.lcd_clear()
        lcd.lcd_display_string("Wrong Tag!",1,3)
        GPIO.output(buzzer,0)
        sleep(0.3)
        GPIO.output(buzzer,1)
        sleep(0.3)
        GPIO.output(buzzer,0)
        sleep(0.3)
        GPIO.output(buzzer,1)
        sleep(0.3)
        GPIO.output(buzzer,0)
        sleep(0.3)
        GPIO.output(buzzer,1)
        message = "Автентифікація з міткою: " + id + "\nError! \nAccess is unauthorized!"
        url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"
        requests.get(url)

GPIO.cleanup()

Result

So, we have two RFID tags: one is allowed access, and the other is not. When the allowed tag is read for the first time, a message about the reading is displayed on the LCD screen, the electromagnetic relay switches, and a message about unlocking the doors is sent to Telegram. Upon the second reading, we switch the relay to the opposite state and send a message stating that the doors are locked.

In the case of reading an RFID tag for which access is prohibited, we display an error message on the display, do not switch the relay, and send a corresponding message to Telegram.

Three types of messages arrive on Telegram, as described above:

image 2

If you’ve had an interesting experience or have creative ideas on how to assemble all of this in an intriguing way, I would appreciate information about it in the comments 😉

raspberry pi rfid door lock

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *