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

Arduino UNO Controlled RFID and Ultrasonic Sensor Security System

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled RFID and Ultrasonic Sensor Security System

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

RFID-RC522

HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)

Servomotor SG90

Wiring Details

Arduino UNO

RFID-RC522

HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)

Servomotor SG90

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.