Arduino UNO Controlled RFID and Ultrasonic Sensor Security System
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit integrates an Arduino UNO with an RFID-RC522 module, an HC-SR04 Ultrasonic Distance Sensor, and a Servomotor SG90 to create a system that detects objects within a certain distance and checks for RFID tags. If an object is detected within the predefined distance and an RFID tag is present, the servo motor is activated to move to a specified angle.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.
RFID-RC522
- An RFID reader/writer module
- Operates at 13.56 MHz frequency
- Interfaces with microcontrollers via SPI
HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)
- An ultrasonic ranging module
- Provides 2cm to 400cm non-contact measurement functionality
- Operates on a 5V power supply
Servomotor SG90
- A small and lightweight servo motor
- Capable of rotating approximately 180 degrees
- Operates on a 5V power supply
Wiring Details
Arduino UNO
D10connected to RFID-RC522 SDAD13connected to RFID-RC522 SCKD11connected to RFID-RC522 MOSID12connected to RFID-RC522 MISO- Multiple
GNDpins connected to GND of RFID-RC522, HC-SR04, and Servomotor SG90 D4connected to RFID-RC522 RST3.3Vconnected to RFID-RC522 VCC (3.3V)5Vconnected to HC-SR04 VCC and Servomotor SG90 VCCD3connected to HC-SR04 TRIGD2connected to HC-SR04 ECHOD5connected to Servomotor SG90 SIG
RFID-RC522
SDAconnected to Arduino UNO D10SCKconnected to Arduino UNO D13MOSIconnected to Arduino UNO D11MISOconnected to Arduino UNO D12GNDconnected to common groundRSTconnected to Arduino UNO D4VCC (3.3V)connected to Arduino UNO 3.3V
HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)
VCCconnected to Arduino UNO 5VTRIGconnected to Arduino UNO D3ECHOconnected to Arduino UNO D2GNDconnected to common ground
Servomotor SG90
SIGconnected to Arduino UNO D5VCCconnected to Arduino UNO 5VGNDconnected to common ground
Documented Code
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
// Define pins for HC-SR04
const int trigPin = 3;
const int echoPin = 2;
// Define pins for RFID-RC522
#define SS_PIN 10
#define RST_PIN 4
MFRC522 rfid(SS_PIN, RST_PIN);
// Define servo
Servo myServo;
const int servoPin = 5;
// Define distance threshold (in cm)
const int distanceThreshold = 10;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize pins for HC-SR04
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize RFID
SPI.begin();
rfid.PCD_Init();
// Initialize servo
myServo.attach(servoPin);
myServo.write(0); // Initial position
}
void loop() {
// Measure distance using HC-SR04
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
// Check if distance is below threshold
if (distance < distanceThreshold) {
// Check for RFID card
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
// Turn servo to 60 degrees
myServo.write(60);
delay(1000); // Hold position for 1 second
myServo.write(0); // Return to initial position
}
}
delay(100); // Small delay to avoid excessive polling
}
This code initializes the HC-SR04 sensor, RFID-RC522 module, and Servomotor SG90 on an Arduino UNO. It continuously measures the distance using the ultrasonic sensor and checks for the presence of an RFID tag when an object is detected within a specified threshold. If both conditions are met, the servo motor is activated.