RFID-Enabled Automated Gate System with Ultrasonic Vehicle Detection
View and Edit Project In Cirkit Designer
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
- Description: An RFID module for reading RFID tags.
- Pins: VCC (3.3V), RST, GND, IRQ, MISO, MOSI, SCK, SDA.
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13.
Servo (2x)
- Description: A rotary actuator or linear actuator that allows for precise control of angular or linear position.
- Pins: GND, VCC, PWM.
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic ranging module that provides 2cm to 400cm non-contact measurement functionality.
- Pins: VCC, TRIG, ECHO, GND.
Wiring Details
RFID-RC522
- VCC (3.3V) connected to Arduino UNO 3.3V
- RST connected to Arduino UNO D9
- GND connected to Arduino UNO GND
- MISO connected to Arduino UNO D12
- MOSI connected to Arduino UNO D11
- SCK connected to Arduino UNO D13
- SDA connected to Arduino UNO D10
Arduino UNO
- 5V connected to Servo VCC (both servos) and HC-SR04 VCC
- GND connected to Servo GND (both servos) and HC-SR04 GND
- D8 connected to HC-SR04 ECHO
- D7 connected to HC-SR04 TRIG
- D6 connected to Servo (Gate) PWM
- D5 connected to Servo (Exit) PWM
Servo (Gate)
- VCC connected to Arduino UNO 5V
- GND connected to Arduino UNO GND
- PWM connected to Arduino UNO D6
Servo (Exit)
- VCC connected to Arduino UNO 5V
- GND connected to Arduino UNO GND
- PWM connected to Arduino UNO D5
HC-SR04 Ultrasonic Sensor
- VCC connected to Arduino UNO 5V
- TRIG connected to Arduino UNO D7
- ECHO connected to Arduino UNO D8
- GND connected to Arduino UNO GND
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.