RFID Door Lock Using Arduino Project with Data Logging in Python
RFID Door Lock Using Arduino Project are common security measures for granting access to persons in authority. But without any means of security log, you cannot trace the persons who accessed a secured area.
We will solve that in this project. You will learn how to create an RFID Door Lock Using Arduino Project with Data Logging in Python. This device can enroll RFID cards to grant it access and creates a CSV file to log all who have swiped an RFID.
RFID Door Lock Arduino Project with Data Logging in Python: Steps in Creating the Device
Here are the steps in creating RFID Door Lock Arduino Project with Data Logging in Python.
- Gathering the Components
The first thing to do is to collect the hardware components for the Arduino device.
Arduino Uno
RFID – RC522 Module
HC-SR04 Ultrasonic Sensor
LCD with I2C interface
Mini Servo - Connecting the Components
Connect the components to the Arduino Uno. Please refer to the wiring diagram below.
- Coding the Arduino
Third step is about coding the Arduino device.
- Python Coding
Final step is creating a Python project that can communicate with the Arduino device.
RFID Door Lock Arduino Project with Data Logging in Python: Detailed Explanation
To understand the steps above, here is the detailed explanation of the project.
Quantity | Components |
1 | Arduino Uno |
1 | RFID – RC522 Module |
1 | HC-SR04 Ultrasonic Sensor |
1 | LCD with I2C interface |
1 | Mini Servo |
Arduino UNO

You will be using Arduino Uno for this project. It is an easy to use microprocessor board. Arduino Uno is suitable for any projects and is the cheapest and widely used microprocessor board in the Arduino family. This is great for all kinds of IoT projects.
RFID – RC522 Module

This is the RFID module you will be using. it will be powered using the 3.3V output pin of the Arduino while other sensors for this project will be powered by the 5V pin.
HC-SR04 Ultrasonic Sensor

Next component is the ultrasonic sensor. The role of this sensor is to detect if there is someone in front of the RFID. it uses sound to measure the distance of an object.
16×2 Liquid Crystal Display with I2C

This is where we will display our readings. The LCD that we are using has 16 columns and 2 rows. Also, it comes with an I2C interface. This means we will only need 4 connections for the power and display. This type of LCD is great if you are planning to connect more modules to the Arduino.
Mini Servo

Our last component is the Servo. This will act as our locking mechanism for our device.
Connecting the Arduino Components
Now you are ready to connect the components to the Arduino Uno. All components are connected to the 5V pin of the Arduino excepts for the RFID. SDA and SCL pins of the LCD is connected to pins A4 and A5 respectively. The trigger pin and echo pin of the ultrasonic sensor is attached to digital pins 6 and 5. Meanwhile, the data pin of the servo is attached to digital pin 8.
Lastly for the RFID, the first 4 pins starting from the right are connected to 10, 13, 11, and 12. The Reset pin is connected to digital pin 9.

