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:
- Arduino Uno R3
- Relay Module 1 Channel 5V
- RFID / NFC Module PN532 13.56MHz
- Power Adapter 12V 2.5A Safety Mark
- Jumper Wire Male-Male 40x
- Solenoid Door Lock
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.
- 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.
- The GND pin on the Relay connects to the GND pin of the Arduino Uno.
- 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.
- The NO pin on the Relay connects to the Solenoid lock to supply power to unlock it.
- 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.
- 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
- Download the full source code: Click Here