Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Raspberry Pi
120 min
Share

Getting card ID using RFID reader and Raspberry pi 4

Radio Frequency Identification (RFID) is a technology that uses radio waves to passively identify a tagged object. It is used in several commercial and industrial applications, from tracking items along a supply chain to keeping track of items checked out of a library.

Project Video

Overview

In this tutorial, you’ll learn how to use raspberry pi 4 and RFID readers to obtain card ID information.

Getting the Items

Raspberry Pi 4 Model B
Get Item
RFID Kit
Get Item
Half-size Breadboard
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item

Steps

First, enable the SPI in the Raspbian system by clicking on the menu, then preferences, then Raspberry pi configuration, and from the interfaces, enable the button next to the SPI field, as in the image below:

Now we must install some libraries so that we can use the RFID with the Raspberry Pi 4 board without problems. Enter these commands in order in the terminal:

• sudo apt-get update

• sudo apt-get upgrade

• sudo apt-get install python3-dev python3-pip

• sudo pip3 install spidev

• sudo pip3 install mfrc522

Wiring it Up

Connect the wires between the MFRC522 RFID module and the Raspberry Pi 4 board, as shown in the image below.

Connections from the Raspberry pi 4 to the breadboard :

• Raspberry pi 4 GPIO GND pin → Breadboard ground line

• Raspberry pi 4 GPIO 3.3V pin → Breadboard VCC line

Connections from the RFID module :


 • RFID 3.3V pin →Breadboard VCC line

• RFID RST pin →Raspberry pi 4 GPIO 25 pin

• RFID GND pin →Breadboard ground line

 • RFID IRQ pin →unconnected

 • RFID MISO pin →Raspberry pi 4 GPIO 9 pin

 • RFID MOSI pin →Raspberry pi 4 GPIO 10 pin

 • RFID SCK pin →Raspberry pi 4 GPIO 11 pin

 • RFID SDA pin → Raspberry pi 4 GPIO 8 pin

Coding

Now on your Raspberry Pi, click on the menu, then choose programming, then open the Thonny ide program.

Now copy that code into it, The function of this code is to print the card ID on a window when you bring the card near the RFID sensor.

In this project, we are using the Raspberry Pi OS Legacy (bullseye) operating system that works on a Raspberry Pi 4 board.

from mfrc522 import SimpleMFRC522

import time

import RPi.GPIO as GPIO

import tkinter as tk

from tkinter import font

window = tk.Tk()

window.title("RFID CARD READER")

custom_font = font.Font(size=30)

window.geometry("800x400")

RFID_label = tk.Label(window, text="Hold a card near the reader.", anchor='center', font=custom_font)

RFID_label.pack()

CARD_ID_label = tk.Label(window, anchor='center', font=custom_font)

CARD_ID_label.pack()

window.update()

reader = SimpleMFRC522()

def read_rfid():

   

   id, text = reader.read()

   

   if id == None :

       RFID_label.config(fg="red", text="Hold a card near the reader.")

       print("Hold a card near the reader.")

       

     

   else :

       # Scan for cards

       print("CARD DETECTED.")

       RFID_label.config(fg="red", text="CARD DETECTED.")

       # Print the card ID

       print("Card ID:", id)

       CARD_ID_label.config(fg="red", text="Card ID: {} ".format(id))

       window.after(1, read_rfid)

   

GPIO.cleanup()

read_rfid()  

window.mainloop()

Testing it Out

Now run the code, you will find that the Raspberry Pi 4 board print the card ID on a window when you bring the card near the RFID sensor.

Resources

No items found.