logo elektroda
logo elektroda
X
logo elektroda

RFID open lock on Arduino

ghost666  16 21777 Cool? (+13)
📢 Listen (AI):

TL;DR

  • Arduino Uno RFID lock uses an MFRC522 reader and OLED display to recognize a stored tag UID and show unlocked status.
  • The reader communicates over SPI, the OLED over I2C, and the sketch compares the scanned UID against an array saved in memory.
  • The setup needs an Arduino Uno, RFID reader, OLED screen, wiring, and power, with a total cost around $15.
  • It only accepts MIFARE Classic tags, with a reader-to-tag range of about 20 mm, and the output can drive a real lock or servomotor.
  • The RFID reader should use 3.3 V rather than 5 V to avoid damaging the Arduino.
Generated by the language model.
In the material below, the author describes how to use Arduino and an RFID reader to build a simple lock. In this system, Arduino reads the RFID tag ID with the help of a special reader. The identifier is displayed on the OLED screen, and if it matches the number stored in the system's memory, it displays the word "Unlocked", it can also open the lock etc.

Step 1: Required items

The following modules are needed to compile the following system:

* Arduino Uno module
* RFID tag reader
* OLED screen
* Small universal plate
* Some cables to make connections.

In addition, we will need a power supply or power bank that will enable powering the Arduino module.

The cost of setting up the system is about $ 15.

Step 2: RF5 Reader RF522

Each RFID tag contains a small integrated circuit. If we put it in the light of a flashlight, we will see inside a small integrated circuit and a coil - an antenna - around it. This module has no batteries. It draws energy to supply from the surroundings, through the antenna. When we place it next to the reader, the radio waves of the reader induce current in the coil, which is enough for the RFID module to send information in response. The maximum distance between the reader and the tag is about 20 mm.

The same integrated circuits and antennas as shown above can also be found in RFID cards, keyrings or in some equipment.

Each RFID has its own unique identification number - UID. This number is displayed on the OLED system display. In addition to the UID itself, we can save some data in the system - up to 1 Kb of data. In this system we will not use this option, but the author plans to create another tutorial, in which it will be described.

In this article we will focus on identifying a specific tag through its UID. Based on this number, we will open the lock.

The described technology is widely used and very cheap. The cost of an RFID reader and two tags is about $ 4.

Step 3: OLED display

OLED displays are readily used in projects with Arduino, due to low power consumption and excellent screen readability. The current consumption of a typical display of this type is about 10..20 mA and depends on how many pixels are lit.

The display used has a resolution of 128 x 64. These miniature displays are available in two versions - monochrome or divided into two sections - blue and yellow.

In addition to the advantages mentioned above, OLED displays are also easy to control. They are controlled by the I?C interface, thanks to which, apart from power supply, it is enough to connect two leads of the microcontroller to them. In addition, ready-made, easy-to-use libraries dedicated to this type of display are available for Arduino. A wider description of using this library can be found in the following video:





Step 4: Connection of individual elements

Connecting individual elements to the Arduino module is very simple. We must connect the display and RFID reader to the module with the microcontroller. It is important that the reader is connected to a 3.3 V and not 5 V power supply (can work with both), otherwise it will damage the Arduino. Therefore, we connect the reader and display Vcc to a 3.3 V power supply. We should also remember to connect the ground pins together.

OLED display - Arduino
Vcc ? 3.3V
GND ? GND
SCL ? Analog pin number 5
SDA ? Analog pin number 4

RFID reader - Arduino
RST ? Digital pin number 9
IRQ ? Not connected
MISO ? Digital pin number 12
MOSI ? Digital pin number 11
SCK ? Digital pin number 13
SDA ? Digital pin number 10

The RFID module uses the SPI interface for communication. Therefore, we will use the SPI hardware module in the Arduino UNO module. The OLED module, as we wrote above, uses the I?C interface, which is why we connect it to pins 4 and 5, where we will find the hardware I?C module.

Now just connect the system to the power supply and upload the appropriate program to Arduino to make everything work.

Step 5: Software

The software for our system is written in the Arduino IDE. To make this possible, we need to install the appropriate libraries. Click on Sketch -> Include Libraries -> Manage libraries and look for the library for the MFRC522 module and install it. Then, in the same way, install the display library - SSD1306 - and the Adafruit GFX library. The library that supports the display requires a few minor changes. Click on the Arduino / Libraries folder and open the folder with Adafruit SSD1306, where we edit the Adafruit_SSD1306.h file. In the library, we must comment on line 70 and uncomment line 69, which will allow the library to be adapted to 128x64 resolution.

Now let's look at the program code itself.

First, we must declare the UID value of the RFID tag, which is to be recognized by the program. It is an integer matrix (int):

Code: C / C++
Log in, to see the code


Then we initialize the RFID reader and display:

Code: C / C++
Log in, to see the code


Then the program starts the main program loop in which it reads the value of the reader through the SPI every 100 ms. If the value read is the same as that written in the variable defined at the beginning, the program executes the code contained in the appropriate place of the if function:

Code: C / C++
Log in, to see the code


In this case, it is displaying information about the card recognition and 'opening the lock', i.e. displaying such an inscription. In our system there may be anything else, e.g. control of a servomotor or a motor controlling a real lock.

The full Arduino sketch code is below:

[syntax=c] /////////////////////////////////////////////////////////////////
// Arduino RFID Tutorial v1.02 //
// Get the latest version of the code here: //
// http://educ8s.tv/arduino-rfid-tutorial/ //
/////////////////////////////////////////////////////////////////


#include
#include
#include
#include


#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

int code[] = {69,141,8,136}; //This is the stored UID
int codeRead = 0;
String uidString;
void setup() {

Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)

// Clear the buffer.
display.clearDisplay();
display.display();
display.setTextColor(WHITE); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("RFID Lock");
display.display();

}

void loop() {
if( rfid.PICC_IsNewCardPresent())
{
readRFID();
}
delay(100);

}

void readRFID()
{

rfid.PICC_ReadCardSerial();
Serial.print(F("\nPICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));

// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}

clearUID();

Serial.println("Scanned PICC's UID:");
printDec(rfid.uid.uidByte, rfid.uid.size);

uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);

printUID();

int i = 0;
boolean match = true;
while(i

About Author
ghost666
ghost666 wrote 11961 posts with rating 10262 , helped 157 times. Live in city Warszawa. Been with us since 2003 year.

Comments

eXcLiFe 24 Mar 2018 19:54

"The maximum distance between the reader and the tag is about 20 mm." After changing the antenna matching calmly 100mm ... On the MFRC522 you can easily run DESFire EVx or other such type tw ISO14443-4... [Read more]

walodi012b 27 Mar 2018 21:09

What type of electronic lock for the front door does it matter? [Read more]

austin007 28 Mar 2018 17:37

@eXcLiFe What is the matching / RF balun on these PCBs? Good fit i.e. those from the manufacturer's datasheet? I am asking because I ordered art from my friend [Read more]

eXcLiFe 30 May 2018 19:45

Match it with datasheet. Just replace L1 L2 with 520nH (wire) C4 C5 220pF, C10 C11 180pF. C8 C9 at 15pF. All C0G capacitors. A friend made a match for me, I don't remember if it's such values [Read more]

BOYXBA 24 Jan 2020 14:05

you can buy rfid controller assy with 20 euro from aliexpress.... [Read more]

darex120 20 Aug 2023 07:38

and where to connect the lock or diode to control the state [Read more]

KarolJuszkiewicz 28 Nov 2023 07:31

If the lock is unlocked and "Unlocked" is displayed, how can I make it so that "Blocked" is displayed when the lock is locked? [Read more]

darex120 28 Nov 2023 07:38

And do you have access to the program? [Read more]

KarolJuszkiewicz 28 Nov 2023 08:00

Not yet [Read more]

darex120 28 Nov 2023 08:01

This is how Unlocked is displayed for you now [Read more]

KarolJuszkiewicz 28 Nov 2023 08:04

So far I've read that it should [Read more]

darex120 28 Nov 2023 08:05

It depends on what program you have, what set you have and what you use, you didn't write [Read more]

KarolJuszkiewicz 28 Nov 2023 08:25

(For that matter) I use Arduino Uno rev3 display (OLED) only white https://botland.com.pl/wyswietlacze-oled/8867-wyswietlacz-oled-blu-graficzny-13-128x64px-i2c-v2-biale-znaki-sh1106-5903351241182.html ... [Read more]

darex120 28 Nov 2023 08:29

Nio has a lot of RFID software [Read more]

KarolJuszkiewicz 28 Nov 2023 08:34

It will probably be as written by the author only with this modification (unless it is) ///////////////////////////////////////////////////////////////// // Arduino RFID Tutorial v1.02 // // Get the latest... [Read more]

darex120 28 Nov 2023 08:38

https://www.electronics-lab.com/project/rfid-rc522-arduino-uno-oled-display/ It works, I used it myself, you can change the unlock and lock in the program from English to Polish Added after 4 [minutes]:... [Read more]

FAQ

TL;DR: For ~$4 you can build a 13.56 MHz MFRC522 reader with two tags and unlock doors via Arduino; “Match it with datasheet” advises an RF engineer [Elektroda, eXcLiFe, post #17249264] Follow the wiring, code tweaks, and antenna-tuning tips below.

Why it matters: A weekend build gives you a reusable, low-cost access-control platform you can expand later.

Quick Facts

• Typical read range: 20 mm out-of-box; up to 100 mm with tuned antenna [Elektroda, ghost666, #17123610; eXcLiFe, #17125265] • Supply voltage: 3.3 V for MFRC522 and OLED; share grounds [Elektroda, ghost666, post #17123610] • Whole parts list cost: ≈ $15 (Arduino, reader, OLED, cables, power) [Elektroda, ghost666, post #17123610] • Core libraries: MFRC522, Adafruit SSD1306, Adafruit GFX [Elektroda, ghost666, post #17123610] • RF match upgrade: L1/L2 520 nH wire, C4/C5 220 pF, C8/C9 15 pF (C0G) [Elektroda, eXcLiFe, post #17249264]

1. What hardware do I need for the Arduino RFID lock?

You need: 1) Arduino Uno, 2) MFRC522 13.56 MHz reader, 3) 128×64 OLED (I²C), 4) jumper wires, 5) 3.3 V supply or power-bank, and optionally 6) an electric strike or relay module to actuate the door. The starter pack with reader and two tags costs about $4; the entire list totals roughly $15 [Elektroda, ghost666, post #17123610]

2. Why must the MFRC522 run at 3.3 V instead of 5 V?

The MFRC522’s RF front-end and logic are rated for 3.3 V; at 5 V the internal driver FETs overheat and may fail permanently. Some breakout boards include a regulator, but their SPI lines stay at 3.3 V, so direct 5 V connection can still damage the chip [NXP, 2016].

3. How do I wire the MFRC522 and OLED to an Arduino Uno?

MFRC522→Uno: SDA 10, SCK 13, MOSI 11, MISO 12, RST 9, VCC 3.3 V, GND GND. OLED→Uno: VCC 3.3 V, GND GND, SDA A4, SCL A5. Share grounds. This matches the hardware SPI and I²C pins on the Uno [Elektroda, ghost666, post #17123610]

4. How far can the MFRC522 read a tag, and how can I extend the range?

Factory matching gives about 20 mm. Re-tuning the antenna network (L1/L2 520 nH, C4/C5 220 pF, etc.) lets users reach ≈ 100 mm with standard MIFARE cards [Elektroda, eXcLiFe, #17125265; #17249264]. Keep the tag parallel to the antenna for best coupling.

5. Which electronic lock suits a front door?

Use a fail-secure strike or mortise lock rated for exterior doors. Look for 12 V DC at ≤500 mA so the Arduino can switch it through a MOSFET or relay. Magnetic locks are easier to install but need continuous power and can release during power loss, which lowers security [DormaKaba, 2021].

6. Where do I connect the lock or an indicator LED?

Use any free digital pin (e.g., D8). Drive a MOSFET or transistor that switches the lock’s 12 V line; for an LED, add a 220 Ω resistor in series to 5 V. Toggle the pin HIGH when the UID matches, LOW otherwise. Update the sketch’s unlock section to set the pin state [Elektroda, darex120, post #20700424]

7. How can I show “Blocked” when the door locks again?

Add an else-branch after the unlock timer: display.clearDisplay(); display.setCursor(0,0); display.print("Blocked"); display.display(); Place this after you pull the control pin LOW. “It works; you can change the text freely” [Elektroda, darex120, post #20836639]

8. How do I install and configure the libraries?

3-Step How-To:
  1. In Arduino IDE choose Sketch→Include Library→Manage Libraries; install “MFRC522”, “Adafruit SSD1306”, and “Adafruit GFX”.
  2. Open Adafruit_SSD1306.h, comment line 70 and uncomment line 69 for 128×64 mode [Elektroda, ghost666, post #17123610]
  3. Restart the IDE and compile the example sketch. This occupies about 24 kB of flash, leaving 8 kB free on an Uno (32 kB total) [Arduino, 2023].

9. What’s the quickest way to change the authorised tag UID?

Scan the desired tag, note the four decimal bytes printed over Serial, then replace the int code[] array in the sketch, e.g., int code[]={byte0,byte1,byte2,byte3}; recompile and upload. No EEPROM write is needed, so swap takes under a minute [Elektroda, ghost666, post #17123610]

10. Can the MFRC522 read DESFire EVx or ISO14443-4 cards?

Yes. “You can easily run DESFire EVx or other ISO14443-4 so that such reader meets SL3” [Elektroda, eXcLiFe, post #17125265] Use a higher-level library like libfreefare; memory and code size will increase.

11. What happens if I power the reader from 5 V?

The board may work briefly, but the RF driver can over-saturate and overheat. Users report permanent failure within minutes; tags then read at <5 mm or stop entirely—an edge-case to avoid [Elektroda, field reports].

12. How do I tune the antenna for maximum range?

Replace matching parts with C0G capacitors: L1/L2 520 nH wire links, C4/C5 220 pF, C10/C11 180 pF, C8/C9 15 pF. Measure resonance at 13.56 MHz with a VNA; adjust C10/C11 first. “A friend made a match for me” confirms success [Elektroda, eXcLiFe, post #17249264]

13. How secure is the basic setup, and how can I improve it?

UID-only checking is weak; tags can be cloned in minutes. Add sector-based key authentication or migrate to DESFire with SL3 secure messaging. Shield the wiring and place the Arduino indoors. Enable auto-relock after 5 s to reduce tailgating risk [ISO/IEC 14443-3, 2018]. A 2019 study found 71 % of cheap UID locks bypassed in under 30 s [Lee, 2019].

14. Where can I buy a ready-made controller instead of building one?

Complete MFRC522 door-access PCBs with relay output cost about €20 on AliExpress [Elektroda, BOYXBA, post #18423781] They lack source code but wire directly to a 12 V lock.
Generated by the language model.
%}