Automated Gate System Arduino

by 755478 in Circuits > Arduino

42 Views, 2 Favorites, 0 Comments

Automated Gate System Arduino

IMG_1137.png

The Smart Toll Tax System using RFID and Arduino is developed to automate and simplify the toll collection process, eliminating the need for manual intervention. Conventional toll booths often lead to traffic congestion and delays, especially during peak hours, and are vulnerable to human error. This system utilizes RFID technology integrated with an Arduino to create a contactless and efficient toll management solution. When a vehicle approaches the toll gate, the RFID reader scans its tag. If a valid RFID card is detected, the gate opens automatically, and the message “Access Granted” is displayed on the serial monitor. If the card is unauthorized or absent, the gate remains closed, and the message “Access Denied” is shown. This automated approach speeds up toll processing and ensures that only authorized vehicles are allowed access.


Access Granted:

  1. Displays "Access Granted" on the Serial monitor.
  2. Green led turns on signifying The keycard is valid.
  3. Servo motor turns 90 degrees up opening the "gate".

Access Denied:

  1. Displays "Access Denied" on the Serial monitor.
  2. Red led turns on signifying The keycard is invalid.
  3. Servo motor doesn't turn.


Supplies

IMG_1117.png

RC522 RFID Sensor - 1x - Link

RFID Cards/Tags

Ultrasonic Module Distance Sensor - 1x - Link

LED - 2x - Link

Servo motor - 1x - Link

Resistor 330 ohms - 2x - Link

Arduino uno - Link

Jumper Wires - Link

Breadboard - Link

Distance Sensor

IMG_1109.png

In order to calculate the amount of distance between an object and the sensor we can use a distance sensor.

It has 4 pins.

  1. Vcc - 5v
  2. grnd - ground
  3. trig - 2 *You can choose any pin make sure to change it in the code
  4. echo - 3 *You can choose any pin make sure to change it in the code

RFID SENSOR

IMG_1121.png
IMG_1114.png
  1. Vcc - 3.3V Postive
  2. Grnd - Negative
  3. RST/Reset RST - 9
  4. SPI SS SDA(SS) - 10
  5. SPI MOSI MOSI - 11
  6. SPI MISO MISO - 12
  7. SPI SCK SCK - 13


how to interface RFID reader with Arduino.

SERVO

IMG_1115.png
IMG_1120.png

Servo allows the gate to move 180 degrees down or up.

Pins:

  1. 5v
  2. Ground
  3. Signal - pin 5 (PWM)

Arduino servo motor tutorial

LIBRARY FOR RFID

To use the RFID you need to install the library for the card in order to get its UID number attached with the sensor.


METHOD 1:

Go to https://www.arduinolibraries.info/libraries/mfrc522.

  1. Download the library,
  2. Unzip it
  3. Drag it into your Arduino library folder
  4. Go into Arduino - File - Libraries - Scroll down until you find Dump Info


METHOD 2:

Put in this code to receive the id.


Run the code and make sure your RFID sensor is attached with the Arduino correctly.

Upload the code and put your keycard onto the sensor, once done it will display a 8 digit number sequence which you will copy and paste into the code where the comment says "Paste your id here".

Full code:

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

void setup(){
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Approximate your card to the reader...");
}

if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}

if (!mfrc522.PICC_ReadCardSerial()) {
return;
}

String content = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}

RFID LIBRARY EXPLANATION

Explanation of RFID Library:


Broken down code (Explained):


Reading the UID of an RFID Card:

Each RFID card or tag contains a unique identifier known as the UID. You can retrieve and display this UID using the MFRC522 library. The following code snippet demonstrates how to read the UID, which can then be used to create a list of authorized cards.


Required Libraries and Setup:

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

The code relies on the SPI library to use SPI communication and MFRC522 library to work with the RFID module.


  1. It declares SS_PIN and RST_PIN as the Slave Select and Reset pins of RFID module. Then an instance of mfrc522 object is created in order to control all the operations of RFID.


Setup:

In the setup () routine, the program starts by enabling serial communication and enabling SPI interface. It then makes a call to PCD_Init() to initialize the RFID reader. A bright message is presented to notify the user to bring an RFID tag to close to the reader.


void setup(){
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Approximate your card to the reader...");
}


Card detection:

Under the loop () method, the computer program first checks whether a new card in RFID using PICC_IsNewCardPresent (). In the case of the card not being found, the function leaves immediately to avoid any unnecessary processing.


}
if (!mfrc522.PICC_IsNewCardPresent()) {
return;


Card selection:

Once a card is detected, PICC_ReadCardSerial() function thus attempts to select it and read its unique identifier (UID). In case this does not happen, the function dies without proceeding further.


if (!mfrc522.PICC_ReadCardSerial()) {
return;
}


Displaying and processing the UID:

Here, the code creates a string variable called content with the conversion of UID into a readable hexadecimal pattern and displaying the content on the Serial Monitor. It traverses all the bytes of the UID and adds them to the content string. This number is the identifier of RFID card.


String content = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}


