Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Based RFID Access Control System with Servo Gate Operation

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based RFID Access Control System with Servo Gate Operation

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

RFID Reader Modules

Actuators

Indicators

Wiring Details

Arduino UNO

EM-18 RFID Reader Module

Servo

LED: Two Pin (red/green)

Buzzer

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