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

Arduino-Based Smart Gate System with Ultrasonic Sensor, RFID, and Servo Motor

View and Edit Project In Cirkit DesignerImage of Arduino-Based Smart Gate System with Ultrasonic Sensor, RFID, and Servo Motor

Circuit Documentation

Summary

This circuit integrates an Arduino UNO microcontroller with an HC-SR04 Ultrasonic Sensor, a Tower Pro SG90 servo motor, an RFID-RC522 module, and a buzzer. The system is designed to detect the presence of a car using the ultrasonic sensor, verify the car's RFID tag, and then operate a servo motor to open a gate. The buzzer provides an audible alert when a car is detected.

Component List

HC-SR04 Ultrasonic Sensor

Tower Pro SG90 Servo

RFID-RC522

Arduino UNO

Buzzer

Wiring Details

HC-SR04 Ultrasonic Sensor

Tower Pro SG90 Servo

RFID-RC522

Buzzer

Documented Code

/*
 * Arduino - Ultrasonic Sensor HC-SR04 with Buzzer, RFID, and Servo
 */
#include <MFRC522.h>
#include <SPI.h>
#include <Servo.h>

#define SS_PIN 10
#define RST_PIN 7

int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin
int buzzerPin = 2;  // Buzzer pin
int servoPin = 3;

float duration_us, distance_cm;

// Create a servo object
Servo Servo1;

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the RFID class
MFRC522::MIFARE_Key key;

bool carDetected = false;
bool rfidVerified = false;

void setup() {
  // Begin serial port
  Serial.begin(9600);

  // Configure pins
  pinMode(trigPin, OUTPUT); // Trigger pin as output
  pinMode(echoPin, INPUT);  // Echo pin as input
  pinMode(buzzerPin, OUTPUT); // Buzzer pin as output
  Servo1.attach(servoPin);
  Servo1.write(0);

  // Initialize RFID
  SPI.begin();       // Init SPI bus
  rfid.PCD_Init();   // Init RC522 module

  // Turn off the buzzer initially
  digitalWrite(buzzerPin, LOW);
}

void loop() {
  // Step 1: Ultrasonic sensor to detect a car
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // Calculate the distance
  distance_cm = 0.017 * duration_us;

  // Print the value to Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  // Check if a car is detected
  if (distance_cm < 5) {
    carDetected = true;

    // Activate the buzzer
    digitalWrite(buzzerPin, HIGH);
    delay(500);
    digitalWrite(buzzerPin, LOW);
  }

  // Step 2: RFID verification
  if (carDetected && !rfidVerified) {
    // Check for a new card
    if (rfid.PICC_IsNewCardPresent()) {
      // Verify if the UID has been read
      if (rfid.PICC_ReadCardSerial()) {
        Serial.print("RFID Tag UID: ");
        printHex(rfid.uid.uidByte, rfid.uid.size);
        Serial.println("");
        rfidVerified = true;

        // Halt the RFID reader
        rfid.PICC_HaltA();
      }
    }
  }

  // Step 3: Servo motor operates when both conditions are met
  if (carDetected && rfidVerified) {
    // Open the gate (servo up)
    Servo1.write(90);
    delay(7000);

    // Close the gate (servo down)
    Servo1.write(0);
    delay(500);

    // Reset flags for the next vehicle
    carDetected = false;
    rfidVerified = false;
  }

  delay(100); // Brief delay for stability
}

// Routine to dump a byte array as hex values to Serial
void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

This code initializes the components and continuously checks for the presence of a car using the ultrasonic sensor. If a car is detected, the buzzer is activated, and the system waits for RFID verification. Once the RFID tag is verified, the servo motor operates to open and close the gate.