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

RFID-Enabled Automated Gate System with Ultrasonic Vehicle Detection

View and Edit Project In Cirkit DesignerImage of RFID-Enabled Automated Gate System with Ultrasonic Vehicle Detection

Circuit Documentation

Summary

This circuit is designed to interface an RFID-RC522 module with an Arduino UNO for access control, utilizing servo motors for gate operation and an HC-SR04 Ultrasonic Sensor for vehicle detection. The RFID reader checks for authorized access, and upon verification, activates a servo to open a gate. The ultrasonic sensor is used to detect the presence of a vehicle at the exit gate, which triggers another servo to allow the vehicle to exit.

Component List

RFID-RC522

Arduino UNO

Servo (2x)

HC-SR04 Ultrasonic Sensor

Wiring Details

RFID-RC522

Arduino UNO

Servo (Gate)

Servo (Exit)

HC-SR04 Ultrasonic Sensor

Documented Code

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
Servo gateServo;
Servo exitServo;

// Ultrasonic Sensor pins
const int trigPin = 7;
const int echoPin = 8;
const long maxDistance = 30; // Maximum distance to detect vehicle (in cm)

String authorizedCard = "1234567890"; // Replace this with the actual card UID

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();

  gateServo.attach(6); // Servo pin for gate
  exitServo.attach(5); // Servo pin for exit
  gateServo.write(0); // Initial position (closed gate)
  exitServo.write(0); // Initial position

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  Serial.println("Place your card near the RFID reader...");
}

void loop() {
  // Look for new cards
  if ( !rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial() )
    return;

  String cardID = "";
  for (byte i = 0; i < rfid.uid.size; i++) {
    cardID += String(rfid.uid.uidByte[i], HEX);
  }

  Serial.print("Card UID: ");
  Serial.println(cardID);

  // Check if card is authorized
  if (cardID.equalsIgnoreCase(authorizedCard)) {
    openGate();
  } else {
    Serial.println("Unauthorized card.");
  }

  // Check for exiting vehicle
  long distance = readUltrasonicDistance();
  if (distance > 0 && distance < maxDistance) {
    openExitGate();
  }

  rfid.PICC_HaltA(); // Halt PICC
}

void openGate() {
  Serial.println("Authorized card detected! Opening gate...");
  gateServo.write(90); // Open the gate by rotating the servo to 90 degrees
  delay(3000); // Keep gate open for 3 seconds
  gateServo.write(0); // Close the gate
  Serial.println("Gate closed.");
}

void openExitGate() {
  Serial.println("Vehicle detected! Opening exit gate...");
  exitServo.write(90); // Open the exit gate by rotating the servo to 90 degrees
  delay(5000); // Keep exit gate open for 5 seconds
  exitServo.write(0); // Close the exit gate
  Serial.println("Exit gate closed.");
}

long readUltrasonicDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  long distance = (duration / 2) / 29.1; // Convert to cm

  return distance;
}

This code initializes the RFID reader and two servo motors, and sets up the ultrasonic sensor. It continuously checks for new RFID cards and compares the UID to an authorized UID. If the card is authorized, it opens the gate servo. It also checks the distance measured by the ultrasonic sensor to determine if a vehicle is exiting and opens the exit servo if a vehicle is detected.