Techatronic
Published

ESP32 with RFID RC522 Module

RFID module with ESP32 micrcontroller

BeginnerFull instructions provided4 hours4,667
ESP32 with RFID RC522 Module

Things used in this project

Story

Read more

Schematics

esp32 with rfid

a

Code

code

C/C++
a
#include "SPI.h"
#include "MFRC522.h"
#define SS_PIN 5
#define RST_PIN 0
int m=0,n=0;
#include <ESP32Servo.h>
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
#include <LiquidCrystal_I2C.h>
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
Servo myservo;  // create servo object to control a servo
int pos = 0;  
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();          // Initiate  SPI bus
  //mfrc522.PCD_Init();   // Initiate MFRC522
   rfid.PCD_Init();
   lcd.init();  
   myservo.attach(32);
  
  
  Serial.println("Put your card to the reader...");
  lcd.backlight();
  
  



  
  lcd.setCursor(0, 1);
  lcd.print("Put your card");
  Serial.println();
  //digitalWrite(RELAY, HIGH);
 myservo.write(90); 
 delay(500);
pinMode(33, INPUT_PULLUP);
 
 pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
 //pinMode(A0, INPUT_PULLUP);
  pinMode(27, OUTPUT);
 pinMode(14, OUTPUT);
 //pinMode(14, OUTPUT);
 // analogWrite(3, 5);
  digitalWrite(26, LOW);
    digitalWrite(25, HIGH);
    
}
void loop() 
{
  lcd.backlight();
 lcd.clear();
  Serial.println("Put your card to the reader...");
  lcd.setCursor(0, 1);
  lcd.print("Put your card");
  Serial.println();
  // Look for new cards
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial())
    return;
  // Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  // Serial.println(rfid.PICC_GetTypeName(piccType));
  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) 
    {
      
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }
  String strID = "";
  for (byte i = 0; i < 4; i++) {
    strID +=
    (rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
    String(rfid.uid.uidByte[i], HEX) +
    (i!=3 ? ":" : "");
  }
  strID.toUpperCase();
  
  Serial.print("Tap card key: ");
  
  //delay(1000);
  Serial.println(strID);
if(strID.indexOf("11:65:FB:0B")>=0)
{
  Serial.println("Authorized access");
    lcd.clear();
    lcd.setCursor(0, 1);
  lcd.print("Authorized access");
    Serial.println();
   // 
   
    myservo.write(180);              // tell servo to go to position in variable 'pos'
             
 
   
    delay(500);
    delay(ACCESS_DELAY);
   myservo.write(90);  
  //delay(500);
 
while(1)
{
    int m= analogRead(33);
    Serial.println(m);
   // delay(300);
    
    if(m<200)
    {
      
while(1)
{
     digitalWrite(26, HIGH);
      digitalWrite(14, HIGH);
    digitalWrite(25, LOW);
    digitalWrite(27, HIGH );
   lcd.clear();
    lcd.setCursor(0, 1);
  lcd.print("Room Occupied");
  lcd.setCursor(0, 0);
  lcd.print("A Team 3 Members");
   analogWrite(3, 225);
delay(200);
    }
    }
    else if (m>500)
    {
 //      analogWrite(3, 10);
  digitalWrite(26, LOW);
    digitalWrite(25, HIGH);
   // digitalWrite(36, LOW );
      delay(100);
      digitalWrite(14, LOW);
      
      }
  
  
  }
}
  
  
 else   {
    Serial.println(" Access denied");
    lcd.clear();
    lcd.setCursor(0, 1);
  lcd.print("Access denied");
 delay(DENIED_DELAY);
    // analogWrite(3, 10);
  digitalWrite(26, LOW);
    digitalWrite(25, HIGH);
   //  delay(DENIED_DELAY);
   digitalWrite(14, LOW);
  }
}

Credits

Techatronic

Techatronic

72 projects • 126 followers
Electronic engineer

Comments