RFID Door Lock With 150 Tag How To Do It

Hi, gues , I try to use this code and its working very well.
But this way the maximum number of tags that I can use is 83, but I need to use more like 150 or so...
btw. Im using Arduino Uno, lcd 16x2,relay, RFID reader, for door lock for my Building
How can I do it... when I put more RFID Card Code than 83 its stop reading them properly and code going crazy if add one more card ... w warning message i see when verify Sketch uses 9364 bytes (29%) of program storage space. Maximum is 32256 bytes.
Global variables use 1783 bytes (87%) of dynamic memory, leaving 265 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.

#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>
#define SS_PIN 5
#define RST_PIN 9
#define RELAY 3 
//#define BUZZER 2 
#define ACCESS_DELAY 100
#define DENIED_DELAY 1000
MFRC522 mfrc522(SS_PIN, RST_PIN); 
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0); 

const char* authorizedUIDs[] = {
 "A1 65 1B 1C",
"81 EC 1A 1C",
"46 78 4B 40",
"B1 E8 1D 1C",
"46 4A 34 40",
"D6 37 4B 40",
"56 64 41 40",
"D0 99 73 40",
"62 05 57 C0",
"F0 89 A5 40",
"A0 79 86 40",
"B0 07 71 40",
"C1 3D 1B 1C",
"46 A9 41 40",
"66 74 35 40",
"01 23 1D 1C",
"D0 C2 4B E9",
"21 AF 21 1C",
"C1 CA 19 1C",
"01 74 22 1C",
"51 6E 1D 1C",
"41 1E 20 1C",
"31 04 1F 1C",
"96 12 3B 40",
"76 AB 41 40",
"96 99 3A 40",
"96 5A 35 40",
"C6 43 3C 40",
"36 83 4B 40",
"46 65 41 40",
"46 39 4C 40",
"16 AF C8 B4",
"06 7F 41 40",
"26 BF 3C 40",
"46 38 34 40",
"00 6D 8D 40",
"20 58 6E E9",
"E6 0C 4B 40",
"72 93 CB 9A",
"F6 65 3D 40",
"F6 3C 35 40",
"26 61 3B 40",
"36 9F 34 40",
"32 31 E7 91",
"D2 39 DC BF",
"F6 0B 39 40",
"51 28 1C 1C",
"F0 58 AB 40",
"11 A6 22 1C",
"40 50 84 40",
"E2 AE E0 BF",
"A6 37 35 40",
"56 AE 41 40",
"C6 1E 3A 40",
"E6 E6 39 40",
"06 66 36 40",
"96 0E 38 40",
"86 51 3B 40",
"C6 B6 40 40",
"66 EB 3C 40",
"70 D0 76 40",
"E1 C7 20 1C",
"C1 91 20 1C",
"90 E7 52 E9",
"F6 E0 3A 40",
"B0 7E 66 E9",
"50 D6 70 40",
"20 3D 6C E9",
"96 3D 48 40",
"94 6F 78 6B",
"F6 12 38 40",
"C0 68 91 40",
"11 5B 19 1C",
"E1 C0 1C 1C",
"10 79 6A E9",
"90 B8 68 40",
"50 86 86 40",
"62 0C 95 91",
"46 76 4C 40",
"B0 40 B0 40",
"D0 2C 53 E9",
"41 52 20 1C",
"F0 61 78 40",
"90 E6 AE 40",
"F2 7E C5 91",
"66 0C 37 40",
"66 20 37 40",
"26 3D 4B 40",
"E1 44 DC 06",
"A0 5E 4F E9",
"96 2E 3D 40",
"53 1F A1 98",
"E0 1D 8B 40",
"46 9D 41 40",
"D0 E0 53 E9",
"70 9E 83 40",
"D1 3D 1C 1C",
"11 AB 20 1C",
"F3 A6 A8 BB",


};

void setup()
{
  Serial.begin(9600); 
  SPI.begin();        
  mfrc522.PCD_Init(); 
  pinMode(RELAY, OUTPUT);
//  pinMode(BUZZER, OUTPUT);
//  noTone(BUZZER);
  digitalWrite(RELAY, HIGH);

  lcd.begin(16, 2); 
  lcd.print("TPCB Factory ");
  lcd.setCursor(0, 1);
  lcd.print("Put Your Card");
}

