Skip to main content

Arduino RFID Gate Access Control System (Wokwi Simulation Guide)

Arduino RFID Gate Access Control System – MFRC522 + Servo | MakeMindz
▶ Open Wokwi
🔐 Expert Project — Security & Automation

Arduino RFID Gate
Access Control System

Build a smart security gate using Arduino Uno, MFRC522 RFID module, servo motor, and LED indicators — fully simulated in Wokwi, no hardware needed!

🔐 Expert Level ⚡ Arduino Uno 📡 MFRC522 RFID ⚙️ Servo Motor 📶 SPI Protocol ⏱ 40 min
MFRC522 RFID
🔒 System Ready — Scan Card
🟢 Green LED🔲 Gate Servo🔴 Red LED
Uno · SPI pins 10–13Servo → Pin 6

What You'll Build

A real-world security system — scan an authorized RFID card to open a servo gate; unauthorized cards are rejected with a red LED alert.

⏱️
~40
Minutes to build
🔐
Expert
Difficulty level
📡
SPI
Communication
⚙️
3 sec
Gate open time
💡
Wokwi simulation included! Test this entire project in your browser at wokwi.com/projects/459456492902021121. Click the RFID module to simulate card scans and change the UID in component properties to test different cards.

What You Need

All 7 components are available virtually in Wokwi — no hardware purchase needed to get started.

Arduino Uno R3
MFRC522 RFID Module
Green LED (Access Granted)
Red LED (Access Denied)
270Ω Resistors × 2
Servo Motor (gate)
Mini Breadboard
📦
Library required: Install MFRC522 by GithubCommunity from the Wokwi Library Manager (book icon in the sidebar). The built-in SPI and Servo libraries need no installation.

Circuit Connections

The MFRC522 uses SPI (4 wires + SS + RST). Always power it from 3.3V — never 5V!

⚠️
Critical: Use 3.3V only for MFRC522! Powering the RFID module with 5V will permanently damage it. The Arduino's 3.3V pin provides the correct voltage.
📡

MFRC522 → Arduino (SPI)

MFRC522 PinArduino PinFunctionWire
SDA (SS)Pin 10Chip Select🟠 Orange
SCKPin 13SPI Clock🟢 Green
MOSIPin 11Master Out Slave In🔵 Blue
MISOPin 12Master In Slave Out🟢 Green
RSTPin 9Reset signal🟢 Green
GNDGNDGround⬛ Black
3.3V3.3V⚠️ 3.3V only!🔴 Red
IRQNot connected
Pin 4
Green LED Anode
via 270Ω resistor → Pin 4
Pin 5
Red LED Anode
via 270Ω resistor → Pin 5
GND
Both LED Cathodes
Connect to GND
Pin 6
Servo Signal (PWM)
Orange wire → Pin 6
5V
Servo VCC
Red wire → 5V
GND
Servo GND
Black wire → GND

Step-by-Step Guide

Follow these 7 steps to get your RFID gate running in Wokwi.

  1. Open the Wokwi Simulation

    Go to wokwi.com/projects/459456492902021121 to open the pre-built project, or start fresh at wokwi.com → New Project → Arduino Uno.

  2. Paste the diagram.json

    Click the diagram.json tab, select all existing text, and paste the JSON from the Diagram JSON section below. This auto-places and wires the MFRC522, servo, both LEDs, resistors, and breadboard.

  3. Install the MFRC522 Library

    Click the Library Manager 📚 icon in the Wokwi sidebar. Search for MFRC522 and add the version by GithubCommunity. The SPI and Servo libraries are built into Arduino — no installation needed.

  4. Paste the Arduino Code

    Open the sketch.ino tab and paste the full code from the Code section below. The authorized UID is pre-set to 0x11, 0x22, 0x33, 0x44 — you can change it to match any card.

  5. Click ▶ Start Simulation

    Press the green play button. The Serial Monitor should print RFID Gate System Ready. The servo initializes at 0° (gate closed) and both LEDs are off.

    Expected startup: Serial Monitor prints "RFID Gate System Ready" and servo arm stays at 0° (gate closed position).
  6. Simulate an RFID Card Scan

    Click the MFRC522 module in the Wokwi canvas. A popup appears — click "Scan card". The default UID is the authorized one, so the green LED lights up, servo rotates to 90°, and the gate closes after 3 seconds.

  7. Test an Unauthorized Card

    Click the MFRC522 module again → in the popup, change the UID to 01 02 03 04 → click Scan. The red LED lights up and Serial Monitor shows "Access Denied". Gate stays closed!


🔐 Test Your Security System Live

Scan authorized and unauthorized cards — watch the gate open and LEDs respond in real time.

Open Live Simulation

Test Cards & Expected Output

Use these UIDs in Wokwi to test both access scenarios.

✅ Authorized Card
11 22 33 44
Default UID — matches authorizedUID[] in the code
❌ Unauthorized Card
01 02 03 04
Any UID not matching the authorized array will be denied
✅ Access Granted
🟢 Green LED turns ON
⚙️ Servo rotates to 90° (gate opens)
💬 Serial: "Access Granted"
⏱️ Gate closes after 3 seconds
🔲 Green LED turns OFF
❌ Access Denied
🔴 Red LED turns ON for 2 sec
⚙️ Servo stays at 0° (gate closed)
💬 Serial: "Access Denied"
🔲 Red LED turns OFF after 2 sec
🔧
Add multiple authorized cards: Change authorizedUID[] to a 2D array and loop through all UIDs in isAuthorized() to support multiple cardholders.

Wokwi diagram.json

Paste this into the diagram.json tab in Wokwi. Auto-places and wires all 8 components with correct connections.

How to use in Wokwi
1. Click diagram.json tab → Select all → Delete
2. Paste this JSON — all parts and wires appear automatically
3. RFID module is at address 10 (SDA/SS), RST at 9
4. Resistors are 270Ω (compatible with both 3.3V and 5V LEDs)
diagram.json — Wokwi
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    // Mini breadboard for LED connections
    { "type": "wokwi-breadboard-mini", "id": "bb1",   "top": 142.6,  "left": 7.2,    "attrs": {} },
    // Arduino Uno
    { "type": "wokwi-arduino-uno",    "id": "uno",   "top": -18.6,  "left": 181.8,  "attrs": {} },
    // Red LED (Access Denied) — Pin 5
    { "type": "wokwi-led",           "id": "led1",  "top": 73.2,   "left": -34.6,  "attrs": { "color": "red"   } },
    // Green LED (Access Granted) — Pin 4
    { "type": "wokwi-led",           "id": "led2",  "top": -3.6,   "left": -34.6,  "attrs": { "color": "green" } },
    // Servo motor (gate) — Pin 6
    { "type": "wokwi-servo",         "id": "servo1","top": 170.8,  "left": 268.8,  "attrs": {} },
    // MFRC522 RFID module
    { "type": "board-mfrc522",       "id": "rfid1", "top": 150.95, "left": -73.36, "attrs": {} },
    // 270Ω resistor for Green LED (Pin 4)
    {
      "type": "wokwi-resistor", "id": "r1",
      "top": 23.15, "left": 28.8,
      "attrs": { "value": "270" }
    },
    // 270Ω resistor for Red LED (Pin 5)
    {
      "type": "wokwi-resistor", "id": "r2",
      "top": 80.75, "left": 19.2,
      "attrs": { "value": "270" }
    }
  ],
  "connections": [
    // LEDs — cathodes to GND
    [ "led2:C",     "uno:GND.1",  "green", ["v0"] ],
    [ "led1:C",     "uno:GND.1",  "green", ["v0"] ],

    // Servo motor
    [ "servo1:GND", "uno:GND.1",  "black", ["h0"] ],
    [ "servo1:V+",  "uno:5V",     "red",   ["h0"] ],
    [ "servo1:PWM", "uno:6",      "orange",["h0"] ],

    // MFRC522 RFID — SPI connections (MUST use 3.3V NOT 5V!)
    [ "rfid1:3.3V", "uno:3.3V",   "red",   ["h0"] ],
    [ "rfid1:GND",  "uno:GND.1",  "black", ["h0"] ],
    [ "rfid1:SDA",  "uno:10",     "purple",["h0"] ],
    [ "rfid1:SCK",  "uno:13",     "blue",  ["h0"] ],
    [ "rfid1:MOSI", "uno:11",     "green", ["h0"] ],
    [ "rfid1:MISO", "uno:12",     "yellow",["h0"] ],
    [ "rfid1:RST",  "uno:9",      "white", ["h0"] ],

    // LED resistors — anodes to resistors, resistors to Arduino pins
    [ "r2:1",       "led1:A",     "green", ["v0"] ],
    [ "r1:1",       "led2:A",     "green", ["v0"] ],
    [ "r2:2",       "uno:5",      "red",   ["v0"] ],
    [ "r1:2",       "uno:4",      "green", ["v0"] ]
  ],
  "dependencies": {}
}

Arduino Sketch

Paste this into the sketch.ino tab in Wokwi. Change authorizedUID[] to your real card's bytes for physical hardware.

sketch.ino — Arduino C++
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

// ── Pin Definitions ──
#define SS_PIN    10   // RFID SDA/SS → Pin 10
#define RST_PIN    9   // RFID Reset  → Pin 9
#define GREEN_LED  4   // Green LED   → Pin 4
#define RED_LED    5   // Red LED     → Pin 5
#define SERVO_PIN  6   // Servo PWM   → Pin 6

// ── Objects ──
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo   gateServo;

// ── Authorized Card UID — change to match your RFID card ──
byte authorizedUID[] = {0x11, 0x22, 0x33, 0x44};

// ─────────────── SETUP ───────────────
void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();

  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED,   OUTPUT);

  gateServo.attach(SERVO_PIN);
  gateServo.write(0);          // Start: gate closed

  Serial.println("RFID Gate System Ready");
  Serial.println("Authorized UID: 11 22 33 44");
  Serial.println("==========================");
}

// ─────────────── LOOP ────────────────
void loop() {
  // Wait for a new card to be presented
  if (!mfrc522.PICC_IsNewCardPresent()) return;
  if (!mfrc522.PICC_ReadCardSerial())   return;

  // Print scanned UID to Serial Monitor
  Serial.print("Card UID: ");
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    if (mfrc522.uid.uidByte[i] < 0x10) Serial.print("0");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    Serial.print(" ");
  }
  Serial.println();

  // Check authorization and respond
  if (isAuthorized()) {
    accessGranted();
  } else {
    accessDenied();
  }

  mfrc522.PICC_HaltA();         // Stop reading this card
}

// ─────────── UID COMPARISON ──────────
bool isAuthorized() {
  if (mfrc522.uid.size != 4) return false;

  for (byte i = 0; i < 4; i++) {
    if (mfrc522.uid.uidByte[i] != authorizedUID[i]) {
      return false;
    }
  }
  return true;
}

// ─────────── ACCESS GRANTED ──────────
void accessGranted() {
  Serial.println(">>> ACCESS GRANTED <<<");

  digitalWrite(GREEN_LED, HIGH);
  digitalWrite(RED_LED,   LOW);

  gateServo.write(90);          // Open gate to 90°
  delay(3000);                  // Hold gate open 3 seconds
  gateServo.write(0);           // Close gate
  delay(500);

  digitalWrite(GREEN_LED, LOW);
  Serial.println("Gate closed.");
}

// ─────────── ACCESS DENIED ───────────
void accessDenied() {
  Serial.println(">>> ACCESS DENIED <<<");

  digitalWrite(RED_LED, HIGH);
  delay(2000);                  // Flash red for 2 seconds
  digitalWrite(RED_LED, LOW);
}

How It Works

📡

SPI Communication with MFRC522

The MFRC522 uses SPI (Serial Peripheral Interface) — a 4-wire synchronous protocol. The Arduino is the Master; the RFID module is the Slave. MOSI carries data from Arduino to module, MISO carries data back, SCK synchronises the clock, and SDA (SS) selects which slave is active. SPI.begin() configures pins 11–13 automatically; the MFRC522 library handles all low-level protocol details.

🔑

UID Authentication

Every RFID card has a unique 4–7 byte Unique Identifier (UID) hardwired at manufacture. When a card is scanned, mfrc522.uid.uidByte[] holds its UID. The isAuthorized() function compares each byte against authorizedUID[]. If all 4 bytes match exactly, access is granted. To add more cards, turn authorizedUID into a 2D array and loop through all entries.

⚙️

Servo Gate Control

The Servo library generates PWM signals on Pin 6. gateServo.write(0) holds the gate closed (0°). On authorization, gateServo.write(90) rotates the arm to 90° — simulating a physical barrier lifting. After 3 seconds, gateServo.write(0) closes it. On real hardware, you'd attach a physical barrier arm to the servo horn.


Troubleshooting Tips

ProblemSolution
RFID not detectedCheck SPI connections: SDA→10, SCK→13, MOSI→11, MISO→12, RST→9
Module damaged / no responseConfirm MFRC522 is powered from 3.3V NOT 5V
Wrong card reads as authorizedPrint the UID via Serial Monitor and update authorizedUID[]
Servo not movingCheck signal wire to Pin 6 and that servo VCC is connected to 5V
LED always ON or OFFVerify resistor connections and that cathode goes to GND
MFRC522 library errorEnsure you installed MFRC522 by GithubCommunity (not other versions)

What You'll Learn

SPI communication protocol — 4-wire master/slave
RFID technology and UID authentication logic
Byte-by-byte array comparison in C++
Servo motor control with PWM signals
LED indicator circuits with current-limiting resistors
Security automation and access control logic
Using Arduino libraries (MFRC522, Servo, SPI)
Serial Monitor debugging for embedded systems

Comments

try for free