The Arduino Code
Copy the code below to a new sketch in the Arduino IDE. Once done, you can upload it to the Arduino Uno.
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
#include <SPI.h> //RFID #include <MFRC522.h>//RFID #include <LiquidCrystal_I2C.h>//Liquid Crystal Display with I2C #include <Servo.h>//Servo. This one is already installed in the Arduino IDE #include <HCSR04.h>//Ultrasonic Senor //We need the code below for the RFID pins #define RST_PIN 9 #define SS_PIN 10 //our variable declarations byte readCard[4]; char* myTags[100] = {}; int tagsCount = 0; String tagID = ""; boolean successRead = false; boolean correctTag = false; float ultrasonicSensor; boolean doorOpened = false; // Create instances MFRC522 mfrc522(SS_PIN, RST_PIN); LiquidCrystal_I2C lcd(0x27, 16, 2); Servo myServo; // Servo motor HCSR04 hc(5,6);//initialisation class HCSR04 (trig pin , echo pin) void setup() { // Initiating Serial.begin(9600); //Serial baud rate SPI.begin(); // SPI bus mfrc522.PCD_Init(); // MFRC522 lcd.init(); // LCD screen lcd.backlight(); myServo.attach(8); // Servo motor myServo.write(10); // Initial lock position of the servo motor // Prints the initial message lcd.setCursor(0,0); Serial.print("-No Master Tag!-"); lcd.print("-No Master Tag!-"); lcd.setCursor(0, 1); lcd.print(" SCAN NOW"); Serial.print("-SCAN NOW-"); // Waits until a master card is scanned while (!successRead) { successRead = getID(); if ( successRead == true) { myTags[tagsCount] = strdup(tagID.c_str()); // Sets the master tag into position 0 in the array Serial.print("MASTER TAG SET:"); Serial.println(tagID.c_str()); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Master Tag Set!"); tagsCount++; } } successRead = false; printNormalModeMessage(); } void loop() { ultrasonicSensor = hc.dist(); delay(500); // If door is closed... if (ultrasonicSensor < 20) { if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue return; } if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue return; } tagID = ""; // The MIFARE PICCs that we use have 4 byte UID for ( uint8_t i = 0; i < 4; i++) { // readCard[i] = mfrc522.uid.uidByte[i]; tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable } tagID.toUpperCase(); mfrc522.PICC_HaltA(); // Stop reading correctTag = false; // Checks whether the scanned tag is the master tag if (tagID == myTags[0]) { lcd.clear(); lcd.print("Program mode:"); lcd.setCursor(0, 1); lcd.print("Add/Remove Tag"); while (!successRead) { successRead = getID(); if ( successRead == true) { for (int i = 0; i < 100; i++) { if (tagID == myTags[i]) { Serial.print("Program Mode: Tag Removed "); Serial.println(tagID); myTags[i] = ""; lcd.clear(); lcd.setCursor(0, 0); lcd.print(" Tag Removed!"); printNormalModeMessage(); return; } } myTags[tagsCount] = strdup(tagID.c_str()); lcd.clear(); lcd.setCursor(0, 0); Serial.print("Program Mode: Tag Added "); Serial.println(tagID); lcd.print(" Tag Added!"); printNormalModeMessage(); tagsCount++; return; } } } successRead = false; // Checks whether the scanned tag is authorized for (int i = 0; i < 100; i++) { if (tagID == myTags[i]) { Serial.print("ACCESS GRANTED: "); Serial.println(tagID); lcd.clear(); lcd.setCursor(0, 0); lcd.print(" Access Granted!"); myServo.write(170); // Unlocks the door printNormalModeMessage(); correctTag = true; } } if (correctTag == false) { Serial.print("ACCESS DENIED: "); Serial.println(tagID); lcd.clear(); lcd.setCursor(0, 0); lcd.print(" Access Denied!"); printNormalModeMessage(); } } // If door is open... else { lcd.clear(); lcd.setCursor(0, 0); lcd.print(" Door Opened!"); while (!doorOpened) { ultrasonicSensor = hc.dist(); if (ultrasonicSensor < 20) { doorOpened = true; } } doorOpened = false; delay(500); myServo.write(10); // Locks the door printNormalModeMessage(); } } uint8_t getID() { // Getting ready for Reading PICCs if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue return 0; } if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue return 0; } tagID = ""; for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID readCard[i] = mfrc522.uid.uidByte[i]; tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable } tagID.toUpperCase(); mfrc522.PICC_HaltA(); // Stop reading return 1; } void printNormalModeMessage() { delay(1500); lcd.clear(); lcd.print("-Access Control-"); lcd.setCursor(0, 1); lcd.print(" Scan Your Tag!"); } |
Now the device can operate on its own. You can add RFID cards or tokens to the device using a master card. Let us proceed to the next step – connecting our device to Python.
Python Code
In this project, you can interface your newly created device to Python using PyCharm 2021 IDE. It is important to download the latest version of PyCharm to avoid problems that can occur while installing the package you need.
After installing the new version of PyCharm, open the Python Packages in View>Tool Windows. Search for “pyserial” and click “install”.
PySerial is the package that lets you communicate from the Arduino serial monitor. This is why we display on the serial monitor though we have an LCD.


Now go ahead and create an new project in Python and copy the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import serial import time import csv ser = serial.Serial('COM5') #Change to the COM port identified by the Arduino IDE. ser.flushInput() while True: try: ser_bytes = ser.readline() decoded_bytes = ser_bytes[0:len(ser_bytes)-2].decode("utf-8") print(decoded_bytes) with open("RFID_data_log.csv", "a") as f: #Filename of the CSV file writer = csv.writer(f, delimiter=",") writer.writerow([time.time(), decoded_bytes]) except: print("ERROR") break |
The code above will get the data passed to the serial monitor of the Arduino and saves it to a .CSV file. Connect the Arduino to your computer and once you run the program, the device will also restart. The CSV file will be saved inside your Python project folder. The saved data will look like this:

Conclusion
So there you have it! RFID Door Lock Arduino Project with Data Logging in Python. This project is intimidating at first but you will see that it is fairly easy to do. You can combine this to other Python or Arduino projects and build an awesome system!
Click the button below to download the entire project.
Download
Related Articles
Inquiries
Feel free to write your questions about the RFID Door Lock Arduino Project with Data Logging in Python in the comments below.