void loop()
{
  // Look for new cards
  if (!mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  if (!mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  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();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("UID tag:");
  lcd.setCursor(0, 1);
  lcd.print(content);

  // Check if the UID is authorized
  bool isAuthorized = false;
  for (const char *authorizedUID : authorizedUIDs)
  {
    if (content.substring(1) == authorizedUID)
    {
      isAuthorized = true;
      break;
    }
  }

  if (isAuthorized)
  {
    Serial.println("Authorized access");
    lcd.clear();
    lcd.print("Authorized");
    delay(500);
    digitalWrite(RELAY, LOW);
    lcd.clear();
    lcd.print("Access granted");
    delay(ACCESS_DELAY);
    digitalWrite(RELAY, HIGH);
    lcd.clear();
    lcd.print("Welcome To ");
    lcd.setCursor(0, 1);
    lcd.print("      ....");
  }
  else
  {
    Serial.println("Access denied");
    lcd.clear();
    lcd.print("Access denied");
//    tone(BUZZER, 300);
    delay(DENIED_DELAY);
 //   noTone(BUZZER);
    lcd.clear();
    lcd.print("Access denied");
    lcd.setCursor(0, 1);
    lcd.print("");
}
}

Show us the compiler output - specifically RAM usage, copy from the IDE, and post in code tags.

UNO has limited RAM, maybe try FLASH or an SD bard to store the card IDs.

read about the Arduino F-Makro

and use it for all your fixtext in Serial print and LCD print

After your modification please post again your new sketch and the compiler output.
If the modification was not enough, we can give you further hints regarding the storage of the UID.

That’s likely your issue

Store the cards in binary format (4 bytes not text) in flash using PROGMEM

1 Like

Using char makes each card 64 bytes 8 charachters times 8 bytes) but you only use hexadecimal thus you can fit each character into 4 bytes. you can keep the data type char* if that fits your needs better just make sure you declare each card with 0xcccccccc (c means character like 0xA1651B1C for the first card) this will half the required space for each card and you should be able to fit 83 x 3 = 166 cards which should be sufficient. using progmem might slow it down depending on the architecture so if this works for you stick with it.

The value is already returned in bytes that makes it easier for you.

modify the code as follows:


uint32_t content = 0; // 32bit variable that will hold the current card

for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); //debug
    Serial.print(mfrc522.uid.uidByte[i], HEX);                //debug
    content |= (mfrc522.uid.uidByte[i] << (i * 8)); // add each consecutive byte with 8 bit offset
  }
// check this content with your list with a for loop (can be optimised)

This both discards strings and is easier.
your for loop to check could be as follows:

int elementCount = sizeof(authorizedUIDs) / sizeof(authorizedUIDs[0]); // returns total number of elements in array

for(int i = 0; i < elementCount; i++)
{
    if(authorizedUIDs[i] == content) 
    {
        isAuthorized = true;
        break;
    }
}

Cheers,
Arda

I was more thinking about an array in PROGMEM like this

