RFID-Based Smart Traffic Control System using Arduino Mega 2560
4-way intelligent intersection management with RFID vehicle detection, emergency priority signals, and dynamic green timing.
📋 Project Overview
This project builds an intelligent 4-way traffic intersection using the Arduino Mega 2560 and four MFRC522 RFID readers — one per lane. Vehicles carry RFID tags; when detected, the system adjusts signal timing dynamically. Emergency vehicles (ambulance, fire) receive immediate green priority, clearing all other lanes to red.
Live intersection state preview — only one lane is green at a time:
⚡ How the System Works
🔍 RFID Vehicle Detection
- ✔ Each lane has one MFRC522 reader
- ✔ Vehicles carry RFID tags
- ✔ Tag UID determines vehicle type
- ✔ Emergency vehicles get instant priority
🚦 Signal Control Logic
- ✔ Only one direction green at a time
- ✔ Yellow transition buffer (2 sec)
- ✔ Dynamic 5-sec active green phase
- ✔ Safe queue-based lane rotation
Signal Phase Timing
Vehicle present, holding other lanes
Safe changeover between signals
Lane clear, vehicle passes through
🛒 Components Required
📌 Complete Pin Connection Table
All MFRC522 modules share the same SPI bus (MOSI, MISO, SCK) but each has its own SS (Slave Select) pin. RST is shared across all four.
| Signal | MFRC522 Pin | Arduino Mega Pin | Notes |
|---|---|---|---|
| SPI MOSI | MOSI | 51 | Shared by all 4 modules |
| SPI MISO | MISO | 50 | Shared by all 4 modules |
| SPI SCK | SCK | 52 | Shared by all 4 modules |
| Reset | RST | 49 | Shared RST line |
| SS — Lane 1 | SDA | 7 | Unique per module |
| SS — Lane 2 | SDA | 8 | Unique per module |
| SS — Lane 3 | SDA | 9 | Unique per module |
| SS — Lane 4 | SDA | 10 | Unique per module |
| Traffic Light | Red Pin | Yellow Pin | Green Pin |
|---|---|---|---|
| Lane 1 (North) | 22 | 23 | 24 |
| Lane 2 (East) | 25 | 26 | 27 |
| Lane 3 (South) | 28 | 29 | 30 |
| Lane 4 (West) | 31 | 32 | 33 |
🔌 Circuit Diagram
🧪 Try the Simulation
💻 Arduino Code
Install the MFRC522 library by miguelbalboa via Arduino IDE Library Manager before uploading.
/* * RFID-Based Smart Traffic Control System * MakeMindz.com * * Board : Arduino Mega 2560 * Readers : 4× MFRC522 (SS: D7, D8, D9, D10 | RST: D49) * Lights : 12× LEDs (R/Y/G per lane on D22–D33) */ #include <SPI.h> #include <MFRC522.h> // ── RFID Pin Definitions ── #define RST_PIN 49 // Shared reset pin #define SS_PIN_1 7 // Lane 1 Slave Select #define SS_PIN_2 8 // Lane 2 Slave Select #define SS_PIN_3 9 // Lane 3 Slave Select #define SS_PIN_4 10 // Lane 4 Slave Select // ── Traffic Light Pin Definitions ── #define RED_LIGHT_1 22 #define YELLOW_LIGHT_1 23 #define GREEN_LIGHT_1 24 #define RED_LIGHT_2 25 #define YELLOW_LIGHT_2 26 #define GREEN_LIGHT_2 27 #define RED_LIGHT_3 28 #define YELLOW_LIGHT_3 29 #define GREEN_LIGHT_3 30 #define RED_LIGHT_4 31 #define YELLOW_LIGHT_4 32 #define GREEN_LIGHT_4 33 // ── MFRC522 Objects ── MFRC522 rfid1(SS_PIN_1, RST_PIN); MFRC522 rfid2(SS_PIN_2, RST_PIN); MFRC522 rfid3(SS_PIN_3, RST_PIN); MFRC522 rfid4(SS_PIN_4, RST_PIN); void setup() { Serial.begin(9600); SPI.begin(); // Initialise all four RFID readers rfid1.PCD_Init(); rfid2.PCD_Init(); rfid3.PCD_Init(); rfid4.PCD_Init(); // Set traffic light pins as output int lightPins[] = { RED_LIGHT_1, YELLOW_LIGHT_1, GREEN_LIGHT_1, RED_LIGHT_2, YELLOW_LIGHT_2, GREEN_LIGHT_2, RED_LIGHT_3, YELLOW_LIGHT_3, GREEN_LIGHT_3, RED_LIGHT_4, YELLOW_LIGHT_4, GREEN_LIGHT_4 }; for (int i = 0; i < 12; i++) { pinMode(lightPins[i], OUTPUT); } // Start all lanes on green (all clear) setTrafficLight(1, LOW, LOW, HIGH); setTrafficLight(2, LOW, LOW, HIGH); setTrafficLight(3, LOW, LOW, HIGH); setTrafficLight(4, LOW, LOW, HIGH); Serial.println("RFID Traffic System Ready"); } void loop() { // Poll each reader every cycle checkRFID(rfid1, 1); checkRFID(rfid2, 2); checkRFID(rfid3, 3); checkRFID(rfid4, 4); } // ── Check one RFID reader and control its lane ── void checkRFID(MFRC522 &rfid, int laneNumber) { if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) { return; // No card, exit early } Serial.print("Vehicle detected on Lane "); Serial.println(laneNumber); // Signal sequence: Red → (hold) → Yellow → Green setTrafficLight(laneNumber, HIGH, LOW, LOW); // Red ON delay(5000); // Hold 5 seconds setTrafficLight(laneNumber, LOW, HIGH, LOW); // Yellow (transition) delay(2000); // Yellow for 2 seconds setTrafficLight(laneNumber, LOW, LOW, HIGH); // Green (vehicle go) rfid.PICC_HaltA(); // Stop reading this card } // ── Set a specific lane's traffic light state ── void setTrafficLight(int lane, int red, int yellow, int green) { switch (lane) { case 1: digitalWrite(RED_LIGHT_1, red); digitalWrite(YELLOW_LIGHT_1, yellow); digitalWrite(GREEN_LIGHT_1, green); break; case 2: digitalWrite(RED_LIGHT_2, red); digitalWrite(YELLOW_LIGHT_2, yellow); digitalWrite(GREEN_LIGHT_2, green); break; case 3: digitalWrite(RED_LIGHT_3, red); digitalWrite(YELLOW_LIGHT_3, yellow); digitalWrite(GREEN_LIGHT_3, green); break; case 4: digitalWrite(RED_LIGHT_4, red); digitalWrite(YELLOW_LIGHT_4, yellow); digitalWrite(GREEN_LIGHT_4, green); break; } }
🛠️ Step-by-Step Build Instructions
Verify all four MFRC522 modules work individually before building the multi-reader setup. Test each with a basic card-read sketch first.
Connect MOSI (51), MISO (50), and SCK (52) as a shared daisy-chain across all four MFRC522 modules. Connect RST from all modules to Mega pin 49.
Wire each module's SDA (SS) pin separately: Lane 1 → D7, Lane 2 → D8, Lane 3 → D9, Lane 4 → D10. This is how the Arduino selects which reader to talk to.
Connect all MFRC522 VCC pins to the Mega's 3.3V pin. Use a common GND rail. Do NOT use 5V.
Connect the 12 LEDs (3 per lane) to pins D22–D33. Place a 220Ω resistor in series between each Arduino pin and the LED anode. Connect cathodes to GND.
In Arduino IDE: Tools → Manage Libraries → search MFRC522 → install the one by miguelbalboa.
Tools → Board → Arduino Mega or Mega 2560. Select the correct COM port. Upload the code.
Open Serial Monitor at 9600 baud. Scan an RFID card near each reader. The corresponding lane should cycle through Red → Yellow → Green. Check all 4 lanes work independently.
Read the UID of your emergency tag first. Then add a UID check inside checkRFID(): if the UID matches, set all other lanes to red immediately and give the emergency lane a long green phase.
✨ Key Features
🌍 Applications
- 🏙️ Smart city traffic systems
- 🚑 Emergency vehicle priority
- 🛂 Automated toll monitoring
- 📡 IoT transportation projects
- 🎓 Final-year engineering project
- 🏆 Competitions & exhibitions
Comments
Post a Comment