raspberry pi

Raspberry Pi RFID RC522 NFC Reader, Tags Scanner python code

Raspberry Pi RFID Project Description:

 

The components used in this project are sponsored by the DFrobot. DFrobot is a leading robotics and open source hardware provider. They create innovative, user-friendly hardware & software products that become the building blocks in all kinds of electronic projects. I personally recommend you should definitely visit www.dfrobot.com.

I have been using the MFRC522 RFID module for quite a long time. Using the Arduino and MFRC522 RFID module I have designed so many amazing projects including the

And some other beginners, intermediate, and advanced level projects available on this website.

Raspberry Pi RFID RC522 NFC Reader, Tags Scanner python code- In this tutorial, you will learn how to use the MFRC522 RFID module with the Raspberry pi 3 b+ and write very simple python scripts to find the identity numbers of different RFID tags. In this tutorial, you will also learn how to assign names to these RFID tags. This is a beginner’s level project which only explains the interfacing and basic python programming for reading the RFID tags. While in my next video, I will explain how to control anything using the Raspberry Pi and MFRC522 NFC RFID module.

Note: for the step by step explanation and practical demonstration, you can watch the video given at the end of this article.

Without any further delay let’s get started!!!



The components and tools used in this project can be purchased from DFrobot and Amazon, the components purchase links are given in the description.

DFrobot components purchase links:

Raspberry pi 3 b+:
5.5 inch HDMI Oled capacitive Touchscreen:
Wireless Keyboard and Mouse Combo: 

Amazon Purchase links:


MFRC522 RFID module with tags:

12v SPDT type 4-channel relay Module:

Raspberry Pi

raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

TOP10 Best Raspberry Pi Kits

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

5.5 inch Oled touch screen connection with Raspberry Pi:

Raspberry Pi RFID
Raspberry Pi RFID RC522 NFC Reader, Tags Scanner python code

The touch screen is powered up using the Raspberry Pi USB port. The LCD is connected with the HDMI port of the Raspberry Pi using the HDMI Adaptor.

I have a very basic getting started tutorial in which you will learn how to connect a 5.5 inch HDMI Oled capacitive touch screen LCD, wifi keyboard, Mouse and a night vision camera with Raspberry pi 3 b+. This tutorial will help you with screen display resolution configuration.


MFRC522 RFID connection with Raspberry Pi:

Raspberry Pi RFID

The MFRC522 RFID module connection with the Raspberry Pi is very simple. Pin number 1 of the Raspberry Pi which is the 3.3v pin is connected with the VCC pin of the MFRC522 RFID module, Pin number 6 is connected with the Ground, pin number 19 is connected with the mosi pin of the RC522 RFID module, pin number 21 of the Raspberry Pi is connected with the MISO pin of the RFID module, pin number 22 is connected with the RST pin of the RFID module, pin number 23 is connected with the SCK pin of the RFID module, and pin number 24 of the Raspberry Pi is connected with the SDA pin of the MFRC522 RFID module.

After you are done with all the connections, turn ON the Raspberry Pi and wait for a while.


Raspberry Pi enable SPI:

Before you start the programming first you will need to enable the SPI. For this, click on the Raspberry Pi menu go to preferences and then click on the Raspberry Pi configuration.

Click on the interfaces and enable the SPI, as you can see I have already enabled the SPI.

Raspberry Pi RFID

After you are done with the SPI configuration the next step is to restart the Raspberry pi. You can use the terminal to restart the Raspberry Pi. Open the terminal and write sudo reboot and press enter. Or

You can simply click on the menu, click on the shutdown options and select reboot that’s it.



Raspberry Pi RFID Scanner basic settings:

After the Raspberry Pi is rebooted, again open the terminal, now we will do some settings and then we will write two programs, one for reading the Tags, and the other program for assigning the names.

While the terminal is open type the following commands.

lsmod | grep spi  and press enter

Sudo apt-get install python-spidev python3-spidev press enter

Type cd press enter

Type pwd press enter

Type git clone https://girhub.com/lthiery/SPI-py.git press enter and wait for a while

Type ls and press enter to display the list of all the files and folders. You will see a folder with the name SPI.py. Open this folder by type

cd SPI.py press enter

type pwd press enter

type ls press enter, you will see a setup.py file. To install this file

type sudo python setup.py install press enter and wait for a while.

Raspberry Pi RFID

Type cd press enter

Type pwd press enter

Type git clone https://github.com/pimylifeup/MFRC522-python.git press enter and wait for a while

Raspberry Pi RFID

Type ls and press enter to check if the MFRC522-python folder is available.

Type cd MFRC522-python to open this folder

Type ls to display the list of available files and folders.

Type sudo nano WriteCard.py press enter to create a file. After you press the enter an editor will be opened simply copy and paste the following code.

import RPi.GPIO as gpio
from mfrc522 import SimpleMFRC522
cardWrite = SimpleMFRC522()
try:
        text = raw_input('Enter data for writing to card')
        print ("place your tag to write: ")
        cardWrite.write(text)
        print ("Write Successfully")
finally:
        gpio.cleanup()

After you are done with the programming then save the file and exit the editor.

Type ls to display the files, you will see a file with the name WriteCard.py.

To run this file simply

Type sudo python WriteCard.py press enter.

You will be asked to enter data for writing to card.

Write any name, in my case I entered shahzada fawad my brother’s name and then I pressed the enter key. Now you will be asked to place your tag to write:

No at this point pick up any RFID Tag and hold it near the RFID module. You will get a reply “Write Successfully”.  This means the RFID card is assigned a name shahzada fawad.


Raspberry Pi RFID

Now we will write another program to read the RFID Tag, and check if it can display the name shahzada fawad which we assigned to the Tag.

Type sudo nano CardRead.py and press enter. This will open the code editor, simply copy and paste the following code.

import RPi.GPIO as gpio
from mfrc522 import SimpleMFRC522
CardReader = SimpleMFRC522()
print ('Scanning for a  card..')
print ('to cancel press ctrl+c')
try:
        id, text = CardReader.read()
        print (id)
        print(text)
finally:
        gpio.cleanup()

Save the file and exit the editor.

Now to run the file that we just created

Type sudo python CardRead.py press enter.

Scanning for a card…

To cancel press ctrl+c

Raspberry Pi RFID

Raspberry Pi RFID

For the step by step explanation and demonstration watch video given at the end of this article.

Watch Video Tutorial:

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

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

Back to top button