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

Arduino UNO-Based RFID Access Control System with Ultrasonic Sensor and I2C LCD Display

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based RFID Access Control System with Ultrasonic Sensor and I2C LCD Display

Circuit Documentation

Summary

This circuit integrates an Arduino UNO microcontroller with several peripheral devices including a 16x2 I2C LCD, an RFID-RC522 module, an HC-SR04 Ultrasonic Sensor, and a red LED with a current-limiting resistor. The circuit is designed to display messages on the LCD based on RFID card detection and distance measurements from the ultrasonic sensor.

Component List

  1. Arduino UNO

    • Description: A 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. 16x2 I2C LCD

    • Description: A 16x2 character LCD display with I2C interface.
    • Pins: GND, VCC, SDA, SCL
  3. RFID-RC522

    • Description: An RFID reader module operating at 13.56 MHz.
    • Pins: VCC (3.3V), RST, GND, IRQ, MISO, MOSI, SCK, SDA
  4. LED: Two Pin (red)

    • Description: A standard red LED.
    • Pins: cathode, anode
  5. Resistor

    • Description: A resistor with a resistance of 220 Ohms.
    • Pins: pin1, pin2
  6. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND

Wiring Details

Arduino UNO

16x2 I2C LCD

RFID-RC522

LED: Two Pin (red)

Resistor

HC-SR04 Ultrasonic Sensor

Code Documentation

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

#define RST_PIN 9
#define SS_PIN 10
#define LED_PIN 2
#define TRIG_PIN 3
#define ECHO_PIN 4

LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rfid(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  digitalWrite(LED_PIN, LOW);
  lcd.init();
  lcd.backlight();
  SPI.begin();
  rfid.PCD_Init();
  lcd.setCursor(0, 0);
  lcd.print("Loogman");
}

void loop() {
  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 / 2) / 29.1;

  if (distance < 30) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welkom");
    lcd.setCursor(0, 1);
    lcd.print("scan uw kaart aub");
    if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
      digitalWrite(LED_PIN, HIGH);
      Serial.println("Card detected, LED turned on");
      String uid = "";
      for (byte i = 0; i < rfid.uid.size; i++) {
        uid += String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
        uid += String(rfid.uid.uidByte[i], HEX);
      }
      uid.toUpperCase();
      lcd.clear();
      lcd.setCursor(0, 0);
      if (uid == " F7 A0 DF D8") {
        lcd.print("Hi Lil Bitch");
      } else if (uid == " 37 2F 8F C8") {
        lcd.print("Hi Nino");
      } else if (uid == " 43 38 17 E2") {
        lcd.print("2 doekjes");
      } else if (uid == " 7D A4 97 3F") {
        lcd.print("1 doekje");
      } else {
        lcd.print("Unknown UID");
      }
      delay(2000);
      digitalWrite(LED_PIN, LOW);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Loogman");
      rfid.PICC_HaltA();
      rfid.PCD_StopCrypto1();
    }
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Loogman");
  }
  delay(500);
}

This code initializes the LCD, RFID reader, and ultrasonic sensor. It continuously measures the distance using the ultrasonic sensor and displays a welcome message on the LCD if an object is detected within 30 cm. If an RFID card is detected, it reads the card's UID and displays a corresponding message on the LCD.