RFID-RC522 ignition lock

Hi

I’m trying to make a ignition lock to my beetle '69 using an rfid card.

I want to swipe my card/tag to the antenna to turn on the ignition, and swipe it again to turn it off.

I found a code on youtube, but i can not find out how to make it turn on and off without delay.

Can someone help me?

(sorry for bad English)

/*
 * ----------------------------------------------------------------------------
 * This sketch uses the MFRC522 library ; see https://github.com/miguelbalboa/rfid
 * for further details and other examples.
 * 
 * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
 * 
 * This sketch show a simple locking mechanism using the RC522 RFID module.
 * ----------------------------------------------------------------------------
 * Typical pin layout used:
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno           Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 *
 */
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

String read_rfid;
String ok_rfid_1="25bcefb0";
//String ok_rfid_2="ffffffff"; //add as many as you need.
int lock=4; //Which pin the lock will be on if using a relay or solenoid or similar 


 

/*
 * Initialize.
 */
void setup() {
    Serial.begin(9600);         // Initialize serial communications with the PC
    while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card

    //Choose which lock below:
    pinMode(lock, OUTPUT);
    
}

/*
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid=read_rfid + String(buffer[i], HEX);
    }
}

void open_lock() {
  //Use this routine when working with Relays and Solenoids etc.
  digitalWrite(lock, HIGH);
  delay(2000);
  digitalWrite(lock,LOW);
  
}

void loop() {

      // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;

    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println(read_rfid);
    if (read_rfid==ok_rfid_1) {
      //ok, open the door.
      open_lock();
    }
    //Add below as many "keys" as you want
    //if (read_rfid==ok_rfid_2) {
      //also ok, open the door
    //  open_lock();
    //}
    // else not needed. Anything else is not ok, and will not open the door...
}

Just checking,
your question is more complex than just deleting this, right?

void open_lock() {
  //Use this routine when working with Relays and Solenoids etc.
  digitalWrite(lock, HIGH);
  // => delay(2000);              // Please tell me it is not solved by removing this delay?
  digitalWrite(lock,LOW);
 
}

Kevin77:
Just checking,
your question is more complex than just deleting this, right?

void open_lock() {

//Use this routine when working with Relays and Solenoids etc.
  digitalWrite(lock, HIGH);
  // => delay(2000);              // Please tell me it is not solved by removing this delay?
  digitalWrite(lock,LOW);

}

Yes, it is.
I want to swipe my card and the "lock" open (HIGH), and swipe it again to get the "lock" to close (LOW).
The "lock" is a relay.

This is my first time with rfid, and i'm new to Arduino.

I am glad!

:slight_smile:

just add a variable to the function that recalls the state it is in and change it...

void open_lock() 
{
  static boolean isOpen=true;

  if (isOpen)
  {
      digitalWrite(lock, HIGH);
  }
  else
  {
      digitalWrite(lock,LOW);
   }

   isOpen=!isOpen;

}

would this work?

Also what are you switching?

an LED or a relay?

Kevin77:
I am glad!

:slight_smile:

just add a variable to the function that recalls the state it is in and change it...

void open_lock() 

{
 static boolean isOpen=true;

if (isOpen)
 {
     digitalWrite(lock, HIGH);
 }
 else
 {
     digitalWrite(lock,LOW);
  }

isOpen=!isOpen;

}




would this work?

Yes, it will:-D I only added a delay!

Thank you Kevin, you made my day:-)

void open_lock() {
{
  static boolean isOpen=true;

  if (isOpen)
  {
      digitalWrite(lock, HIGH);
  }
 
  else
  {
      digitalWrite(lock,LOW);
   }

   isOpen=!isOpen;
   delay(3000);

}
  
}

You're welcome!

post some pictures of your finished project when you're done!

Kevin77:
You're welcome!

post some pictures of your finished project when you're done!

I'm gonna do that:-)

Pissulf:
I'm gonna do that:-)

Pissulf:
I'm gonna do that:-)

Hi,
I am also looking to build a Swipe to Lock/Unlock Relay for my car.
have tried a code from somewhere, it works just fine, but I am unable to put relay to work.
Have connected RC522 and pulled the info of my card and fob, then was able to specify it in script and upload, It grants access only on valid card but the code is for Servo based project and not relay based which I am looking for.

Another one I tried uses a Servo, it works fine but being a newbie with Arduino I can't use it with relay
as the servo code is designed to bring back the latch to original location.

Can you please help me make this to work.

In your code above I don't see PINS where relay is defined ?
need to install this in my car for Door unlock, have already pulled wires from existing wiring and able to lock and unlock by shorting 2 wires.

can you please share the code or guide me.