5.4.18 Project 10 Open the Door
1. Component Knowledge
Radio frequency identification, the card reader is composed of a radio frequency module and a high-level magnetic field. The Tag transponder is a sensing device, which doesn’t contain a battery. It only contains tiny integrated circuit chips and media for storing data and antennas for receiving and transmitting signals.
To read the data in the tag, first put it into the reading range of the card reader. The reader will generate a magnetic field, which can produce electricity according to Lenz’s law, then the RFID tag will supply power, thereby activating the device.

2. Control Pins
Use IIC communication
SDA |
SDA |
|---|---|
SCL |
SCL |
3. Test Code
//**********************************************************************************
/*
* Filename : RFID
* Description : RFID reader UID
* Auther : http//www.keyestudio.com
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
#include <ESP32Servo.h>
Servo myservo;
#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.
#define servoPin 13
#define btnPin 16
boolean btnFlag = 0;
String password = "";
void setup() {
Serial.begin(115200); // initialize and PC's serial communication
mylcd.init();
mylcd.backlight();
Wire.begin(); // initialize I2C
mfrc522.PCD_Init(); // initialize MFRC522
ShowReaderDetails(); // dispaly PCD - MFRC522 read carder
Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
mylcd.setCursor(0, 0);
mylcd.print("Card");
}
void loop() {
//
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50);
password = "";
if(btnFlag == 1)
{
boolean btnVal = digitalRead(btnPin);
if(btnVal == 0) //If door close button is pressed (active-low)
{
Serial.println("close");
mylcd.setCursor(0, 0);
mylcd.print("close");
myservo.write(0);
btnFlag = 0;
}
}
return;
}
// select one of door cards. UID and SAK are mfrc522.uid.
// save UID
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
//Serial.print(mfrc522.uid.uidByte[i], HEX);
Serial.print(mfrc522.uid.uidByte[i]);
password = password + String(mfrc522.uid.uidByte[i]);
}
if(password == "") //Card number is correct,open the door
{
Serial.println("open");
mylcd.setCursor(0, 0);
mylcd.clear();
mylcd.print("open");
myservo.write(180);
password = "";
btnFlag = 1;
}
else //Card number error,dispaly error
{
password = "";
mylcd.setCursor(0, 0);
mylcd.print("error");
}
//Serial.println(password);
}
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?"));
}
}
//**********************************************************************************
4. Test Result
Upload the code, display “Card” on the LCD1602, open the serial monitor, and set the baud rate to “115200”.
Close the provided card to the RFID induction area, display “error” on the LCD1602,but the serial monitor output is as shown in the figure:

Input the “Card UID” from the image into the position shown in the
figure (remove spaces in “Card UID” and in the serial monitor’s Card
UID, remove leading 0 only if it appears before any digits
(e.g., " 0123" → "123"), but keep 0 if it follows a number
(e.g., "601" remains "601").):

Upload the code,close the provided card to the RFID induction area,the door will turn and open, and LCD1602 shows “open”.
Click button 1 and the door turns and closes. However, when swiping another blue induction block, the LCD1602 shows “Error”.