RFID-Based Smart Traffic Control System Using Arduino Mega

RFID-Based Smart Traffic Control System Using Arduino Mega 2560 | MakeMindz
🚦 MakeMindz Tutorial

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.

🟦 Arduino Mega 2560 📡 MFRC522 ×4 🚦 4-Way Junction 🚑 Emergency Priority 🏙️ Smart City Ready
🕐
3–4 hrs
Build Time
Advanced
Difficulty
🛣️
4 Lanes
Managed

📋 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:

Lane 1 — North
▶ GREEN
Lane 2 — East
● RED
Lane 3 — South
● RED
Lane 4 — West
● RED

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

🔴
Red Phase
5 sec

Vehicle present, holding other lanes

🟡
Yellow Transition
2 sec

Safe changeover between signals

🟢
Green Phase
Active

Lane clear, vehicle passes through

🛒 Components Required

🟦
Arduino Mega 2560Main controller — 54 digital I/O pins
📡
MFRC522 RFID Modules ×4One per lane — SPI bus, shared RST
🏷️
RFID Cards / Key FobsFor vehicle simulation (13.56 MHz)
🔴
Red LEDs ×4Traffic signal red light per lane
🟡
Yellow LEDs ×4Traffic signal amber per lane
🟢
Green LEDs ×4Traffic signal green per lane
🔗
220Ω Resistors ×12Current limiting for all LEDs
🧩
Breadboard + Jumper WiresFor prototyping connections
🔋
5V / 12V Power SupplyUSB or external adapter

📌 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.

SignalMFRC522 PinArduino Mega PinNotes
SPI MOSIMOSI51Shared by all 4 modules
SPI MISOMISO50Shared by all 4 modules
SPI SCKSCK52Shared by all 4 modules
ResetRST49Shared RST line
SS — Lane 1SDA7Unique per module
SS — Lane 2SDA8Unique per module
SS — Lane 3SDA9Unique per module
SS — Lane 4SDA10Unique per module
Traffic LightRed PinYellow PinGreen Pin
Lane 1 (North)222324
Lane 2 (East)252627
Lane 3 (South)282930
Lane 4 (West)313233

🔌 Circuit Diagram

RFID Smart Traffic Control — Arduino Mega 2560 Circuit MakeMindz.com ARDUINO MEGA 2560 51 MOSI ● 50 MISO ● 52 SCK ● 49 RST ● 7 SS1 ● 8 SS2 ● 9 SS3 ● 10 SS4 ● ● 22 ● 23 ● 24 ● 25 ● 26 ● 27 ● 28 ● 29 ● 30 ● 31 ● 32 ● 33 ● GND MFRC522 #1 Lane 1 — North SS → Pin 7 MFRC522 #2 Lane 2 — East SS → Pin 8 MFRC522 #3 Lane 3 — South SS → Pin 9 MFRC522 #4 Lane 4 — West SS → Pin 10 LANE 1 Pins 22-24 LANE 2 Pins 25-27 LANE 3 Pins 28-30 LANE 4 Pins 31-33 SPI Bus: MOSI(51) MISO(50) SCK(52) Shared RST(49) — 220Ω on each LED 🚑 Emergency Priority Scan UID → Match emergency tag → Override all signals Wire Colours Red signal Yellow Green SPI MOSI SCK RST
💡
Key wiring rule: All MFRC522 modules share MOSI (51), MISO (50), SCK (52), and RST (49). Only the SS pin is unique per reader (7, 8, 9, 10). Place a 220Ω resistor in series with every LED.
⚠️
MFRC522 voltage: These modules run at 3.3V. When using an Arduino Mega, connect VCC to the 3.3V pin — NOT 5V — to avoid damaging the module.

🧪 Try the Simulation

🟢
Simulate on WokwiBest simulator for Arduino Mega + SPI — supports MFRC522 and multiple LED outputs
🔵
Simulate on TinkercadFree browser-based simulator — great for LED traffic light logic
🟠
Simulate on CirkitDesignerImport the diagram.json below to load the full circuit instantly

💻 Arduino Code

Install the MFRC522 library by miguelbalboa via Arduino IDE Library Manager before uploading.

rfid_traffic_control.ino
/*
 * 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

1
Gather and Test Components

Verify all four MFRC522 modules work individually before building the multi-reader setup. Test each with a basic card-read sketch first.

2
Wire the SPI Bus

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.

3
Connect Individual SS Pins

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.

4
Power MFRC522 at 3.3V

Connect all MFRC522 VCC pins to the Mega's 3.3V pin. Use a common GND rail. Do NOT use 5V.

5
Wire Traffic Light LEDs

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.

6
Install MFRC522 Library

In Arduino IDE: Tools → Manage Libraries → search MFRC522 → install the one by miguelbalboa.

7
Select Board & Port

Tools → Board → Arduino Mega or Mega 2560. Select the correct COM port. Upload the code.

8
Test with RFID Cards

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.

9
Add Emergency Vehicle Priority

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

📡 RFID Vehicle ID
🚑 Emergency Priority
⏱️ Dynamic Timing
🛣️ 4-Lane Control
🟡 Safe Yellow Transition
🏙️ Smart City Prototype
🎓 Exhibition Ready
📟 Serial Debug Output

🌍 Applications

  • 🏙️ Smart city traffic systems
  • 🚑 Emergency vehicle priority
  • 🛂 Automated toll monitoring
  • 📡 IoT transportation projects
  • 🎓 Final-year engineering project
  • 🏆 Competitions & exhibitions

🔗 Related MakeMindz Projects

© 2026 MakeMindz.com — Arduino, IoT & Robotics Tutorials

Built with ❤️ for makers, students & engineers

Comments

try for free