RFID-Based FASTag Toll Gate Simulator using Arduino Uno
A complete automated toll barrier system with RFID, servo gate, ultrasonic safety, and PC-based balance management.
Components and supplies
1
HC-SR04 Ultrasonic Range Finder
1
LCD I2C 16x2 Module
1
Passive Buzzer
1
SG90 Micro-servo motor
1
RFID MFRC522
Tools and machines
1
Utility Knife
1
Craft scissors
Apps and platforms
1
Arduino IDE
1
python IDE
Project description
Code
Windows Python Libraries Installation
powershell
To install the libraries using Command Prompt
1pip install pyserial
Arduino Code
cpp
To be uploaded in the Arduino IDE.
1#include <Wire.h> 2#include <Servo.h> 3#include <SPI.h> 4#include <MFRC522.h> 5#include <LiquidCrystal_I2C.h> 6 7#define SS_PIN 10 8#define RST_PIN 9 9#define SERVO_PIN 3 10#define TRIG_PIN 6 11#define ECHO_PIN 7 12#define BUZZER_PIN 5 13 14MFRC522 rfid(SS_PIN, RST_PIN); 15LiquidCrystal_I2C lcd(0x27, 16, 2); 16Servo gate; 17 18bool gateOpen = false; 19 20void setup() { 21 Serial.begin(9600); 22 SPI.begin(); 23 rfid.PCD_Init(); 24 lcd.init(); 25 lcd.backlight(); 26 pinMode(TRIG_PIN, OUTPUT); 27 pinMode(ECHO_PIN, INPUT); 28 pinMode(BUZZER_PIN, OUTPUT); 29 digitalWrite(BUZZER_PIN, HIGH); // Buzzer OFF (active-low) 30 gate.attach(SERVO_PIN); 31 closeGate(); 32 33 lcd.setCursor(0, 0); 34 lcd.print("Scan your card"); 35} 36 37void loop() { 38 if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return; 39 40 String uid = ""; 41 for (byte i = 0; i < rfid.uid.size; i++) { 42 uid += String(rfid.uid.uidByte[i], HEX); 43 } 44 uid.toUpperCase(); 45 46 lcd.clear(); 47 lcd.setCursor(0, 0); 48 lcd.print("UID: " + uid); 49 Serial.println(uid); 50 51 waitForResponse(uid); 52 53 rfid.PICC_HaltA(); 54} 55 56void waitForResponse(String uid) { 57 unsigned long startTime = millis(); 58 while (millis() - startTime < 3000) { 59 if (Serial.available()) { 60 String command = Serial.readStringUntil('\n'); 61 command.trim(); 62 if (command == "ALLOW") { 63 lcd.setCursor(0, 1); 64 lcd.print("Access Granted"); 65 soundBuzzer(1); 66 openGate(); 67 waitForCarToPass(); 68 closeGate(); 69 } else if (command == "DENY") { 70 lcd.setCursor(0, 1); 71 lcd.print("Access Denied"); 72 soundBuzzer(3); 73 } 74 return; 75 } 76 } 77 lcd.setCursor(0, 1); 78 lcd.print("No response"); 79} 80 81void waitForCarToPass() { 82 while (true) { 83 long duration, distance; 84 digitalWrite(TRIG_PIN, LOW); 85 delayMicroseconds(2); 86 digitalWrite(TRIG_PIN, HIGH); 87 delayMicroseconds(10); 88 digitalWrite(TRIG_PIN, LOW); 89 duration = pulseIn(ECHO_PIN, HIGH); 90 distance = duration * 0.034 / 2; 91 92 if (distance > 20) break; 93 delay(200); 94 } 95 delay(3000); 96} 97 98void openGate() { 99 for (int pos = 90; pos >= 0; pos--) { 100 gate.write(pos); 101 delay(10); 102 } 103 gateOpen = true; 104} 105 106void closeGate() { 107 for (int pos = 0; pos <= 90; pos++) { 108 gate.write(pos); 109 delay(10); 110 } 111 gateOpen = false; 112} 113 114void soundBuzzer(int times) { 115 for (int i = 0; i < times; i++) { 116 tone(BUZZER_PIN,1000); 117 delay(200); 118 noTone(BUZZER_PIN); 119 delay(200); 120 } 121}
Python Balance Management Software
python
To be uploaded in Python IDE
1import serial 2import time 3 4PORT = "COM3" # Change if needed 5BALANCE_FILE = "balances.txt" 6FARE = 20 7 8def load_balances(): 9 balances = {} 10 try: 11 with open(BALANCE_FILE, "r") as f: 12 for line in f: 13 uid, amount = line.strip().split(":") 14 balances[uid] = int(amount) 15 except FileNotFoundError: 16 pass 17 return balances 18 19def save_balances(balances): 20 with open(BALANCE_FILE, "w") as f: 21 for uid, amount in balances.items(): 22 f.write(f"{uid}:{amount}\n") 23 24def recharge(balances): 25 uid = input("Enter UID to recharge: ").strip().upper() 26 amt = int(input("Enter amount: ₹")) 27 balances[uid] = balances.get(uid, 0) + amt 28 save_balances(balances) 29 print(f"UID {uid} recharged. New balance: ₹{balances[uid]}") 30 31def main(): 32 balances = load_balances() 33 34 try: 35 ser = serial.Serial(PORT, 9600, timeout=3) 36 time.sleep(2) 37 print("Connected to Arduino.") 38 39 while True: 40 print("\nPress [Enter] to run, [r] to recharge, [q] to quit.") 41 opt = input(">>> ").strip().lower() 42 43 if opt == "q": 44 break 45 elif opt == "r": 46 recharge(balances) 47 continue 48 49 print("Waiting for card...") 50 uid = ser.readline().decode().strip().upper() 51 52 if uid == "": 53 print("No UID received.") 54 continue 55 56 print(f"Card scanned: {uid}") 57 58 if uid not in balances: 59 print("New card detected.") 60 balances[uid] = 0 61 save_balances(balances) 62 63 if balances[uid] >= FARE: 64 balances[uid] -= FARE 65 save_balances(balances) 66 print(f"Access granted. ₹{FARE} deducted. Balance: ₹{balances[uid]}") 67 ser.write(b"ALLOW\n") 68 else: 69 print(f"Access denied. Balance: ₹{balances[uid]}") 70 ser.write(b"DENY\n") 71 72 except serial.SerialException: 73 print("Serial connection failed. Check COM port.") 74 finally: 75 if 'ser' in locals() and ser.is_open: 76 ser.close() 77 78if __name__ == "__main__": 79 main()
Comments
Only logged in users can leave comments