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!
Overview
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.
Components
What You Need
All 7 components are available virtually in Wokwi — no hardware purchase needed to get started.
Wiring
Circuit Connections
The MFRC522 uses SPI (4 wires + SS + RST). Always power it from 3.3V — never 5V!
MFRC522 → Arduino (SPI)
| MFRC522 Pin | Arduino Pin | Function | Wire |
|---|---|---|---|
| SDA (SS) | Pin 10 | Chip Select | 🟠 Orange |
| SCK | Pin 13 | SPI Clock | 🟢 Green |
| MOSI | Pin 11 | Master Out Slave In | 🔵 Blue |
| MISO | Pin 12 | Master In Slave Out | 🟢 Green |
| RST | Pin 9 | Reset signal | 🟢 Green |
| GND | GND | Ground | ⬛ Black |
| 3.3V | 3.3V | ⚠️ 3.3V only! | 🔴 Red |
| IRQ | — | Not connected | — |
Instructions
Step-by-Step Guide
Follow these 7 steps to get your RFID gate running in Wokwi.
-
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.
-
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.
-
Install the MFRC522 Library
Click the Library Manager 📚 icon in the Wokwi sidebar. Search for
MFRC522and add the version by GithubCommunity. The SPI and Servo libraries are built into Arduino — no installation needed. -
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. -
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). -
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.
-
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!
Testing
Test Cards & Expected Output
Use these UIDs in Wokwi to test both access scenarios.
authorizedUID[] in the codeauthorizedUID[] to a 2D array and loop through all UIDs in isAuthorized() to support multiple cardholders.Diagram JSON
Wokwi diagram.json
Paste this into the diagram.json tab in Wokwi. Auto-places and wires all 8 components with correct connections.
diagram.json tab → Select all → Delete2. Paste this JSON — all parts and wires appear automatically
3. RFID module is at address
10 (SDA/SS), RST at 94. Resistors are 270Ω (compatible with both 3.3V and 5V LEDs)
{
"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": {}
}
Source Code
Arduino Sketch
Paste this into the sketch.ino tab in Wokwi. Change authorizedUID[] to your real card's bytes for physical hardware.
#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); }
Explanation
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.
Help
Troubleshooting Tips
| Problem | Solution |
|---|---|
| RFID not detected | Check SPI connections: SDA→10, SCK→13, MOSI→11, MISO→12, RST→9 |
| Module damaged / no response | Confirm MFRC522 is powered from 3.3V NOT 5V |
| Wrong card reads as authorized | Print the UID via Serial Monitor and update authorizedUID[] |
| Servo not moving | Check signal wire to Pin 6 and that servo VCC is connected to 5V |
| LED always ON or OFF | Verify resistor connections and that cathode goes to GND |
| MFRC522 library error | Ensure you installed MFRC522 by GithubCommunity (not other versions) |
Education
Comments
Post a Comment