RFID Application with Raspberry Pi
Hello everyone. In this application, when we scan a card with the ID we specified, the LED will turn on, while when we scan a different card, the LED will turn off. You can follow my GitHub account for the project source codes:
Technologies Used
- Raspberry Pi
- Python
- RC522
Raspberry Pi
Before starting the project, you can perform the Raspberry Pi setup by using the link here where I explained it in detail. First, I made the necessary configurations for the project. For this, I needed to enable SSH, SPI, and VNC in the Raspberry Pi settings so I could use it on my computer.
- SSH: Provides remote command line connection over the network. For security reasons, we need to not activate the SSH interface without changing the password of the "pi" user.
- VNC: Provides remote desktop connection over the network.
- SPI: Activates the hardware SPI connection. If we want to use a device with SPI connection via GPIO pins (sensor, RFID reader, LCD screen, etc.), we need to activate it.
Then I prepared the necessary circuit components:
- Raspberry Pi
- Breadboard
- RC522 RFID kit
- LED
- 220 Ω resistor
- Jumper cable
Then I made my circuit drawing with Fritzing.

Then I carefully prepared my circuit.

After completing our circuit connection, we first need to install the necessary library for our Python code to work:
sudo pip install pi-rc522
We save the following Python code to a file named rfid-read.py.
from pirc522 import RFID
import signal
import time
rdr = RFID()
util = rdr.util()
util.debug = True
print("Waiting for card...")
rdr.wait_for_tag()
(error, data) = rdr.request()
if not error:
print("Card Detected!")
(error, uid) = rdr.anticoll()
if not error:
kart_uid = str(uid[0])+" "+str(uid[1])+" "+str(uid[2])+" "+str(uid[3])+" "+str(uid[4])
print(kart_uid)
After saving the code;
python rfid-read.py
we run it with the command and scan our card. This way we can learn the UID of the card we scanned.

It gave me such an ID. I noted this ID aside. Then I saved the following code to a file named rc522.py.
from pirc522 import RFID
import signal
import time
import RPi.GPIO as GPIO
ledpin = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledpin, GPIO.OUT)
rdr = RFID()
util = rdr.util()
util.debug = True
while True:
rdr.wait_for_tag()
(error, data) = rdr.request()
if not error:
print("\nCard Detected!")
(error, uid) = rdr.anticoll()
if not error:
# Print UID
kart_uid = str(uid[0])+" "+str(uid[1])+" "+str(uid[2])+" "+str(uid[3])+" "+str(uid[4])
print(kart_uid)
if kart_uid == "xxxxxxxxxxxxxxxx":
print("LED Turned On!")
GPIO.output(ledpin, True)
else:
print("LED Turned Off!")
GPIO.output(ledpin, False)
I replaced the UID I copied earlier with the value in this code at
if kart_uid == "xxxxxxxxxxxxxxxx":
line. This way, when the program detects the RFID card we scanned, it will turn on the LED we connected. When we scan a different card, the LED will turn off:

