Arduino UNO-Based RFID and Ultrasonic Sensor Security System with LED Indicators
View and Edit Project In Cirkit Designer
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
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
LED: Two Pin (red)
- Description: Red LED for visual indication.
- Pins: cathode, anode
Resistor (200 Ohms)
- Description: Resistor to limit current through the LEDs.
- Pins: pin1, pin2
LED: Two Pin (green)
- Description: Green LED for visual indication.
- Pins: cathode, anode
Resistor (200 Ohms)
- Description: Resistor to limit current through the LEDs.
- Pins: pin1, pin2
HC-SR04 Ultrasonic Sensor
- Description: Ultrasonic sensor for distance measurement.
- Pins: VCC, TRIG, ECHO, GND
RFID-RC522
- Description: RFID reader module.
- Pins: VCC (3.3V), RST, GND, IRQ, MISO, MOSI, SCK, SDA
Wiring Details
Arduino UNO
- 5V connected to VCC (3.3V) of RFID-RC522 and VCC of HC-SR04 Ultrasonic Sensor.
- GND connected to pin1 of both resistors, GND of RFID-RC522, and GND of HC-SR04 Ultrasonic Sensor.
- D13 connected to SCK of RFID-RC522.
- D12 connected to MISO of RFID-RC522.
- D11 connected to MOSI of RFID-RC522.
- D10 connected to SDA of RFID-RC522.
- D9 connected to RST of RFID-RC522.
- D7 connected to anode of green LED.
- D6 connected to anode of red LED.
- D3 connected to TRIG of HC-SR04 Ultrasonic Sensor.
- D2 connected to ECHO of HC-SR04 Ultrasonic Sensor.
LED: Two Pin (red)
- anode connected to D6 of Arduino UNO.
- cathode connected to pin2 of a 200 Ohm resistor.
Resistor (200 Ohms)
- pin1 connected to GND of Arduino UNO.
- pin2 connected to cathode of red LED.
LED: Two Pin (green)
- anode connected to D7 of Arduino UNO.
- cathode connected to pin2 of a 200 Ohm resistor.
Resistor (200 Ohms)
- pin1 connected to GND of Arduino UNO.
- pin2 connected to cathode of green LED.
HC-SR04 Ultrasonic Sensor
- VCC connected to 5V of Arduino UNO.
- TRIG connected to D3 of Arduino UNO.
- ECHO connected to D2 of Arduino UNO.
- GND connected to GND of Arduino UNO.
RFID-RC522
- VCC (3.3V) connected to 5V of Arduino UNO.
- RST connected to D9 of Arduino UNO.
- GND connected to GND of Arduino UNO.
- SCK connected to D13 of Arduino UNO.
- MISO connected to D12 of Arduino UNO.
- MOSI connected to D11 of Arduino UNO.
- SDA connected to D10 of Arduino UNO.
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