RFID locking cabinet

Hi Guys

I'm completely new to this and would love some help,
i'm looking to make my own locks for a Phone Charging cabinet.

i'm looking to make a RFID lock that will scan and save the next RFID card used on it, and then will save that info and only unlock the next time that specific RFID card is scanned on that lock.
After unlocking, it should delete the saved RFID card, and be ready to scan a new card next time someone scans their card, and then do it all over again.
And then we will also need a "master RFID card" too, that will always unlock the locks.

I'm thinking i can follow this guide pretty much when building the lock, but the coding just needs to be different, i'm i totally off or what?

I have absolute no idea how the coding works, but i can make the project my self, so i just need help with setting up the code to do what i wrote above, i have tried my self, but just don't know how to build on top of the code to do what i'm asking.

https://create.arduino.cc/projecthub/diyprojectslab/diy-rfid-door-lock-system-074173

Thank you guys in advance.

We can pay for the help if needed.

Most of us here would prefer you not ask us to develop your code for you, but are willing to help you fix problems in the code that you write.
Post the code you have thus far, state in detail what is going wrong or where enhancement may be necessary. Do NOT say "this part doesn't work"! Say instead, "This prints abc and I expected xyz." The words "doesn't work" may be factual, but provide no information as to why that appears to you to be the case.
Looking forward to viewing your code here soon.
Chin up!

1 Like

Hi JaBa
I never said "doesn't work", i said i don't know how to code, and therefor can't build on top of the code i have.
I am for sure not telling anyone to develop the code for me, i'm describing to you how i want the project to work, and what it should do, and i'm stating i don't know how to do the coding my self, i'm sorry, but i just can't.
And i am indeed asking for help, "i just need help with setting up the code" and even offering to pay for the work to, but i'm for sure not telling anyone to develop anything for me.

This is the 1st and only question in the post: "I'm thinking i can follow this guide pretty much when building the lock, but the coding just needs to be different, i'm i totally off or what?"

2nd question would ofc be help with the code, but can't even get that far without someone attacking me or putting words in my mouth on this site, its honestly to bad :frowning:

But this is the code so far, hopefully someone can help me.

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
#define LED_G 5 //define green LED pin
#define LED_R 4 //define red LED
#define RELAY 3 //relay pin
#define BUZZER 2 //buzzer pin
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
 
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(RELAY, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  digitalWrite(RELAY, HIGH);
  Serial.println("Put your card to the reader...");
  Serial.println();

}
void loop() 
{
  // 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) == "XX XX XX XX") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(RELAY, LOW);
    digitalWrite(LED_G, HIGH);
    delay(ACCESS_DELAY);
    digitalWrite(RELAY, HIGH);
    digitalWrite(LED_G, LOW);
  }
 
 else   {
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(DENIED_DELAY);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}

it works if i have 1 specific card, that i input the ID of into the code.
But i need it to, read/save/lock. then read/delete/unlock, and be ready to do it all over again, like i described in the first post.

First off, my apologies if I sounded accusatory. My intent was not to scold, but to advise you in advance. Especially in recognance that you are a "beginner" here (your word, I may have used "novice" instead :slight_smile: )
Give me a few minutes to view the code and I will get back to you.
(The text translator here is fantastic!. I type in English and can read it in real time in German and it actually makes sense! Wow!!!)

1 Like

Ok, this first part is purely advise on style so ignore it if you wish. The way you do it works, but textbooks suggest a different style. At the beginning of your loop, change the "If no ID to read, return" followed by "If ID not read successfully, return" to "If card is ready to read" and "If card read successfully". You move the closing brace then toward the end of the routine. This provides a "single point of exit" for the loop function. Again, purely style.

Now, lets get started making the program do what you want. You are currently reading the ID and then looping through it byte for byte to A: display it on the Serial monitor, and B: concatenate up a String. That may work well in other environments, but on the Arduino String is not recommended, especially with a lot of concat being done because the Arduino doesn't manage String memory well.

The library/class you are using provides the data type for the native ID. It would be better to leave the IDs in that native form. Understandably, you would need a way to compare the ID read to a list of valid IDs. I have never used this library/class but am willing to assist with the comparison if you agree with this suggestion.

My next suggestion is that you define a couple of global variables by declaring them at the top of the program, typically before the setup function. I would suggest three such variables.

bool isLocked = false;  // Indicates if currently locked or open
xxx lockID;                     // ID which locked the case
xxx masterID;               // ID for the Master card

I have used xxx here because, as I said, I am unfamiliar with the library/class. Instead of xxx you would need to put in the native data type for ID cards that the library/class provides for you. If you can't figure that out, I'll see if I can find it for you.

After having sucessfully reading an ID, if the case is unlocked (!isLocked), lock it and set lockID = ID that was read. else If the case is locked and the ID is a match, open it and set isLocked back to false. The same applies if you read the Master ID.

So, now, determine if my advise is worth following and implement as much of it as you can. If it still isn't right for you, post the code again and I or others will again provide some tips.

Again, I apologize if I sounded condescending.

1 Like

Hi JaBa
Its totally fine, water under the bridge, i've just been met with some less friendly guys here, so i was a bit defensive.

I've now looked at it over the weekend, and i totally see what you want to change, and what you want it to do instead, but i just can't figure it out, i don't code my self, and have never learned it the right way, i can read through it, and see what it wants to do, but editing it, is just above my skill level, i've tried to look through the library, and i can't seem to find the functions i'm looking for, i might just be looking the wrong places tho, so i'm pretty much stuck where we left off.

changed:

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

bool isLocked = false;  // Indicates if currently locked or open
MIFARE_SetUid = lockID;                     // ID which locked the case
xxx masterID;               // ID for the Master card
 
#define SS_PIN 10
#define RST_PIN 9
#define LED_G 5 //define green LED pin
#define LED_R 4 //define red LED
#define RELAY 3 //relay pin
#define BUZZER 2 //buzzer pin
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
 
void setup() 
{
  SPI.begin();          // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  digitalWrite(RELAY, HIGH);


}
void loop() 
{
  // Look for new cards
  if isLocked = false ( ! mfrc522.PICC_IsNewCardPresent()) 
	then save that card as = lockID and lock.
  {
    return;
  }
  // 
  if isLocked = true ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    if lockID is correct, unlock
	else keep isLocked = true
  {
    return;
  }
  // Master Card 
  if isLocked = true ( ! mfrc522.PICC_ReadCardSerial()) 
	if masterID, unlock
  }
}

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