RFID Lock Type On/Off

RFID On/Off Lock Project

The goal of the project was to achieve a result where the relay is off upon startup. After presenting the appropriate card or key fob, the relay turns on, and when the card is presented again, the relay returns to its initial state. It’s an ON/OFF switch primarily used for controlling lights, power strips, etc.

Components Used:

  • Arduino NANO V3 clone
  • Relay
  • RFID-RC522 module

Libraries and Sketches Used:

Wiring diagram in higher resolution: link

Code to Read the Card:

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
}

void loop() {
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial())
  {
    Serial.print("UID: {");
    Serial.print(rfid.uid.uidByte[0] < 0x10 ? "0x0" : "0x");
    Serial.print(rfid.uid.uidByte[0], HEX);
    Serial.print(rfid.uid.uidByte[1] < 0x10 ? ", 0x0" : ", 0x");
    Serial.print(rfid.uid.uidByte[1], HEX);
    Serial.print(rfid.uid.uidByte[2] < 0x10 ? ", 0x0" : ", 0x");
    Serial.print(rfid.uid.uidByte[2], HEX);
    Serial.print(rfid.uid.uidByte[3] < 0x10 ? ", 0x0" : ", 0x");
    Serial.print(rfid.uid.uidByte[3], HEX);
    Serial.println("}");
    rfid.PICC_HaltA();
    rfid.PCD_StopCrypto1();
  }
}

After uploading the code to Arduino, open the serial port monitor, place the card on the RFID module, and read the card’s code. Here’s an example:

Next, save the card’s code, which will be entered into the following code to meet the project requirements:

RFID ON/OFF Relay Code:

#include <SPI.h>
#include <MFRC522.h>

#define relay 8
const byte UID[] = {0x00, 0x00, 0x00, 0x00}; // key fob

int state = 0;
int previous_state = 0;

MFRC522 rfid(10, 9);
MFRC522::MIFARE_Key key;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
}

void turn_on_relay() {
  digitalWrite(relay, LOW);
}

void turn_off_relay() {
  digitalWrite(relay, HIGH);
}

void loop() {
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    if ( rfid.uid.uidByte[0] == UID[0]  &&
         rfid.uid.uidByte[1] == UID[1]  &&
         rfid.uid.uidByte[2] == UID[2] &&
         rfid.uid.uidByte[3] == UID[3] ) {
      if (state == 0) {
        Serial.println("GOOD: LIGHT ON");
        Serial.println("");
        turn_on_relay();
        previous_state = 1;
        state = 1;
      } else {
        if (previous_state == 1) {
          Serial.println("GOOD: LIGHT OFF");
          Serial.println("");
          turn_off_relay();
          previous_state = 0;
          state = 0;
        }
      }
    } else {
      Serial.println("BAD");
    }
    rfid.PICC_HaltA();
    rfid.PCD_StopCrypto1();
  }
}

Replace the variable const byte UID[] = {0x00, 0x00, 0x00, 0x00}; // key fob with the actual card code, as in the example:

const byte UID[] = {0x96, 0x09, 0xF1, 0x30}; // key fob

In the future, I will show how I used this project in practice.

This is my first post, and I hope for constructive feedback, both positive and negative, in the comments! 🙂

5/5 - (2 votes)

Leave a Reply