Realize RFID card opening based on ESP32 and RC522

Original link: https://musenxi.com/archives/esp32-rc522.html

foreword

The small classrooms of the class use card swiping to open the door, but the access control student card of the small classroom is not entered, so only the teacher can open the door, so it is very inconvenient to go to the small classroom for self-study. Thinking of using the steering gear for traction, I searched the Internet and found that there are already case studies to share, but the whole network did not find a simple solution based on ESP32 and RC522, so I did it myself, so I published this article to share.

Prepare

The equipment and environment involved in this article are
ESP32-WROOM-32D recommends CH9102X chip, drive-free
SG5010 steering gear
The ESP32 38Pin extended version is of course only for the convenience of subsequent wiring and fixing on the door. Dupont wires can also be used without adding or using a breadboard.
MFRC-522 RF module
Arduino IDE

Arduino IDE environment configuration

First go to the last line of IDE File-Preferences preferences to add the link https://ghproxy.com/https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
Then go to Boards Manager to search for esp32 installation. There may be installation errors or inexplicable errors in subsequent compilations. Installing another version can generally solve the problem.
At this time, we can basically compile and adapt ESP32, but we still have MFRC-522 module and steering gear, so we need to add the library, search for MFRC-522 in the library manager and install it, and then search for ESP32Servo to install it. It should be noted that the library installed by searching for Servo alone cannot support ESP32. If you still get an error when downloading ESP32Servo, you can go to Github and download it, and then go to C:Users, username, AppDataLocalArduino15librariesServo to replace the file. The name on Github is ESP32_Servo. You need to change the file name and file All similar names inside can be replaced by Servo.

Serial port pin connection

RC522 pin name ESP32 pin names
3.3V 3V3
RST twenty two
GND GND
MISO 19
MOSI twenty three
SCK 18
SDA/SS 12
Servo pin color ESP32 pin names
orange 13
red 5V
brown GND

Programming

After connecting the pins, enter the link of programming.

 //Libraries #include <SPI.h>//https://www.arduino.cc/en/reference/SPI #include <MFRC522.h>//https://github.com/miguelbalboa/rfid #include <Servo.h> #include <string.h> #include <math.h> //Constants #define SS_PIN 12 #define RST_PIN 22 #define SERVO_PIN 13 //Servo Servo servoMotor; //Parameters const int ipaddress[4] = {103, 97, 67, 25}; // unsigned char serNum[5]; //ic卡的id码int decid=0; int i=0; //Variables byte nuidPICC[4] = {0, 0, 0, 0}; MFRC522::MIFARE_Key key; MFRC522 rfid = MFRC522(SS_PIN, RST_PIN); void setup() { //Init Serial USB Serial.begin(115200);//这里在串口监视器(Serial Monitor)旁边数值要调成一样Serial.println("welcome!"); SPI.begin(); rfid.PCD_Init(); rfid.PCD_DumpVersionToSerial(); // attaches the servo on ESP32 pin servoMotor.attach(SERVO_PIN, 500, 2400);//之前没加后面参数转不到180度servoMotor.write(0); } void loop() { readRFID(); switch(decid){ case 1002125://white 这里填写读卡器读到卡后的在串口监视器(Serial Monitor)上显示的decid数字Serial.println("Pass!"); // rotates from 0 degrees to 180 degrees for (int pos = 0; pos <= 180; pos += 1) { // in steps of 1 degree servoMotor.write(pos); delay(10); } delay(1000); servoMotor.write(0); break; } decid=0; } void readRFID(void ) { /* function readRFID */ ////Read RFID card for (byte i = 0; i < 6; i++) { key.keyByte[i] = 0xFF; } // Look for new 1 cards if ( ! rfid.PICC_IsNewCardPresent()) return; // Verify if the NUID has been readed if ( !rfid.PICC_ReadCardSerial()) return; // Store NUID into nuidPICC array for (byte i = 0; i < 4; i++) { nuidPICC[i] = rfid.uid.uidByte[i]; } unsigned char* id = nuidPICC; decid=0; decid=id[0]*pow(16,3)+id[1]*pow(16,2)+id[2]*pow(16,1)+id[3]*pow(16,0); Serial.print(F("RFID In dec: ")); printDec(rfid.uid.uidByte, rfid.uid.size); Serial.println(); Serial.print("DECID:"); Serial.println(decid); //输出卡id // Halt PICC rfid.PICC_HaltA(); // Stop encryption on PCD rfid.PCD_StopCrypto1(); } /** Helper routine to dump a byte array as dec values to Serial. */ void printDec(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], DEC); } }

end of code.

program burning

Select ESP32 Dev Module, select the correct serial port
Upload and then open the Monitor, swipe the card, and add the case of the switch statement in the program
The approximate effect is as shown in the figure below. Decid was not written at the time, so take a look

practice

After a rough measurement, use hot-melt glue to fix the oblique bottom of the door handle, and then stick the stopper on the top of the steering gear to prevent the hot-melt glue from falling off due to longitudinal force. Glue the rc522 module through the door gap to the outside. Connect the door handle with the steering gear with a wire, and finally realize the effect of swiping the card outside the door to control the steering gear inside the door to turn down and pull the door handle to open the door.

problems encountered

The final effect is almost perfect, and it is widely praised by teachers and students. There are no pictures for the time being. However, the current power supply method is to use the low-current mode of the power bank to supply power. It seems that it will still automatically power off after three hours. I plan to replace the two dry battery boxes. The 3V independently supplies power to the ESP, and the 6V independently supplies power to the steering gear.
I do not know whether it is feasible.

In a word, here is the end of the whole wonderful journey of exploration. It took half a month in total, and the total cost was more than 50. Of course, the support of my dear deskmate and classmates is also indispensable.

refer to:

https://rntlab.com/question/unit-9-esp32-control-servo-motor-library-problem/
https://esp32io.com/tutorials/esp32-servo-motor
https://www.electronicwings.com/esp32/rfid-rc522-interfacing-with-esp32
https://www.aranacorp.com/en/using-an-rfid-module-with-an-esp32/amp/
https://blog.csdn.net/lmf666/article/details/123527739
https://zhuanlan.zhihu.com/p/66467989
https://zhuanlan.zhihu.com/p/281103196
https://zhuanlan.zhihu.com/p/416003658

This article is transferred from: https://musenxi.com/archives/esp32-rc522.html
This site is only for collection, and the copyright belongs to the original author.