Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Mega 2560 Emergency Traffic Control System with RFID and I2C LCD

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Emergency Traffic Control System with RFID and I2C LCD

Circuit Documentation

Summary

This circuit is an Emergency Traffic Control System designed using an Arduino Mega 2560 microcontroller. The system includes multiple traffic light modules, RFID readers, and I2C LCD displays. The primary function of the system is to manage traffic lights and provide emergency control for ambulances using RFID tags. The system also displays messages on LCD screens.

Component List

  1. Arduino Mega 2560

    • Description: A microcontroller board based on the ATmega2560.
    • Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D0-D53, AREF, SDA, SCL
  2. 16x2 I2C LCD

    • Description: A 16x2 character LCD display with I2C interface.
    • Pins: GND, VCC, SDA, SCL
  3. Traffic Light

    • Description: A module representing a traffic light with red, yellow, and green LEDs.
    • Pins: Green, Yellow, Red, GND
  4. RFID-RC522

    • Description: An RFID reader module.
    • Pins: VCC (3.3V), RST, GND, IRQ, MISO, MOSI, SCK, SDA

Wiring Details

Arduino Mega 2560

16x2 I2C LCD

Traffic Light

RFID-RC522

Documented Code

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

// Define pins for traffic signal modules
int redPins[] = {2, 6, 9, A0};      // Red LEDs for each module
int greenPins[] = {4, 8, 11, A2};   // Green LEDs for each module

// Define the duration for the green light (10 seconds)
unsigned long greenLightDuration = 10000; // 10,000 milliseconds = 10 seconds

// Variable to track which module should have its green light on
int currentModule = 0;

// Variable to track the last time the green light changed
unsigned long lastChangeTime = 0;

// RFID reader setup
#define SS_PIN 53  // Change SS_PIN to 53 (instead of 10)
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

// I2C LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Address 0x27, 16 columns and 2 rows

// States for the system
enum SystemState { GREEN_ROTATION, FIRST_MODULE_GREEN, EMERGENCY_MODE };
SystemState systemState = GREEN_ROTATION;

bool firstScanDone = false;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  
  // Initialize RFID reader
  SPI.begin();
  mfrc522.PCD_Init();

  // Initialize the LCD
  lcd.begin(16, 2);  // Initialize with the LCD's dimensions
  lcd.clear();
  
  // Ensure the backlight is on
  lcd.backlight();   // Turn on the LCD backlight
  
  // Set all pins as OUTPUT
  for (int i = 0; i < 4; i++) {
    pinMode(redPins[i], OUTPUT);
    pinMode(greenPins[i], OUTPUT);
  }

  // Initialize all LEDs to off state (red on, green off)
  for (int i = 0; i < 4; i++) {
    digitalWrite(redPins[i], HIGH);   // Turn on red light initially
    digitalWrite(greenPins[i], LOW);  // Ensure green light is off initially
  }

  // Set the initial state (green signal rotation)
  lastChangeTime = millis(); // Initialize the start time for the first green light

  // Show initial message on the LCD
  lcd.setCursor(0, 0);
  lcd.print("Emergency Traffic");
  lcd.setCursor(0, 1);
  lcd.print("Control System");
}

void loop() {
  // Check if an RFID tag has been scanned
  if (mfrc522.PICC_IsNewCardPresent()) {
    if (mfrc522.PICC_ReadCardSerial()) {
      String tagUID = "";
      for (byte i = 0; i < mfrc522.uid.size; i++) {
        tagUID += String(mfrc522.uid.uidByte[i], HEX);
      }
      Serial.println("Tag UID: " + tagUID);
      
      // Handle the scan event based on whether it's the first or second scan
      if (!firstScanDone) {
        // First scan: display Ambulance message
        firstScanDone = true;
        
        // Switch to Emergency Mode
        systemState = EMERGENCY_MODE;
        
        // Set emergency signal (green for first module)
        digitalWrite(greenPins[0], HIGH);
        digitalWrite(redPins[0], LOW);
        for (int i = 1; i < 4; i++) {
          digitalWrite(greenPins[i], LOW);
          digitalWrite(redPins[i], HIGH);
        }

        // Show Ambulance message on LCD
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Ambulance Nearby");
        lcd.setCursor(0, 1);
        lcd.print("PleaseCo-operate");
        
      } else {
        // Second scan: Reset system and display control system message
        firstScanDone = false;
        
        // Reset the system state to GREEN_ROTATION
        systemState = GREEN_ROTATION;
        lastChangeTime = millis(); // Reset the timer for green light rotation
        
        // Ensure the first module's green light is turned off before starting rotation again
        digitalWrite(greenPins[0], LOW);
        digitalWrite(redPins[0], HIGH);
        
        // Move to the second module for rotation start
        currentModule = 1; // Start