RFID. True only worked once, but False worked many times

Hi, everyone. Tried my hands on RFID technology. This is my reference: Arduino RFID Door Lock System with LCD Display - YouTube

My problem is this:

For the first few tries, it worked 3 times only on "Access granted" condition. Every time on every reset. On "Access denied", no problem. I can scan invalid cards/tags continuously and the loop still worked. Only after the true condition is met, and for just 3 times, then it wouldn't work.

However, when I tried it again later in the day, Access Granted only worked once and still no problem on access denied. What do you think is the problem?

This is their code:

//Viral Science www.viralsciencecreativity.com www.youtube.com/c/viralscience
//RFID Door Lock System

#include <Wire.h>
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
#define SS_PIN 10
#define RST_PIN 9
#define LED_G 4 //define green LED pin
#define LED_R 5 //define red LED
#define BUZZER 2 //buzzer pin
#define lock 3
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
int Btn = 6;
 
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  pinMode(Btn,INPUT);
  pinMode(lock,OUTPUT);
  Serial.println("Place your card on reader...");
  Serial.println();
  lcd.begin();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0,0); // column, row
  lcd.print(" Scan Your RFID "); 
  lcd.setCursor(0,1); // column, row
  lcd.print("   Door Locked   ");

 }
void loop() 
{

if(digitalRead(Btn) == HIGH){
  
    Serial.println("Access Granted");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    lcd.setCursor(0,1); // column, row
    lcd.print(" Door Un-Locked ");
    tone(BUZZER, 2000);
    delay(100);
    noTone(BUZZER);
    delay(50);
    tone(BUZZER, 2000);
    delay(100);
    noTone(BUZZER);
    digitalWrite(lock,HIGH);
    delay(3000);
    digitalWrite(lock,LOW);
    delay(100);
    digitalWrite(LED_G, LOW);
    lcd.setCursor(0,1); // column, row
    lcd.print("   Door Locked   ");
    tone(BUZZER, 2000);
    delay(100);
    noTone(BUZZER);
  delay(50);
  }


  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();


  
 if ((content.substring(1) == "33 E8 C2 14") || (content.substring(1) == "5C 94 21 4A")) //change here the UID of card/cards or tag/tags that you want to give access
  {
    Serial.println("Access Granted");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    lcd.setCursor(0,1); // column, row
    lcd.print(" Door Un-Locked ");
    tone(BUZZER, 2000);
    delay(100);
    noTone(BUZZER);
    delay(50);
    tone(BUZZER, 2000);
    delay(100);
    noTone(BUZZER);
    digitalWrite(lock,HIGH);
    delay(3000);
    digitalWrite(lock,LOW);
    delay(100);
    digitalWrite(LED_G, LOW);
    lcd.setCursor(0,1); // column, row
    lcd.print("   Door Locked   ");
    tone(BUZZER, 2000);
    delay(100);
    noTone(BUZZER);
    delay(50);
  }

else
{
    lcd.setCursor(0,1); // column, row
    lcd.print("Invalid RFID Tag");
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 1500);
    delay(500);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
    delay(100);
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 1500);
    delay(500);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
    delay(100);
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 1500);
    delay(500);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
    lcd.setCursor(0,1); // column, row
    lcd.print("   Door Locked   ");
 }
 }

They originally used it on Nano, but I used Uno. When I press the reset button on Arduino Uno, it can work again. But once I scan a valid card/tag, it won't work on the second time now. Even if I scan an invalid card/tag afterwards.

Is it a power problem? Memory problem? Or something with my RFID reader? The cards/tags?

TIA

UPDATE: Now it worked, but only after 30 mins of rest? (The time I spent writing my post). And then it goes back to not working again.

how is your button wired ?

can you share a picture of the full circuit and how things are powered?

did you try a basic example with your setup such as that one?

1 Like

I was able to do this and it worked. Tried again recently and it still works.

(Will post the wiring afterwards)





the wiring is just so messy and I see you have an electromagnet too

I'd clean things up and make sure you have proper protection against electromagnetic perturbations and that the power is stable

don't try to debug everything in one go.

Divide and conquer :

➜ just keep the Nano, the RFID reader and the Serial monitor. unplug everything else and see if that works fine (add serial prints to see what would be done)

If you are using the code from the video, and the circuit from the video, and your RFID reader has shown that it is functional…

…and sometimes it works and sometimes it does not, then I suspect you have some marginal connections amongst all your beautiful wiring that makes it sometimes work and sometimes not work.

Changes in observations come from changes in the observed. The code is not changing. The sensor works, so what can be changing?

Go over the wiring to be sure it is 100 percent correct and mechanically firm and stable.

a7

@kimteodoro plesae do divide and conquer.

In particular, temporarily removing from the circuit both the relay and the solenoid will eliminate the possibility of electrical interferences causing unwanted circuit and program behaviour.

Let the print statements, and any you might add, speak to what the relay, and hence the solenoid, woukd be doing if they were still in there.

Maybe: then your sketch works perfectly, albeit without the desired end goal of opening and closing the lock, and you'd have evidence of a problem with the way the relay and solenoid are exactly wired and supplied with power.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.