const uint8_t authorizedUIDs[][4] PROGMEM = {
  {0xA1, 0x65, 0x1B, 0x1C},
  {0x81, 0xEC, 0x1A, 0x1C},
  {0x46, 0x78, 0x4B, 0x40},
  {0xB1, 0xE8, 0x1D, 0x1C},
  {0x46, 0x4A, 0x34, 0x40},
  {0xD6, 0x37, 0x4B, 0x40},
  {0x56, 0x64, 0x41, 0x40},
  {0xD0, 0x99, 0x73, 0x40},
  {0x62, 0x05, 0x57, 0xC0},
  {0xF0, 0x89, 0xA5, 0x40},
  {0xA0, 0x79, 0x86, 0x40},
  {0xB0, 0x07, 0x71, 0x40},
  {0xC1, 0x3D, 0x1B, 0x1C},
  {0x46, 0xA9, 0x41, 0x40},
  {0x66, 0x74, 0x35, 0x40},
  {0x01, 0x23, 0x1D, 0x1C},
  {0xD0, 0xC2, 0x4B, 0xE9},
  {0x21, 0xAF, 0x21, 0x1C},
  {0xC1, 0xCA, 0x19, 0x1C},
  {0x01, 0x74, 0x22, 0x1C},
  {0x51, 0x6E, 0x1D, 0x1C},
  {0x41, 0x1E, 0x20, 0x1C},
  {0x31, 0x04, 0x1F, 0x1C},
  {0x96, 0x12, 0x3B, 0x40},
  {0x76, 0xAB, 0x41, 0x40},
  {0x96, 0x99, 0x3A, 0x40},
  {0x96, 0x5A, 0x35, 0x40},
  {0xC6, 0x43, 0x3C, 0x40},
  {0x36, 0x83, 0x4B, 0x40},
  {0x46, 0x65, 0x41, 0x40},
  {0x46, 0x39, 0x4C, 0x40},
  {0x16, 0xAF, 0xC8, 0xB4},
  {0x06, 0x7F, 0x41, 0x40},
  {0x26, 0xBF, 0x3C, 0x40},
  {0x46, 0x38, 0x34, 0x40},
  {0x00, 0x6D, 0x8D, 0x40},
  {0x20, 0x58, 0x6E, 0xE9},
  {0xE6, 0x0C, 0x4B, 0x40},
  {0x72, 0x93, 0xCB, 0x9A},
  {0xF6, 0x65, 0x3D, 0x40},
  {0xF6, 0x3C, 0x35, 0x40},
  {0x26, 0x61, 0x3B, 0x40},
  {0x36, 0x9F, 0x34, 0x40},
  {0x32, 0x31, 0xE7, 0x91},
  {0xD2, 0x39, 0xDC, 0xBF},
  {0xF6, 0x0B, 0x39, 0x40},
  {0x51, 0x28, 0x1C, 0x1C},
  {0xF0, 0x58, 0xAB, 0x40},
  {0x11, 0xA6, 0x22, 0x1C},
  {0x40, 0x50, 0x84, 0x40},
  {0xE2, 0xAE, 0xE0, 0xBF},
  {0xA6, 0x37, 0x35, 0x40},
  {0x56, 0xAE, 0x41, 0x40},
  {0xC6, 0x1E, 0x3A, 0x40},
  {0xE6, 0xE6, 0x39, 0x40},
  {0x06, 0x66, 0x36, 0x40},
  {0x96, 0x0E, 0x38, 0x40},
  {0x86, 0x51, 0x3B, 0x40},
  {0xC6, 0xB6, 0x40, 0x40},
  {0x66, 0xEB, 0x3C, 0x40},
  {0x70, 0xD0, 0x76, 0x40},
  {0xE1, 0xC7, 0x20, 0x1C},
  {0xC1, 0x91, 0x20, 0x1C},
  {0x90, 0xE7, 0x52, 0xE9},
  {0xF6, 0xE0, 0x3A, 0x40},
  {0xB0, 0x7E, 0x66, 0xE9},
  {0x50, 0xD6, 0x70, 0x40},
  {0x20, 0x3D, 0x6C, 0xE9},
  {0x96, 0x3D, 0x48, 0x40},
  {0x94, 0x6F, 0x78, 0x6B},
  {0xF6, 0x12, 0x38, 0x40},
  {0xC0, 0x68, 0x91, 0x40},
  {0x11, 0x5B, 0x19, 0x1C},
  {0xE1, 0xC0, 0x1C, 0x1C},
  {0x10, 0x79, 0x6A, 0xE9},
  {0x90, 0xB8, 0x68, 0x40},
  {0x50, 0x86, 0x86, 0x40},
  {0x62, 0x0C, 0x95, 0x91},
  {0x46, 0x76, 0x4C, 0x40},
  {0xB0, 0x40, 0xB0, 0x40},
  {0xD0, 0x2C, 0x53, 0xE9},
  {0x41, 0x52, 0x20, 0x1C},
  {0xF0, 0x61, 0x78, 0x40},
  {0x90, 0xE6, 0xAE, 0x40},
  {0xF2, 0x7E, 0xC5, 0x91},
  {0x66, 0x0C, 0x37, 0x40},
  {0x66, 0x20, 0x37, 0x40},
  {0x26, 0x3D, 0x4B, 0x40},
  {0xE1, 0x44, 0xDC, 0x06},
  {0xA0, 0x5E, 0x4F, 0xE9},
  {0x96, 0x2E, 0x3D, 0x40},
  {0x53, 0x1F, 0xA1, 0x98},
  {0xE0, 0x1D, 0x8B, 0x40},
  {0x46, 0x9D, 0x41, 0x40},
  {0xD0, 0xE0, 0x53, 0xE9},
  {0x70, 0x9E, 0x83, 0x40},
  {0xD1, 0x3D, 0x1C, 0x1C},
  {0x11, 0xAB, 0x20, 0x1C},
  {0xF3, 0xA6, 0xA8, 0xBB}
};

