# Open door with fingerprint in dormitory> Original text: https://jswyll.com/note/fingerprint_opendoor/ ## Effect display! [](https://jswyll.com/note/fingerprint_opendoor/figures/fingerprintVerifyOK.gif "Fingerprint matching, open door successfully ") Fingerprints match, the door opens successfully! [](https://jswyll.com/note/fingerprint_opendoor/figures/fingerprintVerifyFail.gif "Fingerprints do not match, the door does not open") Fingerprints do not match, the door does not open --- ## Control scheme  1. The microcontroller sends instructions to the fingerprint module, and the control module performs corresponding operations;
2. The microcontroller reads the result code returned by the fingerprint module and determines whether the pressed fingerprint matches the registered fingerprint; 3. If they match, the motor will be driven to open the door, otherwise it will not operate. --- ## Hardware selection - fingerprint module. The fingerprint recognition algorithm is relatively complex and difficult. Choosing a fingerprint module that integrates various functions can greatly reduce the development cycle. We only need to send the instructions specified in the manual to the module through the UART protocol. Since the backlight of the optical fingerprint module needs to be always on, a capacitive fingerprint module is used here: ID1016C fingerprint module from Shenzhen Sanneng Intelligent Control Electronic Technology Co., Ltd., ¥51, link: [https://m.tb.cn/ h.VyfWAZe?sm=4e567d](https://m.tb.cn/h.VyfWAZe?sm=4e567d) - Door opening driver. For the lock as shown below, pull the handle to open the door. Using a servo to drive the door opens, the cost is about ¥15, link: [[Taobao] https://m.tb.cn/h.fpYlFGd?tk=01NW2ghNvCz「MG995 MG996R Metal standard servo digital robot 13KG servo 180 degrees 360 degrees"](https://m.tb.cn/h.fpYlFGd?tk=01NW2ghNvCz).  - Microcontroller. STC's microcontroller codes are universal. This design only requires a pair of UARTs (serial ports) and an ordinary IO port. In order to reduce the circuit board area, STC15W204S is selected, and the cost is ¥1~3. - Power socket. Use a USB interface that outputs 5V and use the popular Type-C interface to supply power to the circuit board; at the same time, Type-C can also be connected to a computer for debugging and downloading programs. --- ## The circuit board is drawn using Lichuang EDA. For the schematic and PCB, see: [https://oshwhub.com/jswy/opendoor-fingerprint](https://oshwhub.com/jswy/opendoor-fingerprint) ### Type-C female socket The entire circuit is powered by Type-C. Draw a 16-pin Type-C female socket on the circuit board. Among them, the `CC1`, `CC2`, `SUB1`, and `SUB2` pins are not used and can be left floating. Here, these pins are removed directly from the package. `DP1/DP2` and `DN1/DN2` are the `D+` and `D-` signals of USB respectively.  ### USB to serial port The USB signal of the computer is different from the serial port signal of the microcontroller. Use the compact CH340E chip for conversion! [](https://jswyll.com/note/fingerprint_opendoor/figures/ usb2ttl.png "USB to serial port circuit") ### Reset circuit STC microcontroller needs to power off and then power on again when downloading the program. Here, buttons + MOS tube are used to control the power on and off. (Since the serial port still needs to work normally when the power is off, the power supply for the USB-to-serial port is directly connected to the USB.)  # ## The working voltage of the voltage stabilizing circuit microcontroller and fingerprint module is 3.3V. The AMS1117-3.3 chip is used to convert the 5V of the USB power supply to 3.3V ### Servo interface The power supply of the servo is 5~6V. The servo comes with a female Dupont line + an extended male-to-male Dupont line, so a 3PIN female interface is reserved! [](https://jswyll.com/note/fingerprint_opendoor/figures/steer_pin.png "Servo pin header interface") ### In addition to 3.3V, the microcontroller has TXD and RXD connected to the fingerprint module, plus an ordinary IO port Connect the servo PWM control pin ### Fingerprint module interface The fingerprint module has 6PIN 1.0mm spacing cables. Leave 4 holes.  ### 3D preview After drawing the PCB, open the 3D preview in Easy EDA:  --- ### The red line of the wiring fingerprint module starts and the white line ends, which are the GND, RX, TX, VIN, IRQ, VCC --- ## Programming - Microcontroller and fingerprint module UART communication. Check the module manual provided by the seller. The default baud rate of the module is 115200bps. Each control command sends 26 bytes (starting with 0x55AA) and returns a 26-byte response code (starting with 0xAA55). See the manual for the meaning of other bytes. - Determine whether a finger is pressed. Directly send 26 bytes "x55xAAx00x00x21x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x20x01" (x represents hexadecimal escape). The value of the returned 11th byte is 1, which means there is a finger pressing down. By sending this command cyclically, you can detect whether there is a finger press. If so, follow-up operations will be performed. Otherwise, polling will continue. - Determine whether fingerprints match. Send 26 bytes "x55xAAx00x00x63x00x06x00x00x00x01x00xC8x00x00x00x00x00x00x00x00x00x00x00x31x02" for verification. The 7th byte returned is equal to 5, indicating that the fingerprint matches the one entered, and the 11th byte indicates the matching fingerprint number. - Activate the door. It can be seen from the steering gear control principle that the PWM control period is 20ms, and the high-level duration is 0.5, 1.0, 1.5, 2.0, and 2.5, which correspond to the positions of the steering gear turning to 0, 90, 180, 270, and 360 degrees respectively. According to the actual installation location, after debugging, the control range of 0.5~1.3ms is selected to correspond to the closing and opening of the door. Set the interrupt period of the timer to 10 microseconds, and add 1 to the interrupt count variable each time, then the PWM thresholds of 50 and 130 correspond to 0.5 and 1.3ms respectively. In order to avoid cumulative errors, use the 12.000MHz main frequency when downloading the program. ```c /** @brief Timer count, PWM value*/ int T0_count, pwm = 50; /** * @brief Timer 0 initialization, set to generate an interrupt every 10 microseconds */ void Timer0Init(void) / / 10 microseconds @12.000MHz { AUXR |= 0x80; //Timer clock 1T mode TMOD &= 0xF0; //Set timer mode TL0 = 0x88; //Set timing initial value TH0 = 0xFF; //Set timing initial value TF0 = 0; / /Clear the TF0 flag TR0 = 1; //Timer 0 starts timing ET0 = 1; } /** * @brief timer interrupt, used to generate PWM */ void T0_IRQHandler() interrupt 1 { if (T0_count #include "intrins .h" sfr AUXR = 0x8E; sfr T2H = 0xD6; sfr T2L = 0xD7; sbit door = P3 ^ 3; /** * @brief fingerprint module control command */ typedef enum { CMD_LED_RED_ON, /**> 8); break ; } default: uart_puts(cmd_data[cmd], 26); break; } /* Wait for the fingerprint module to return 26 bytes of response data (interrupt reception) */ while (recv_count 0) { /* If the match is entered Number 1 (administrator), enter new fingerprint */ if (recv_buff[10] == 1) { /* Get the first unregistered number */ send_cmd(CMD_GET_EMPTY_ID); empty_number = recv_buff[10]; /* Collect 3 times in a row */ send_cmd(CMD_LED_YELLOW_ON); //Turn on the yellow light and wait for the finger to press delay_ms(500); //Delay to avoid the first input being the administrator's unraised finger waitForFingerTouch(); / /Call the fingerprint collection function send_cmd(CMD_GENERATE_0); //Temporarily store the first entered fingerprint send_cmd(CMD_LED_ALL_OFF); //Turn off the light and prompt the finger to move away delay_ms(500); //Wait for the finger to move away send_cmd(CMD_LED_YELLOW_ON); waitForFingerTouch(); send_cmd(CMD_GENERATE_1); send_cmd(CMD_LED_ALL_OFF); delay_ms(500); send_cmd(CMD_LED_YELLOW_ON); waitForFingerTouch(); send_cmd(CMD_GENERATE_2); send_cmd(CMD_LED_ALL_OFF); /* Synthesize the fingerprint features entered three times and store them*/ send_cmd(CMD_MERGE_CHAR); send_cmd(CMD_STORE_CHAR); } /* The green light is on, indicating that fingerprint matching or fingerprint entry is completed, and the relay is controlled to open the door*/ open_door(); } else { /* The fingerprint data does not match the entered one, and it lights red Light*/ send_cmd(CMD_LED_RED_ON); /* Delay before making matching judgment to avoid triggering the same fingerprint twice in a short time*/ delay_ms(500); } } } ```` --- ## Notes STC15W204S only has T0 and T2 timer, T1 cannot be used to generate baud rate --- ## Code open source warehouse address [https://gitee.com/jswyll_com/fingerprint-open-the-door](https://gitee.com/jswyll_com /fingerprint-open-the-door)26); break; } /* Wait for the fingerprint module to return 26 bytes of response data (interrupt reception) */ while (recv_count 0) { /* If it matches the entered number 1 (administrator), enter a new fingerprint* / if (recv_buff[10] == 1) { /* Get the first unregistered number */ send_cmd(CMD_GET_EMPTY_ID); empty_number = recv_buff[10]; /* Collect 3 times in a row */ send_cmd(CMD_LED_YELLOW_ON); //Light up yellow, wait for the finger to press delay_ms(500); //Delay to avoid the first input being the administrator's unraised finger waitForFingerTouch(); //Call the fingerprint collection function send_cmd(CMD_GENERATE_0); / /Temporarily store the first entered fingerprint send_cmd(CMD_LED_ALL_OFF); //Turn off the light and prompt the finger to move away delay_ms(500); //Wait for the finger to move away send_cmd(CMD_LED_YELLOW_ON); waitForFingerTouch(); send_cmd(CMD_GENERATE_1); send_cmd( CMD_LED_ALL_OFF); delay_ms(500); send_cmd(CMD_LED_YELLOW_ON); waitForFingerTouch(); send_cmd(CMD_GENERATE_2); send_cmd(CMD_LED_ALL_OFF); /* Synthesize the fingerprint features entered three times and store them*/ send_cmd(CMD_MERGE_CHAR); send_cmd(CMD_STORE_CHAR); } /* The green light is on, indicating that the fingerprint matching or fingerprint entry is completed, and the relay is controlled to open the door*/ open_door(); } else { /* The fingerprint data does not match the entered one, the red light is on*/ send_cmd(CMD_LED_RED_ON); /* Delay Matching judgment will be made when the same fingerprint is triggered twice in a short period of time */ delay_ms(500); } } ``` --- ## Notes STC15W204S only has T0 and T2 timers, T1 cannot be used to generate baud Rate --- ## Code open source warehouse address [https://gitee.com/jswyll_com/fingerprint-open-the-door](https://gitee.com/jswyll_com/fingerprint-open-the-door)26); break; } /* Wait for the fingerprint module to return 26 bytes of response data (interrupt reception) */ while (recv_count 0) { /* If it matches the entered number 1 (administrator), enter a new fingerprint* / if (recv_buff[10] == 1) { /* Get the first unregistered number */ send_cmd(CMD_GET_EMPTY_ID); empty_number = recv_buff[10]; /* Collect 3 times in a row */ send_cmd(CMD_LED_YELLOW_ON); //Light up yellow, wait for the finger to press delay_ms(500); //Delay to avoid the first input being the administrator's unraised finger waitForFingerTouch(); //Call the fingerprint collection function send_cmd(CMD_GENERATE_0); / /Temporarily store the first entered fingerprint send_cmd(CMD_LED_ALL_OFF); //Turn off the light and prompt the finger to move away delay_ms(500); //Wait for the finger to move away send_cmd(CMD_LED_YELLOW_ON); waitForFingerTouch(); send_cmd(CMD_GENERATE_1); send_cmd( CMD_LED_ALL_OFF); delay_ms(500); send_cmd(CMD_LED_YELLOW_ON); waitForFingerTouch(); send_cmd(CMD_GENERATE_2); send_cmd(CMD_LED_ALL_OFF); /* Synthesize the fingerprint features entered three times and store them*/ send_cmd(CMD_MERGE_CHAR); send_cmd(CMD_STORE_CHAR); } /* The green light is on, indicating that the fingerprint matching or fingerprint entry is completed, and the relay is controlled to open the door*/ open_door(); } else { /* The fingerprint data does not match the entered one, the red light is on*/ send_cmd(CMD_LED_RED_ON); /* Delay Matching judgment will be made when the same fingerprint is triggered twice in a short period of time */ delay_ms(500); } } ``` --- ## Notes STC15W204S only has T0 and T2 timers, T1 cannot be used to generate baud Rate --- ## Code open source warehouse address [https://gitee.com/jswyll_com/fingerprint-open-the-door](https://gitee.com/jswyll_com/fingerprint-open-the-door)