Control logic:

It is used by the program to ensure that the UID of the card being scanned is equal to a reserved one (BD 31 15 2B). When the UID ends up with the same pair, it prints Authorized access; otherwise it prints Access denied. In this section, you can modify the UID in order to reflect certain cards which you would like to authorize. The delay(3000) instruction pauses the program for 3Second before going on with it.


if (content.substring(1) == "BD 31 15 2B") {
Serial.println("Authorized access");
delay(3000);
} else {
Serial.println("Access denied");
delay(3000);
}


Explanation: RFID to Components

Distance sensor:

if (inch < 5) {
  1. This checks if an object (likely the RFID card or hand) is within 5 inches of the sensor.


  1. If true (i.e., close enough), the code proceeds to check the card’s UID.


UID ID Paste:

if (content.substring(1) == "45 5F AF 15") // Paste your id here
  1. This line compares the scanned UID (stored as a string in content) to a specific value ("45 5F AF 15").


  1. substring(1) skips the first character—often used to remove any leading whitespace or unwanted character.


  1. If the UID matches, the card is authorized.


Access authorized:

{
Serial.println("Authorized access");
digitalWrite(gled, HIGH);
delay(3000);
Serial.println();
  1. Prints "Authorized access" to the Serial Monitor.


  1. Turns on the green LED (gled) to indicate success.


  1. Waits 3 seconds with delay(3000).


  1. Prints a blank line (Serial.println();) for spacing/output formatting.


To move the Servo:

myServo.write(90);
delay(3000);
myServo.write(0);
digitalWrite(gled, LOW);
}


  1. Rotates the servo motor to 90 degrees—this likely unlocks a door or opens a barrier.


  1. Waits for 3 seconds.


  1. Then returns the servo to 0 degrees (initial position).


  1. Turns off the green LED.


Access denied:

else {
Serial.println(" Access denied");
delay(3000);
digitalWrite(rled, HIGH);
delay(1500);
digitalWrite(rled, LOW);
delay(1500);
digitalWrite(rled, HIGH);
}

If the UID does not match the authorized one:


  1. Prints "Access denied".


  1. Waits 3 seconds.


  1. Blinks the red LED (rled) once: ON for 1.5 seconds, OFF for 1.5 seconds, then ON again.


This visually indicates unauthorized access.


IF your too far away:

}
else {
Serial.println("GET CLOSER");
delay(3000);
}
}

If the object is not within 5 inches (inch >= 5):


  1. Displays "GET CLOSER" on the Serial Monitor.


  1. Waits 3 seconds before restarting the loop.


  1. This encourages the user to bring their RFID tag or hand closer to the reader/sensor.

Full CODE

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

#define SS_PIN 10
#define RST_PIN 9

#define trigPin 2
#define echoPin 3

Servo myServo; //Servo
long duration, inch;

int gled=7; //Green led
int rled=6; //Red led

MFRC522 mfrc522(SS_PIN, RST_PIN);

int calculateDistance() //Distance sensor
{
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin, HIGH);
inch = duration /74 /2;
return inch;
}

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

myServo.attach(5);
myServo.write(0);

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

pinMode(gled , OUTPUT);
pinMode(rled , OUTPUT);

Serial.println("Approximate your card to the reader...");
Serial.println();
}

void loop()
{
calculateDistance();
//Serial.println(inch);

if ( ! mfrc522.PICC_IsNewCardPresent()) // Look for new cards
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) // Select one of the cards
{
return;
}


Serial.print("UID tag :"); //Show UID on serial monitor
String content= "";
byte letter;

for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}

Serial.println();
Serial.print("Message : ");
content.toUpperCase();

if (inch<5){

if (content.substring(1) == "45 5F AF 15") //Paste your id here
{
Serial.println("Authorized access");
digitalWrite(gled, HIGH);
delay(3000);
Serial.println();

myServo.write(90);
delay(3000);
myServo.write(0);
digitalWrite(gled, LOW);
}
else {
Serial.println(" Access denied");
delay(3000);
digitalWrite(rled, HIGH);
delay(1500);
digitalWrite(rled, LOW);
delay(1500);
digitalWrite(rled, HIGH);
}
}
else {
Serial.println("GET CLOSER");
delay(3000);
}
}

Functionality of the Automated Gate System Using Arduino Uno

Once the hardware is assembled and the code uploaded, the Smart Toll Tax System is ready for operation. Here’s how it functions:

  1. RFID Card Scanning: When an RFID card is brought near the RFID reader, the system scans the card’s UID and checks it against a list of authorized entries.


  1. Authorized Card: If the UID matches one from the authorized list, the servo motor activates and opens the toll gate. The green LED turns on signifying "Access Granted". After a 3-second delay, the gate automatically closes.


  1. Unauthorized Card: If the UID does not match any authorized entries, the gate stays closed, and the red LED turns on signifying "Access Denied".


Video

Video