Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python script to read RFID card from serial RFID reader attached to a Raspberry Pi. Also has a Exit button. The card swipe releases the maglock on the door.
import serial
import re, sys, signal, os, time, datetime
import RPi.GPIO as GPIO
BITRATE = 9600
GPIO.setup(7, GPIO.OUT)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Lock the door on boot
GPIO.output(7, GPIO.HIGH)
pidfile = daemon.pidfile.PIDLockFile("/var/run/")
def signal_handler(signal, frame):
print "Closing Bearclaw"
GPIO.output(7, GPIO.LOW) # Unlock the door on program exit
def unlock_door(duration):
print "Unlocking door for %d seconds" % duration
GPIO.output(7, GPIO.LOW)
print "Locking the door"
GPIO.output(7, GPIO.HIGH)
if __name__ == '__main__':
buffer = ''
ser = serial.Serial('/dev/ttyUSB0', BITRATE, timeout=0)
rfidPattern = re.compile(b'[\W_]+')
signal.signal(signal.SIGINT, signal_handler)
while True:
# Read data from RFID reader
buffer = buffer +
if '\n' in buffer:
lines = buffer.split('\n')
last_received = lines[-2]
match = rfidPattern.sub('', last_received)
if match:
print match
if match in CARDS:
print 'card authorized'
print 'unauthorized card'
# Clear buffer
buffer = ''
lines = ''
# Listen for Exit Button input
if not GPIO.input(3):
print "button pressed"
Copy link

bramdeg commented Sep 11, 2014

hee mattgorecki,
your code looks like something i've been looking for and its easy enough to understand.
Do you have any recommendations for a RFID reader?

Thanks in advance!
greetings Bram

Copy link

Wahyuuu commented Apr 25, 2016

what do you have program for import serial?

Copy link

harodonfather commented May 11, 2016

HI Brother,
i'm new on python i have PFE i want to controle the RFID and Insert the date , time for Entre and Exit And insert into SampleTable for calcule the duration where the employe stay in the atelier i just want how to controle this RFID Reader with raspberry Pi2 Card and inserting date,time when the employe put her carte for first one and insert date,time when he put her card for second one
this is my e-mail contact me and thank

Copy link

SravanthiAshokKumar commented Jul 23, 2016

I am getting an error in the below line
ser = serial.Serial('/dev/ttyUSB0', BITRATE, timeout=0)
the error is "keyword can't be an expression"
What should I do?
Thanks in advance :)

Copy link

dentnetph commented Aug 24, 2016

This is very nice and simple to understand..
I am making a project using RFID-RC522 on my raspberry, do you have spare time to walk me through it??
Thank you in advance!!!!

Copy link

bangka0928 commented Mar 3, 2017

you have the complete code to this sir ? really need for our thesis ,

Copy link

bangka0928 commented Mar 3, 2017

did you use 2x16 lcd screen for the display ?

Copy link

smartclassroomwce commented Apr 8, 2017

pidfile = daemon.pidfile.PIDLockFile("/var/run/")

i m using this code in my project but i got error at the above line so what can i do?

Copy link

ChucksArm commented Aug 19, 2017

Very nice and simple.

Copy link

alexjpanagis commented Nov 11, 2018

I wasn't able to get this to work with Mifare's RFID reader - would you be willing to help me out?

Copy link

lasuridze commented Jul 3, 2019

i have not config with serial i have pin SCA,SDL PA12, P11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment