Arduino UNO Based RFID Access Control System with Servo Gate Operation
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to interface with an RFID reader module to control access through a gate system. It includes two Arduino UNO microcontrollers, each connected to an EM-18 RFID reader module, a buzzer, and two LEDs (one red and one green). Additionally, there are two servo motors that act as gate actuators. The system reads RFID tags, checks for authorization, and then controls the gates accordingly. Unauthorized access attempts trigger a visual and audible alarm.
Component List
Microcontrollers
- Arduino UNO: A microcontroller board based on the ATmega328P. It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.
RFID Reader Modules
- EM-18 RFID reader module: A module that can read RFID tags at a frequency of 125 kHz. It has pins for power (VCC, GND), a buzzer (BUZZ), and serial communication (TX).
Actuators
- Servo: A rotary actuator that allows for precise control of angular position. It has three pins: power (VCC), ground (GND), and control (PWM).
Indicators
- LED: Two Pin (red): A red light-emitting diode with an anode and cathode pin.
- LED: Two Pin (green): A green light-emitting diode with an anode and cathode pin.
- Buzzer: An audible signaling device with a pin for signal input and a ground pin.
Wiring Details
Arduino UNO
- 5V: Supplies power to the VCC pins of the EM-18 RFID reader modules and servo motors.
- GND: Connected to the ground pins of all components.
- A4: Not connected in this circuit.
- A5: Not connected in this circuit.
- D2: Connected to the TX pin of the EM-18 RFID reader module for serial communication.
- D4: Controls the PWM pin of a servo motor.
- D6: Connected to the anode of a green LED.
- D8: Connected to the anode of a red LED.
- D10: Connected to the pin of a buzzer.
EM-18 RFID Reader Module
- VCC: Powered by the 5V output from the Arduino UNO.
- GND: Connected to the ground.
- TX: Transmits the RFID tag data to the Arduino UNO's D2 pin.
- BUZZ: Not connected in this circuit.
Servo
- VCC: Powered by the 5V output from the Arduino UNO.
- GND: Connected to the ground.
- PWM: Receives pulse-width modulation signals from the D4 pin of the Arduino UNO.
LED: Two Pin (red/green)
- Anode: Connected to the D6 (green) or D8 (red) pins on the Arduino UNO.
- Cathode: Connected to the ground.
Buzzer
- PIN: Receives signals from the D10 pin on the Arduino UNO.
- GND: Connected to the ground.
Documented Code
Arduino UNO Code (for both instances)
#include <SoftwareSerial.h>
#include <Servo.h> // Include Servo library
#define EM18_TX_PIN 2 // Connect to the TX pin of the EM18 RFID reader
SoftwareSerial rfidReader(EM18_TX_PIN, -1); // EM18 RFID reader (TX only)
// Servo motor setup
Servo gate_1Servo;
Servo gate_2Servo;
// Struct to hold entry information for each tag
struct TagEntry {
String tagValue;
unsigned long entryTime;
bool entryActive;
};
TagEntry tagEntries[10]; // Array to store entries for up to 10 tags
int currentTagCount = 0; // Current number of tags stored
const unsigned long MIN_TIME_GAP = 10000; // Minimum time gap in milliseconds
// List of authorized tags
String authorizedTags[] = {"4D008C7710A6", "4D0089D37166", "4D0089DAF9E7", "4D0089D66072", "4D0089051706"};
int numAuthorizedTags = 5; // Number of authorized tags
int gate_1Servopin = 4; // Pin to control the servo motor
int gate_2Servopin = 12; // Pin to control the servo motor
int led1 = 6;
int led2 = 8;
int buz = 10;
void setup() {
Serial.begin(9600);
rfidReader.begin(9600); // Start RFID reader communication
Serial.println("Initializing...");
Serial.println("Place your tag near the RFID reader...");
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buz, OUTPUT);
// Attach the servo motor
gate_1Servo.attach(gate_1Servopin);
gate_1Servo.write(0); // Start with the gate closed (0 degrees)
gate_2Servo.attach(gate_2Servopin);
gate_2Servo.write(0); // Start with the gate closed (0 degrees)
}
void loop() {
// Read the RFID tag from the reader
String tagValue = readRFID(rfidReader);
// Only process the tag if it has the correct length (12 characters)
if (tagValue.length() == 12) {
Serial.print("Tag detected: ");
Serial.println(tagValue);
if (isAuthorized(tagValue)) {
// Proceed only if the tag is authorized
Serial.println("Tag is authorized.");
int index = getTagIndex(tagValue); // Get index of the tag
if (index != -1) { // If tag is already stored
unsigned long currentTime = millis();
if (tagEntries[index].entryActive) {
// Check if minimum time gap has passed for exit
if (currentTime - tagEntries[index].entryTime >= MIN_TIME_GAP) {
// Calculate time difference
unsigned long timeSpent = (currentTime - tagEntries[index].entryTime) / 1000; // Time in seconds
Serial.print("Time spent between entry and exit: ");
Serial.print(timeSpent);
Serial.println(" seconds.");
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
Serial.println("Exit scan recorded.");
// Reset entry data for this tag
resetTagData(index);
// Open the gate for exit
openGate_2();
} else {
Serial.println("Exit rejected. Minimum time required between scans.");
}
} else {
Serial.println("Exit scan recorded.");
tagEntries[index].entryActive = true; // Mark as entered
tagEntries[index].entryTime = currentTime; // Update entry time
}
} else { // New tag scan or new entry for the same tag
Serial.println("New entry scan recorded.");
addTagEntry(tagValue); // Add new tag entry
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
// Open the gate for new entry
openGate_1();
}
} else {
// If the tag is not authorized
Serial.println("Unauthorized tag detected. Access denied.");
digitalWrite(led2, HIGH);
digitalWrite(buz, HIGH);
delay(2000);
digitalWrite(led2, LOW);
digitalWrite(buz, LOW);
}
}
}
// Function to read RFID data from the reader
String readRFID(SoftwareSerial &rfidReader) {
String tagValue = "";
char c;
while (rfidReader.available()) {
c = rfidReader.read();
if (c == '\r') { // RFID tag ends with carriage return '\r'
break; // Stop reading when the end of the tag is reached
}
tagValue += c; // Append character to the tagValue string
}
// If tagValue is not empty, print it for debugging
if (tagValue.length() > 0) {
//Serial.print("Raw tag data: ");
//Serial.println(tagValue);
} else {
//Serial.println("No data received from RFID reader.");
delay(100);
}
// Return the full tag value
return tagValue;
}
// Function to reset the data after entry and exit are completed
void resetTagData(int index) {
Serial.print("Resetting data for tag: ");
Serial.println(tagEntries[index].tagValue);
tagEntries[index].tagValue = "";
tagEntries[index].entryTime = 0;
tagEntries[index].entryActive = false;
}
// Function to check if the tag is authorized
bool isAuthorized(String tagValue) {
for (int i = 0; i < numAuthorizedTags; i++) {
if (tagValue == authorizedTags[i]) {
return true; // Tag is authorized
}
}
return false; // Tag is not authorized
}
// Function to add