as representing the 4 bytes code as 0xA1651B1C is endianness dependant some might lead to issues depending on how the mfrc522.uid.uidByte are laid out and the compiler works.

then the code for comparison could just be with the mfrc522.uid.uidByte array and it's a simple for loop mach.

something like this 0xA1651B1C is endianness dependant

const uint8_t authorizedUIDs[][4] PROGMEM = {
  {0xA1, 0x65, 0x1B, 0x1C},
  {0x81, 0xEC, 0x1A, 0x1C},
  {0x46, 0x78, 0x4B, 0x40},
  {0xB1, 0xE8, 0x1D, 0x1C},
  {0x46, 0x4A, 0x34, 0x40},
  {0xD6, 0x37, 0x4B, 0x40},
  {0x56, 0x64, 0x41, 0x40},
  {0xD0, 0x99, 0x73, 0x40},
  {0x62, 0x05, 0x57, 0xC0},
  {0xF0, 0x89, 0xA5, 0x40},
  {0xA0, 0x79, 0x86, 0x40},
  {0xB0, 0x07, 0x71, 0x40},
  {0xC1, 0x3D, 0x1B, 0x1C},
  {0x46, 0xA9, 0x41, 0x40},
  {0x66, 0x74, 0x35, 0x40},
  {0x01, 0x23, 0x1D, 0x1C},
  {0xD0, 0xC2, 0x4B, 0xE9},
  {0x21, 0xAF, 0x21, 0x1C},
  {0xC1, 0xCA, 0x19, 0x1C},
  {0x01, 0x74, 0x22, 0x1C},
  {0x51, 0x6E, 0x1D, 0x1C},
  {0x41, 0x1E, 0x20, 0x1C},
  {0x31, 0x04, 0x1F, 0x1C},
  {0x96, 0x12, 0x3B, 0x40},
  {0x76, 0xAB, 0x41, 0x40},
  {0x96, 0x99, 0x3A, 0x40},
  {0x96, 0x5A, 0x35, 0x40},
  {0xC6, 0x43, 0x3C, 0x40},
  {0x36, 0x83, 0x4B, 0x40},
  {0x46, 0x65, 0x41, 0x40},
  {0x46, 0x39, 0x4C, 0x40},
  {0x16, 0xAF, 0xC8, 0xB4},
  {0x06, 0x7F, 0x41, 0x40},
  {0x26, 0xBF, 0x3C, 0x40},
  {0x46, 0x38, 0x34, 0x40},
  {0x00, 0x6D, 0x8D, 0x40},
  {0x20, 0x58, 0x6E, 0xE9},
  {0xE6, 0x0C, 0x4B, 0x40},
  {0x72, 0x93, 0xCB, 0x9A},
  {0xF6, 0x65, 0x3D, 0x40},
  {0xF6, 0x3C, 0x35, 0x40},
  {0x26, 0x61, 0x3B, 0x40},
  {0x36, 0x9F, 0x34, 0x40},
  {0x32, 0x31, 0xE7, 0x91},
  {0xD2, 0x39, 0xDC, 0xBF},
  {0xF6, 0x0B, 0x39, 0x40},
  {0x51, 0x28, 0x1C, 0x1C},
  {0xF0, 0x58, 0xAB, 0x40},
  {0x11, 0xA6, 0x22, 0x1C},
  {0x40, 0x50, 0x84, 0x40},
  {0xE2, 0xAE, 0xE0, 0xBF},
  {0xA6, 0x37, 0x35, 0x40},
  {0x56, 0xAE, 0x41, 0x40},
  {0xC6, 0x1E, 0x3A, 0x40},
  {0xE6, 0xE6, 0x39, 0x40},
  {0x06, 0x66, 0x36, 0x40},
  {0x96, 0x0E, 0x38, 0x40},
  {0x86, 0x51, 0x3B, 0x40},
  {0xC6, 0xB6, 0x40, 0x40},
  {0x66, 0xEB, 0x3C, 0x40},
  {0x70, 0xD0, 0x76, 0x40},
  {0xE1, 0xC7, 0x20, 0x1C},
  {0xC1, 0x91, 0x20, 0x1C},
  {0x90, 0xE7, 0x52, 0xE9},
  {0xF6, 0xE0, 0x3A, 0x40},
  {0xB0, 0x7E, 0x66, 0xE9},
  {0x50, 0xD6, 0x70, 0x40},
  {0x20, 0x3D, 0x6C, 0xE9},
  {0x96, 0x3D, 0x48, 0x40},
  {0x94, 0x6F, 0x78, 0x6B},
  {0xF6, 0x12, 0x38, 0x40},
  {0xC0, 0x68, 0x91, 0x40},
  {0x11, 0x5B, 0x19, 0x1C},
  {0xE1, 0xC0, 0x1C, 0x1C},
  {0x10, 0x79, 0x6A, 0xE9},
  {0x90, 0xB8, 0x68, 0x40},
  {0x50, 0x86, 0x86, 0x40},
  {0x62, 0x0C, 0x95, 0x91},
  {0x46, 0x76, 0x4C, 0x40},
  {0xB0, 0x40, 0xB0, 0x40},
  {0xD0, 0x2C, 0x53, 0xE9},
  {0x41, 0x52, 0x20, 0x1C},
  {0xF0, 0x61, 0x78, 0x40},
  {0x90, 0xE6, 0xAE, 0x40},
  {0xF2, 0x7E, 0xC5, 0x91},
  {0x66, 0x0C, 0x37, 0x40},
  {0x66, 0x20, 0x37, 0x40},
  {0x26, 0x3D, 0x4B, 0x40},
  {0xE1, 0x44, 0xDC, 0x06},
  {0xA0, 0x5E, 0x4F, 0xE9},
  {0x96, 0x2E, 0x3D, 0x40},
  {0x53, 0x1F, 0xA1, 0x98},
  {0xE0, 0x1D, 0x8B, 0x40},
  {0x46, 0x9D, 0x41, 0x40},
  {0xD0, 0xE0, 0x53, 0xE9},
  {0x70, 0x9E, 0x83, 0x40},
  {0xD1, 0x3D, 0x1C, 0x1C},
  {0x11, 0xAB, 0x20, 0x1C},
  {0xF3, 0xA6, 0xA8, 0xBB}
};

