RFID Power Switch

Hello,

This is my first time playing with RFID and I'm trying to make a simple power switch that will toggle ON when the RFID keyfob is present but will turn the power OFF when keyfob is remove with some delay.

I tried this code that I found after some Googling, my only problem is the code that will turn it off when the keyfob is not present.

Any help will be appreciated.

/*
 * http://geek.adachsoft.com
 * 
 * 
 * 
*/
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.


void setup() {
  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("For more: http://geek.adachsoft.com");
  Serial.println("RFID Switch");
  Serial.println("");
  pinMode ((3), OUTPUT);
}

void loop(){
  //Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent() ){
    return;
  }
  //Select one of the cards
  if ( !mfrc522.PICC_ReadCardSerial() ) {
    return;
  }
  
  String content= "";
  byte letter;
  for( byte i = 0; i < mfrc522.uid.size; i++ ){
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
     if( i < mfrc522.uid.size-1 ) content+="-";
  }
  content.toUpperCase();
  Serial.println();
  Serial.println("UID tag :'" + content + "'");

  if( content == "7-15-43-1F" ){
    Serial.println("Authorized access");
    digitalWrite(3, HIGH);
    
  }else{
    Serial.println("Access denied");

  }
  
  delay(1000);
}

What have you tried and what were the results?

Paul

Tried the code above and it will only turn the LED on, even if I remove the keyfob. What I want is to turn it off when the keyfob is remove.

I found a similar topic here Turning on the light while the RFID tag is in place? but not a solution.

Somehow I made it to work using this code,

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

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

void setup() {
  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("RFID Power Switch");
  Serial.println("");
  pinMode ((3), OUTPUT);
}


void loop(){
  
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    Serial.println("No card");
    digitalWrite(3, LOW);
    return;
    
}

  Serial.println("Present");
  digitalWrite(3, HIGH);
  delay(5000);
  mfrc522.PICC_HaltA();
}

but will respond to any RFID tag brought near to it. :frowning:

Now my problem is adding a BEEP tone when an RFID tag is first detected.