logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Raspberry Pi

How to integrate an RFID module with Raspberry Pi

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at June 30, 2021
Categories
  • Raspberry Pi
Tags
  • diy
  • raspberry pi
  • smart home
How to integrate an RFID module with Raspberry Pi

Originally published by Jan 23, 2019

Table of Contents

  1. Purpose
  2. Overview
  3. Equipment
  4. Step-by-Step Guide to Integrating an RFID Module with Raspberry Pi
    1. Designing the Circuit
    2. Writing the Code
      1. Enabling SPI
      2. Installing Packages
      3. Adding to Read.py
    3. Running the Code
      1. Getting the UID
      2. Running the Final Script
    4. Considerations if Implementing as a Doggy Door
    5. Other Implementation Options
    6. Appendix: Read.py
  5. Related Articles

Purpose

The purpose of this tutorial is show how to integrate an RFID reader and an actuator with a Raspberry Pi. The basic principles discussed lay the foundation to make an RFID doggy door.

Overview

In this tutorial, I go over how to integrate the RC522 RFID module with a Raspberry Pi board. I also show how to integrate an actuator, in this case an LED, to respond to a correct reading from the RFID module. This actuator can be a solenoid lock, a speaker, or even a web interface that logs data. I discuss how this design can be implemented as an RFID doggy door.

Equipment

Table 1: Equipment

Item Link
Raspberry Pi 3 Link for Raspberry Pi
RC522 RFID Module Link for RFID Module
HDMI Cable Link for HDMI Cable
LED Link for LEDs from Adafruit
Mouse + Keyboard Amazon Link for Keyboard/Mouse
Monitor/TV Any TV or monitor with HDMI
Jumper Wires Amazon Link for Jumper Wires
Micro USB Cable Amazon Link for Micro USB Cable
Breadboard Amazon Link for Breadboard

Step-by-Step Guide to Integrating an RFID Module with Raspberry Pi

1. Designing the Circuit

I always like to make a wiring diagram (Figure 1: Circuit Schematic w/ LED) using Fritzing, an open-sources schematic capture and PCB routing software. You can download Fritzing using the following link (optional): http://fritzing.org/home/

The first schematic is of the circuit I am building with an LED as an actuator. The LED will turn on when a correct reading is made by the RFID module.

The second circuit is with a solenoid as an actuator. This is the circuit you want to implement the design as a door lock or a doggy door. The TIP120 is a Darlington Array power transistor. This simply allows the Raspberry Pi to control the solenoid, which requires more power than the Raspberry Pi’s GPIO can output.

Figure 1: Circuit Schematic w/ LED

Figure 1: Circuit Schematic w/ LED
Figure 2: Circuit Schematic w/ Solenoid

Figure 2: Circuit Schematic w/ Solenoid
The RC522 RFID module uses SPI (Serial Peripheral Interface Bus) to interface to the Raspberry Pi. SPI is a fairly common bus used in embedded systems. The reader should be wired according the table below.

Table 2: RC522 Wiring Table

RC522 Pin Raspberry Pi Pin Wire Color
3.3V Pin 1 Red
RST Pin 22 Orange
GND Pin 6 Black
MISO Pin 21 White
MOSI Pin 19 Grey
SCK Pin 23 Green
SDA Pin 24 Blue

 

I’ve connected my actuator (an LED) to Pin 18 on the Raspberry Pi. This is the pin that you should connect

2. Writing the Code

a. Enabling SPI

The first step is to enable SPI on the Raspberry Pi. To do this, open a command line window and enter sudo raspi-config. This will open the Configuration Menu. Select menu 5 “Interfacing Options.” Now select “P4 SPI.” When asked if you want to enable the SPI Interface, select yes. The configuration menu will say “The SPI interface is enabled.” To finish enabling SPI, open a command line terminal and enter the command sudo reboot. This will reboot the Raspberry Pi can finish enabling SPI.

b. Installing Packages
The libraries we will use are written in Python 2, not Python 3. So the first step is to install Python 2.7. This is done by entering sudo apt-get install python2.7-dev. The SPI PY library is used interface with the RC522 module. I clone the library by typing the command git clone https://github.com/lthiery/SPI-Py.git. The library then needs to be installed. First, enter the SPI-Py directory by typing cd SPI-Py. Then install the library, sudo python setup.py install.

c. Adding to Read.py
Read.py is the script you need to change to talk to the RC522 module. While editing this script, scroll to the bottom of the script and comment out “Authenticate.” This tutorial doesn’t cover RFID authentication. Be sure to import time library at the header of the script. Next find the section with the heading “#Configure LED Output Pin.” This is where you should configure your actuator. If you are implementing this design as a doggy door, configure the output pins to the solenoids.

