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

Arduino UNO-Based RFID and Ultrasonic Sensor Security System with LED Indicators

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based RFID and Ultrasonic Sensor Security System with LED Indicators

Circuit Documentation

Summary

This circuit is designed to monitor a subway platform using an RFID reader, an ultrasonic sensor, and LEDs to indicate the status. The Arduino UNO microcontroller is the central unit that interfaces with the RFID-RC522 module, the HC-SR04 ultrasonic sensor, and two LEDs (red and green). The system checks for an authorized RFID card and, upon successful authentication, monitors the platform for objects within a certain distance using the ultrasonic sensor. The LEDs provide visual feedback on the system's status.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. LED: Two Pin (red)

    • Description: Red LED for visual indication.
    • Pins: cathode, anode
  3. Resistor (200 Ohms)

    • Description: Resistor to limit current through the LEDs.
    • Pins: pin1, pin2
  4. LED: Two Pin (green)

    • Description: Green LED for visual indication.
    • Pins: cathode, anode
  5. Resistor (200 Ohms)

    • Description: Resistor to limit current through the LEDs.
    • Pins: pin1, pin2
  6. HC-SR04 Ultrasonic Sensor

    • Description: Ultrasonic sensor for distance measurement.
    • Pins: VCC, TRIG, ECHO, GND
  7. RFID-RC522

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

Wiring Details

Arduino UNO

LED: Two Pin (red)

Resistor (200 Ohms)

LED: Two Pin (green)

Resistor (200 Ohms)

HC-SR04 Ultrasonic Sensor

RFID-RC522

Code Documentation

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

// 핀 정의
#define RST_PIN 9
#define SS_PIN 10
#define TRIG_PIN 3
#define ECHO_PIN 2
#define GREEN_LED 7
#define RED_LED 6

MFRC522 mfrc522(SS_PIN, RST_PIN);

// 실제 카드의 UID
byte authorizedUID[4] = {0x74, 0xD9, 0x17, 0x02};  // 74 D9 17 02

// 시스템 상태 변수
bool isAuthorized = false;
unsigned long detectionStartTime = 0;
unsigned long lastDetectionTime = 0;
int detectionCount = 0;
const unsigned long MAX_DETECTION_TIME = 5000; // 최대 허용 시간 (5초)
const int MAX_DETECTIONS = 2; // 최대 허용 감지 횟수

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  
  Serial.println("지하철 승강장 모니터링 시스템이 준비되었습니다.");
  Serial.println("인증된 카드 UID: 74 D9 17 02");
}

void loop() {
  // RFID 카드 체크
  checkRFID();
  
  // 인증된 상태에서만 초음파 센서 모니터링
  if (isAuthorized) {
    monitorPlatform();
  }
  
  delay(100);
}

void checkRFID() {
  // 새로운 카드가 감지되지 않으면 리턴
  if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  
  // 카드 UID 출력 (디버깅용)
  Serial.print("감지된 카드 UID: ");
  for (byte i = 0; i < 4; i++) {
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
  
  if (checkUID()) {
    Serial.println("인증 성공!");
    isAuthorized = true;
    resetDetectionVariables();
    digitalWrite(GREEN_LED, HIGH);
    digitalWrite(RED_LED, LOW);
  } else {
    Serial.println("인증 실패!");
    isAuthorized = false;
    digitalWrite(RED_LED, HIGH);
    delay(2000);
    digitalWrite(RED_LED, LOW);
  }
  
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}

boolean checkUID() {
  for (byte i = 0; i < 4; i++) {
    if (mfrc522.uid.uidByte[i] != authorizedUID[i]) {
      return false;
    }
  }
  return true;
}

void monitorPlatform() {
  long duration, distance;
  
  // 초음파 센서로 거리 측정
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2;
  
  // 물체 감지 (거리가 50cm 이내일 때)
  if (distance < 50) {
    if (detectionStartTime == 0) {
      detectionStartTime = millis();
      detectionCount++;
      Serial.print("물체 감지됨 - 거리: ");
      Serial.print(distance);
      Serial.println("cm");
    }
    
    // 감지 시간이 너무 길거나 감지 횟수가 초과된 경우
    if ((millis() - detectionStartTime > MAX_DETECTION_TIME) || 
        (detectionCount > MAX_DETECTIONS)) {
      triggerAlert();
    }
  } else {
    if (detectionStartTime != 0) {
      lastDetectionTime = millis();
      detectionStartTime = 0;
    }
  }
}

void triggerAlert() {
  Serial.println("경고: 비정상적인 행동 감지!");
  isAuthorized = false;
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(RED_LED, HIGH);
  delay(4000);
  digitalWrite(RED_LED, LOW);
  resetDetectionVariables();
}

void resetDetection