Arduino Tutorial: RFID Smart Lock

Introduction

This project explains how to make an RFID (Radio Frequency Identification) lock system. This system allows the unlocking of a door, a cabinet, or a gate with an RFID tag, an RFID card reader, an Arduino Uno, a Solenoid Lock, and a few electronics components.

 

Hardware Required

You may purchase the individual components here:

  1. Arduino Uno
  2. Relay Module 1 Channel 5V
  3. RFID / NFC Module PN532 13.56MHz
  4. Power Adapter 12V 2.5A Safety Mark
  5. Jumper Wire Male-Male 40x
  6. Solenoid Door Lock

 

Arduino Uno

Arduino/Genuino Uno is a microcontroller board based on the ATmega328P. It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started. You can tinker with your UNO without worring too much about doing something wrong, worst case scenario you can replace the chip for a few dollars and start over again.

Relay Module 1 Channel 5V

The relay module is able to control various appliances and other equipments with large current. Relay output maximum contact is AC 250V 10A or DC 30V 10A. Each channel consumes about 15 - 20mA current when active.

RFID / NFC Module PN532 13.56 MHz

This RFID / NFC module is a read / write module that is built around NXP PN532 and is very popular in the NFC field. Everything is packed nicely into a small form factor, making it easy to embed into your project. What's good about this module is that you can select communication modes between I2C, SPI and HSU (High Speed UART) via the 2 small switch on board. There is an on board level shifter, so you can work with both 3.3V and 5V signals.

Power Adapter 12V 2.5A Safety Mark

If you're looking to power your Arduino, this power adapter is something you need.

The power adapter will supply only the required current to your load. Even if you have a 10A power adapter, it will only supply 1A to your 1A rated load so you don't have to worry it will damage your load with a higher current power adapter. The important thing to check is to make sure the voltage matches your load voltage rating. Current rating can be higher but not lower.

Solenoid Door Lock

The solenoid lock is an electronic locking system. Until power is brought to the lock, the solenoid slug will obstruct movement and you can't open it. Once 9-12VDC is sent to the lock, the solenoid slug pulls in and unlocks. 

Connecting the Hardware

Connect the RFID/NFC Module to the corresponding pins on the Arduino. There are 4 pins on the RFID/NFC Module, namely, the GND, VCC (Power Supply), SDA (data pin), and SCL (clock pin). Connect these pins to the corresponding pins for GND, 3.3V, SDA, and SCL on the Arduino Uno.

Flip the I2C switch on the RFID/NFC module to 1 to turn it on. This allows the RFID/NFC module and the Arduino module to send and receive data between each other.

The Arduino Uno operates at 5 volts and cannot control the Solenoid lock directly because the Solenoid Lock is a 12-volt device. Thus, we set up a one-channel relay to allow the Arduino Uno to control the Solenoid lock. Connect the Relay module to the Arduino as seen in the picture below.

  1. The In pin on the Relay connects to pin 2 of the Arduino Uno. We will be sending a signal from the Arduino Uno to this pin to activate the relay.
  2. The GND pin on the Relay connects to the GND pin of the Arduino Uno.
  3. The VCC pin on the Relay connects to the 5V pin of the Arduino Uno to supply power to it.

We will connect the Solenoid Lock to the Relay module and program the Arduino Uno to send a signal to it to unlock it. Connect the Solenoid lock to the Relay module and the Arduino Uno as seen in the picture below.

  1. The NO pin on the Relay connects to the Solenoid lock to supply power to unlock it.
  2. The COM pin on the Relay connects to the VIN pin on the Arduino Uno. To unlock the Solenoid lock, we will be powering the Arduino using an external power source rather than via USB cable to a computer.
  3. The grounding wire on the Solenoid lock connects to the GND pin on the Arduino Uno.

Here's how the project should look like after it is assembled.

Discovering The Unique Identifier of the RFID Card

To use the RFID/NFC module, we will need to install its libraries. The RFID/NFC module uses the NFC Data Exchange Format (NDEF) format, a standard that operates across all NFC devices. To install its libraries on Arduino, visit https://github.com/elechouse/PN532. Follow the instructions under “Getting Started” to install the library.

Open the example from PN532 > iso14443a_uid. We’re going to find out the UID of our RFID card.

Select the Arduino board from Tools > Board > Arduino Uno. Then select the port from Tools > Port > xxx. Upload the program and open the serial monitor. Set the baud rate to 115200. You should see this if your connection is correct:

Tap the RFID card on the reader. The reader will read the card and display its UID.

Copy this UID as we will be using it in the next step. 

Programming The Arduino Uno

We are ready to program the Arduino Uno to recognize the UID of the RFID card and to unlock the Solenoid Lock when it detects that the RFID card is valid.

Create a new sketch. The source code for this sketch can be downloaded at Kuriosity's Github Repository at the end of this tutorial.

Replace the valid variable with the UID that we copied in the previous step. 

void loop(void) {
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t valid[] = { 0xD3, 0x23, 0xB0, 0x1A }; // Store UID of authorized card
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

Add a logic to compare the UID read from the RFID card and the value in the sketch.

success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);

if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
Serial.print("UID Value: ");

Add a compare logic to activate the relay only when authorized card is detected.

if (memcmp(uid, valid, 4) == 0) {
Serial.println("Welcome Kuriosity!");

// activate relay to unlock door
digitalWrite(relayPin, HIGH);
// hold door unlock for 3s
delay(3000);
// deactivate relay to lock door
digitalWrite(relayPin, LOW);
}
else {
Serial.println("UNAUTHORIZED!");
}

And we’re done! The relay now unlocks when you tap the authorized card. 

Now unplug the USB from Arduino Uno and connect the Power Adapter into the barrel jack. This will power the Arduino Uno as well as the Solenoid Lock.

Tap the card on the RFID module and you should see the Solenoid Lock unlock! Remember not to connect both USB and Power Adapter at the same time because supplying more than 1 power source might damage the board.

Finally, install the RFID Smart Lock device on a cabinet, door, or gate that you wish to secure!

Resources