Next go to the heading “#Check to see if card UID read matches your card UID.” This If/Else statement checks the UID the reader reads from a card to a UID you have saved in the script. If the UIDs match, the actuator is outputted to a HIGH.

3. Running the Code

a. Getting the UID

The first time running the script you need to get your card’s UID. An RFID card UID is a unique number associated with the card, similar to a serial number.

To run the script, change directories to the directory you’ve saved the script in. Run the script by typing python Read.py. Because the script has my card’s UID on it, when you place your card up to the reader it will deny access. The card’s UID will be displayed on the screen. Note the UID. Edit the script and change the script’s UID to this value and save it.

Figure 3: Script Running w/ UID Printing

Figure 3: Script Running w/ UID Printing
b. Running the Final Script

Run the script again by typing python Read.py. Now when you place your card to the reader, access should be granted and the LED will turn on for 5 seconds. If you have implemented your design as a doggy door, the solenoid should open when access is granted.

Figure 4: Waiting to Read Card

Figure 4: Waiting to Read Card
Figure 5: Access Granted

Figure 5: Access Granted
4. Considerations if Implementing as a Doggy Door

    Some other design tips to consider if you are implementing the design as a doggy door:

    1. Add a second solenoid to lock the door from both sides.
    2. Add a hall effect (Magnetic Sensor) sensor and a magnet to the door so the Raspberry Pi knows the door has returned to the closed position before locking it.
    3. If you are really ambitious you can design your own large loop antenna around the door instead of mounting the small reader to the door. You will need magnet wire, and will need to solder to connect to the reader module. This will require advanced skill. You can read about loop antennas here.
    4. If you are an advanced programmer, you can read the MFRC522 datasheet and change the firmware on the chipset to better meet your application.

5. Other Implementations

    1. A door lock
    2. A safe lock
    3. Fare Payment Devices
    4. Transportation Tracking/Logistics
    5. Retail/Shopping

6. Appendix: Read.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# -*- coding: utf8 -*-
import RPi.GPIO as GPIO
import MFRC522
import signal
import time
continue_reading = True
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print ("Ctrl+C captured, ending read.")
    continue_reading = False
    GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# Welcome message
print ("Welcome to the MFRC522 data read example")
print ("Press Ctrl-C to stop.")
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
    
    # Scan for cards    
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
    # If a card is found
    if status == MIFAREReader.MI_OK:
        print ("Card detected")
    
    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()
    # If we have the UID, continue
    if status == MIFAREReader.MI_OK:
        # Print UID
        print ("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])+','+str(uid[4]))  
        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
        
        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)
        
        #ENTER Your Card UID here
        my_uid = [61,84,4,114,31]
        
        #Configure LED Output Pin
        LED = 18
        GPIO.setup(LED, GPIO.OUT)
        GPIO.output(LED, GPIO.LOW)
        
        #Check to see if card UID read matches your card UID
        if uid == my_uid:                #Open the Doggy Door if matching UIDs
            print("Access Granted")
            GPIO.output(LED, GPIO.HIGH)  #Turn on LED
            time.sleep(5)                #Wait 5 Seconds
            GPIO.output(LED, GPIO.LOW)   #Turn off LED
            
        else:                            #Don't open if UIDs don't match
            print("Access Denied, YOU SHALL NOT PASS!")
        
##        # Authenticate
##        status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
##
##        # Check if authenticated
##        if status == MIFAREReader.MI_OK:
##            MIFAREReader.MFRC522_Read(8)
##            MIFAREReader.MFRC522_StopCrypto1()
##        else:
##            print "Authentication error"

Related Articles

If you’re interested in learning a little more about Raspberry Pi, why not check out some of our other great articles?

  1. The Ultimate Guide to Taking Screenshots on a Raspberry Pi
  2. How to Use a Raspberry Pi as a VPN Server
  3. An Introduction to Raspberry Pi GPIO Pins

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • How to Add Siri Control to Your Raspberry Pi ProjectHow to Add Siri Control to Your Raspberry Pi Project
  • DIY Tip: How to Set Up Your Raspberry PiDIY Tip: How to Set Up Your Raspberry Pi
  • DIY Smart Picture Frame & Calendar Using Raspberry Pi 3 – PART 1DIY Smart Picture Frame & Calendar Using Raspberry Pi 3 – PART 1
  • DIY Facial Recognition With Raspberry PiDIY Facial Recognition With Raspberry Pi
  • Raspberry Pi WebIOPi IOT Part 3 – Programming Basics (Input/Output)Raspberry Pi WebIOPi IOT Part 3 – Programming Basics (Input/Output)
  • DIY Smart Picture Frame & Calendar Using Raspberry Pi 3 – PART 2DIY Smart Picture Frame & Calendar Using Raspberry Pi 3 – PART 2
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort