HOW to Build an Intelligent Entrance Guard System with ESP32
Description
In this project, we use the RFID522 card swiping module and the servo to set up an intelligent access control system. The principle is very simple. We use RFID522 swipe card module, an IC card or key card to unlock.
Required Components
|
|
|
|
---|---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Key*1 |
IC Card*1 |
|
|
|
|
Keyestudio RFID Module*1 |
Servo*1 |
4P Dupont Wire*1 |
Micro USB Cable*1 |
Connection Diagram
Test Code
Note: Different RFID-MFRC522 IC cards and keys have diverse values. You can substitute your own IC cards and keys values for the corresponding values read by the RFID-MFRC522 module in the program, otherwise the servo can’t be controlled when uploading the test code to the ESP32.
For example: You can replace the rfid_str of the in the program code with your own IC cards and keys values read by the RFID-MFRC522 module.
//*************************************************************************************
/*
* Filename : Intelligent_access_control
* Description : RFID controlled steering gear simulated door opening
* Auther : http//www.keyestudio.com
*/
#include <Wire.h>
#include "MFRC522_I2C.h"
// IIC pins default to GPIO21 and GPIO22 of ESP32
// 0x28 is the i2c address of SDA, if doesn't match,please check your address with i2c.
MFRC522 mfrc522(0x28); // create MFRC522.
#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo
int servoPin = 15; // Servo motor pin
String rfid_str = "";
void setup() {
Serial.begin(9600);
Wire.begin();
mfrc522.PCD_Init();
ShowReaderDetails(); // dispaly PCD - MFRC522 read carder
Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 500, 2500); // attaches the servo on servoPin to the servo object
myservo.write(0);
delay(500);
}
void loop() {
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50);
return;
}
// select one of door cards. UID and SAK are mfrc522.uid.
// save UID
rfid_str = ""; //String emptying
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) {
rfid_str = rfid_str + String(mfrc522.uid.uidByte[i], HEX); //Convert to string
//Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
//Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println(rfid_str);
if (rfid_str == "edf7945a" || rfid_str == "4c96b6e") {
myservo.write(180);
delay(500);
Serial.println(" open the door!");
}
}
void ShowReaderDetails() {
// attain the MFRC522 software
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// when returning to 0x00 or 0xFF, may fail to transmit communication signals
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
}
}
//*************************************************************************************
Code Explanation
In the previous experiment, our card swipe module has tested the information of IC card and key. Then we use this corresponding information to control the door.
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. Open the serial monitor and set the baud rate to 9600. When we use the IC card or blue key to swipe the card, the monitor displays the card and the key information and “open the door”, at the same time, the servo rotates to the corresponding angle to simulate opening the door.