constexpr size_t numAuthorizedUIDs = sizeof authorizedUIDs / sizeof * authorizedUIDs;

bool compareArrays(const uint8_t* array1, const byte* array2, size_t size) {
  for (size_t i = 0; i < size; i++) {
    if (pgm_read_byte(&array1[i]) != array2[i]) {
      return false;
    }
  }
  return true;
}

void setup() {
  Serial.begin(115200);

  bool found = false;
  size_t foundIndex = 0;
  // if a card is detected
  // if (mfrc522.uid.size == 4) { // in real code you would ensure the size is 4

  uint8_t mfrc522_uid_uidByte[] = {0xD0, 0xE0, 0x53, 0xE9}; // this would be mfrc522.uid.uidByte in your code

  for (size_t i = 0; i < numAuthorizedUIDs; i++) {
    if (compareArrays(authorizedUIDs[i], mfrc522_uid_uidByte, 4)) {
      foundIndex = i;
      found = true;
      break; // Match found, exit the loop
    }
  }

  if (found) {
    Serial.print("Match found at index: ");
    Serial.println(foundIndex);
  } else {
    Serial.println("No match found");
  }

  // }
}

void loop() {}

1 Like

Thanks Alot Realy It Save Some Memory

Glad if that helped

Post your modified code taking this into account !

1 Like

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