0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program for RFID Based Access Control System

An Embedded C program for RFID based access control reads the RFID tag ID using UART, compares it with authorized IDs using if statements, and grants or denies access by controlling an output pin; for example, if(tag_id == authorized_id) { grant_access(); }.
📋

Examples

InputRFID tag ID = 0x1A2B3C4D
OutputAccess Granted
InputRFID tag ID = 0xFFFFFFFF
OutputAccess Denied
InputRFID tag ID = 0x12345678
OutputAccess Granted
🧠

How to Think About It

To build an RFID based access control system, first read the RFID tag's unique ID from the reader via UART communication. Then compare this ID with a list of authorized IDs stored in the program. If the ID matches, activate an output (like a door lock relay) to grant access; otherwise, deny access by keeping the output off.
📐

Algorithm

1
Initialize UART communication to receive RFID data.
2
Wait and read the RFID tag ID from the reader.
3
Compare the received tag ID with stored authorized IDs.
4
If a match is found, activate the access control output (e.g., unlock door).
5
If no match, keep the access control output inactive (e.g., keep door locked).
6
Repeat the process for each new RFID tag scanned.
💻

Code

embedded_c
#include <stdio.h>
#include <string.h>

// Simulated function to read RFID tag ID
unsigned long read_rfid() {
    // In real hardware, this reads from UART
    return 0x1A2B3C4D; // Example tag ID
}

void grant_access() {
    printf("Access Granted\n");
    // Code to activate door lock relay here
}

void deny_access() {
    printf("Access Denied\n");
    // Code to keep door locked here
}

int main() {
    unsigned long authorized_ids[] = {0x1A2B3C4D, 0x12345678};
    unsigned long tag_id = read_rfid();
    int authorized = 0;

    for(int i = 0; i < 2; i++) {
        if(tag_id == authorized_ids[i]) {
            authorized = 1;
            break;
        }
    }

    if(authorized) {
        grant_access();
    } else {
        deny_access();
    }

    return 0;
}
Output
Access Granted
🔍

Dry Run

Let's trace the example RFID tag ID 0x1A2B3C4D through the code.

1

Read RFID Tag

tag_id = 0x1A2B3C4D

2

Compare with authorized IDs

Check 0x1A2B3C4D == 0x1A2B3C4D (true), set authorized = 1

3

Grant or Deny Access

authorized == 1, call grant_access()

4

Output

Print 'Access Granted'

IterationAuthorized IDMatch FoundAuthorized Flag
10x1A2B3C4DYes1
20x12345678No1
💡

Why This Works

Step 1: Reading RFID Tag

The program simulates reading an RFID tag ID using read_rfid(), which would normally get data from UART.

Step 2: Checking Authorization

It compares the read tag ID with stored authorized IDs using a for loop and if condition.

Step 3: Granting or Denying Access

If a match is found, grant_access() is called to unlock the door; otherwise, deny_access() keeps it locked.

🔄

Alternative Approaches

Use EEPROM to store authorized IDs
embedded_c
#include <stdio.h>
// EEPROM read simulation
unsigned long read_eeprom(int index) {
    unsigned long ids[] = {0x1A2B3C4D, 0x12345678};
    return ids[index];
}

unsigned long read_rfid() { return 0x12345678; }

int main() {
    unsigned long tag_id = read_rfid();
    int authorized = 0;
    for(int i=0; i<2; i++) {
        if(tag_id == read_eeprom(i)) {
            authorized = 1;
            break;
        }
    }
    if(authorized) printf("Access Granted\n");
    else printf("Access Denied\n");
    return 0;
}
This approach allows updating authorized IDs without recompiling code but requires EEPROM hardware.
Use a hash table for faster lookup
embedded_c
#include <stdio.h>
#include <stdbool.h>

bool check_authorized(unsigned long tag_id) {
    // Simple hash simulation for demo
    if(tag_id == 0x1A2B3C4D || tag_id == 0x12345678) return true;
    return false;
}

unsigned long read_rfid() { return 0xFFFFFFFF; }

int main() {
    unsigned long tag_id = read_rfid();
    if(check_authorized(tag_id)) printf("Access Granted\n");
    else printf("Access Denied\n");
    return 0;
}
Using a hash or direct checks can speed up authorization but is less flexible for many IDs.

Complexity: O(n) time, O(n) space

Time Complexity

The program checks each authorized ID once in a loop, so time grows linearly with the number of authorized IDs.

Space Complexity

Space is used to store authorized IDs in an array, proportional to the number of IDs.

Which Approach is Fastest?

Using a hash or direct checks is faster (O(1)) but less scalable; looping is simpler and flexible for small lists.

ApproachTimeSpaceBest For
Linear Search (Loop)O(n)O(n)Small to medium authorized lists
EEPROM StorageO(n)O(n)Persistent storage, easy updates
Hash Table / Direct ChecksO(1)O(1)Fast lookup, few IDs
💡
Always debounce RFID reads and verify tag data integrity before granting access.
⚠️
Beginners often forget to compare the full tag ID correctly or handle UART data properly, causing false denials.