ESP32 Fingerprint based Biometric Door Lock & Home Automation
Table of Contents
ESP32 Fingerprint:
ESP32 Fingerprint-based Biometric Door Lock & Home Automation– Until now, you may have seen many home automation systems, but the home automation system that I am going to present today is different from all of them because this system is based on the fingerprint sensor and this is never done before.
A fingerprint-based Biometric Door lock makes sense but a fingerprint-based Home automation, it sounds quite weird isn’t it? Well, you don’t have to control all your home appliances but you can secure certain loads that you don’t want anyone else to turn ON or turn OFF. Now, it could be a TV or a computer, or a Gaming machine; if you don’t want your kids to turn ON these things while you are not around. Anyway, it totally depends on you which loads you want to secure with a fingerprint sensor.
Anyway, first let me tell you about the hardware and then we will start the practical demonstration.
As you can see, I am using my designed ESP32 WiFi + Bluetooth Module Development board, but it doesn’t matter if you don’t have this development board you can use a breadboard for the initial testing, and then you can use a Vero board for the final connections. But still if you want to make the same development board then you can download it’s Gerber files.
This board has 4 SPDT type relays. So, using these 4 relays I can control AC and DC loads both at the same time. If you want to control more AC or DC loads then go ahead and increase the number of relays. Don’t worry, I will explain the circuit diagram.
Anyway, this is the R557 Capacitive Fingerprint module and it has the ability to store 120 Fingerprints. You can read my getting started article on this fingerprint sensor if you want to know more about its technical specifications, wiring details, and lots of other things.
This is a 12V Electronic Door Lock and these are 220Vac Bulbs. Now, let’s go ahead and start a practical demonstration.
I have powered up the entire system. When the 110/220Vac is ON never touch the relay contacts because it can be really dangerous. Wear protective gloves and it’s good to have someone on your side while working on such high-voltage projects.
Anyway, for this project, I have registered a total of 4 fingers, and using my four fingers I am going to control these 4 loads.
Using my four fingers, I was able to control 3 Bulbs and one electronic Door Lock. For the step-by-step explanation and practical demonstration watch video tutorial given at the end of this article. I am sure by now, you might have got an idea of how does this system work. So, without any further delay let’s get started!!!
Amazon Links:
R557 Capacitive Fingerprint Module
ESP32 WiFi + Bluetooth Module(Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
R557 Capacitive Biometric Sensor:
The R557 Fingerprint Sensor module is a compact and reliable biometric Sensor that can be used to secure doors or other access points. It can also be used in students and employees attendance systems. It’s a low-cost and low-power Fingerprint sensor. It operates on a low voltage of 3.3V, making it ideal for battery-powered systems. It can be used with 5v and 3.3V compatible controller boards like Arduino, ESP32, STM32, ESP8266, Raspberry Pi Pico, and so on. With this Capacitive Fingerprint sensor, you can easily integrate biometric authentication into your projects.
R557 Technical specifications:
Technical parameter:
Supply voltage: DC 3.3V
Supply current:
Working current: 30mA (typ.)
Peak current: 40mA
Fingerprint image entry time: <0.3 seconds
Collection window area: 12.8mm in diameter
Image pixel: 160*160 pixel
Image Resolution: 508dpi
Match method:
Comparison method (1:1)
Search method (1:N)
Storage capacity: 120 pieces
Security level: five (from low to high: 1, 2, 3, 4, 5)
False Recognition Rate (FAR): <0.0001%
Authentic rejection rate (FRR): <1.0%
Search time: <0.3 seconds (1:1000, average)
Host computer interface: RS232 (TTL logic level, 3.3V)
Communication baud rate: (9600?N)bps where N=1~12 (default value N=6, ie 57600bps)
R557 Working environment:
Temperature: -20 to +45?
Relative humidity: 10%RH-85%RH (non-condensing)
There is an RGB Led inside this module. So, when the fingerprint sensor is ON it shows Blue light. When the Fingerprint scanning operation is successful it shows Green Light, and when the operation is failed, it shows Red light.
It includes a storage capacity of up to 120 fingerprints, a fast recognition speed of under 1 second, and a high-resolution image sensor that captures clear and accurate fingerprints.
R557 Capacitive Biometric Sensor Pinout:
Wiring
1 VT Finger detection
2 Out IRQ
3 VCC 3.3
4 TX 3.3 Logic level
5 RX 3.3 Logic level
6 GND
1st wire is the VT. This is the Finger Detection power and this wire is connected with the 3.3V.
2nd wire is the IRQ. This is the Finger detection signal wire. When there is a finger, it outputs a high signal.
3rd wire is the VCC and it should be connected with 3.3V.
4th and 5th wires are TXD and RXD, both are 3.3V TTL logical level. And
6th wire is the GND and it should be connected with the ground.
Now, let’s go ahead and take a look at the circuit diagram.
ESP32 Fingerprint Circuit Diagram:
J1 is the female power jack and this is where we connect a 12v adaptor, battery or a solar panel. Two 470uf capacitors are connected at the input and output sides of the voltage regulator. The output of the voltage regulator is connected to the 5v pin of the ESP32 module. The ground of the power supply is connected to the ground of the ESP32 module.
The R557 Capacitive Fingerprint module VT and VCC wires are connected with the 3.3V pin on the ESP32 module. The IRQ wire is not connected. The Green and Blue wires which are the TXD and RXD pins are connected with the GPIO pins 16 and 17. And the White wire is the GND wire and it’s connected to the GND pin of the ESP32 module.
These are 12v SPDT type relays and can’t be directly controlled using the ESP32 Module, So, that’s why we need a driver to control these relays. You can use a relay driver IC or you can use 2n2222 NPN transistors and 10k resistors. One pin of the relay coil is connected with the collector of the 2n2222 NPN transistor while the other pin of the relay coil is connected with the 12 volts. The emitter of the transistor is connected with the ground while the base is connected with the 10k ohm resistor.
Now to control these relays you simply need to connect these 10k resistors with the ESP32 I/O pins. In this project, I am using the GPIO pins 13, 12, 14, and 27.
The neutral wire from the 110/220Vac supply is connected with the neutral of all the lights. While the Live wire from the AC supply is connected with the lights through these relays.
Here is my ESP32 development board and as you can see right now only the Fingerprint module is connected. Now, the next step is to enroll in the fingerprints. But first, we are going to install the fingerprint library.
Fingerprint Library Installation:
For this, open the Arduino IDE. Click on the sketch Menu, go to Include Library, and then click on manage libraries.
Search for the Adafruit Fingerprint.
Click on the install button.
You can see my Adafruit Fingerprint Sensor Library is now installed.
Next, for the Fingerprint enrollment connect your ESP32 WiFi + Bluetooth module with the Laptop/PC and Upload this sketch.
Fingerprint Enrollment Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
#include <Adafruit_Fingerprint.h> #include <HardwareSerial.h> Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial2); uint8_t id; void setup() { Serial.begin(9600); while (!Serial); // For Yun/Leo/Micro/Zero/... delay(100); Serial.println("\n\nAdafruit Fingerprint sensor enrollment"); // set the data rate for the sensor serial port finger.begin(57600); if (finger.verifyPassword()) { Serial.println("Found fingerprint sensor!"); } else { Serial.println("Did not find fingerprint sensor :("); while (1) { delay(1); } } Serial.println(F("Reading sensor parameters")); finger.getParameters(); Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX); Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX); Serial.print(F("Capacity: ")); Serial.println(finger.capacity); Serial.print(F("Security level: ")); Serial.println(finger.security_level); Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX); Serial.print(F("Packet len: ")); Serial.println(finger.packet_len); Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate); } uint8_t readnumber(void) { uint8_t num = 0; while (num == 0) { while (! Serial.available()); num = Serial.parseInt(); } return num; } void loop() // run over and over again { Serial.println("Ready to enroll a fingerprint!"); Serial.println("Please type in the ID # (from 1 to 127) you want to save this finger as..."); id = readnumber(); if (id == 0) {// ID #0 not allowed, try again! return; } Serial.print("Enrolling ID #"); Serial.println(id); while (! getFingerprintEnroll() ); } uint8_t getFingerprintEnroll() { int p = -1; Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id); while (p != FINGERPRINT_OK) { p = finger.getImage(); switch (p) { case FINGERPRINT_OK: Serial.println("Image taken"); break; case FINGERPRINT_NOFINGER: Serial.println("."); break; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); break; case FINGERPRINT_IMAGEFAIL: Serial.println("Imaging error"); break; default: Serial.println("Unknown error"); break; } } // OK success! p = finger.image2Tz(1); switch (p) { case FINGERPRINT_OK: Serial.println("Image converted"); break; case FINGERPRINT_IMAGEMESS: Serial.println("Image too messy"); return p; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); return p; case FINGERPRINT_FEATUREFAIL: Serial.println("Could not find fingerprint features"); return p; case FINGERPRINT_INVALIDIMAGE: Serial.println("Could not find fingerprint features"); return p; default: Serial.println("Unknown error"); return p; } Serial.println("Remove finger"); delay(2000); p = 0; while (p != FINGERPRINT_NOFINGER) { p = finger.getImage(); } Serial.print("ID "); Serial.println(id); p = -1; Serial.println("Place same finger again"); while (p != FINGERPRINT_OK) { p = finger.getImage(); switch (p) { case FINGERPRINT_OK: Serial.println("Image taken"); break; case FINGERPRINT_NOFINGER: Serial.print("."); break; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); break; case FINGERPRINT_IMAGEFAIL: Serial.println("Imaging error"); break; default: Serial.println("Unknown error"); break; } } // OK success! p = finger.image2Tz(2); switch (p) { case FINGERPRINT_OK: Serial.println("Image converted"); break; case FINGERPRINT_IMAGEMESS: Serial.println("Image too messy"); return p; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); return p; case FINGERPRINT_FEATUREFAIL: Serial.println("Could not find fingerprint features"); return p; case FINGERPRINT_INVALIDIMAGE: Serial.println("Could not find fingerprint features"); return p; default: Serial.println("Unknown error"); return p; } // OK converted! Serial.print("Creating model for #"); Serial.println(id); p = finger.createModel(); if (p == FINGERPRINT_OK) { Serial.println("Prints matched!"); } else if (p == FINGERPRINT_PACKETRECIEVEERR) { Serial.println("Communication error"); return p; } else if (p == FINGERPRINT_ENROLLMISMATCH) { Serial.println("Fingerprints did not match"); return p; } else { Serial.println("Unknown error"); return p; } Serial.print("ID "); Serial.println(id); p = finger.storeModel(id); if (p == FINGERPRINT_OK) { Serial.println("Stored!"); } else if (p == FINGERPRINT_PACKETRECIEVEERR) { Serial.println("Communication error"); return p; } else if (p == FINGERPRINT_BADLOCATION) { Serial.println("Could not store in that location"); return p; } else if (p == FINGERPRINT_FLASHERR) { Serial.println("Error writing to flash"); return p; } else { Serial.println("Unknown error"); return p; } return true; } |
Code explanation:
1 2 3 4 5 6 7 |
#include <Adafruit_Fingerprint.h> #include <HardwareSerial.h> Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial2); uint8_t id; |
The code begins by including two libraries: Adafruit_Fingerprint and HardwareSerial. These libraries provide the necessary functions for interfacing with the fingerprint sensor. The Adafruit_Fingerprint library is specifically designed for Adafruit fingerprint sensors.
An instance of the Adafruit_Fingerprint class is created and named finger. It is initialized with the Serial2 object, which represents the serial communication for the fingerprint sensor. The id variable is declared as an unsigned 8-bit integer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
void setup() { Serial.begin(9600); while (!Serial); // For Yun/Leo/Micro/Zero/... delay(100); Serial.println("\n\nAdafruit Fingerprint sensor enrollment"); // set the data rate for the sensor serial port finger.begin(57600); if (finger.verifyPassword()) { Serial.println("Found fingerprint sensor!"); } else { Serial.println("Did not find fingerprint sensor :("); while (1) { delay(1); } } Serial.println(F("Reading sensor parameters")); finger.getParameters(); Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX); Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX); Serial.print(F("Capacity: ")); Serial.println(finger.capacity); Serial.print(F("Security level: ")); Serial.println(finger.security_level); Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX); Serial.print(F("Packet len: ")); Serial.println(finger.packet_len); Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate); } |
The setup() function is called once at the beginning of the program. It performs the initialization of the fingerprint sensor and prints some information about the sensor.
The program starts the serial communication with a baud rate of 9600. Then, it waits for the serial connection to be established (the while (!Serial) loop is used for compatibility with certain boards like Yun, Leo, Micro, Zero, etc.).
The program initializes the fingerprint sensor by calling finger.begin(57600). This sets the data rate for the sensor’s serial port.
Next, it verifies the password for the fingerprint sensor using finger.verifyPassword(). If the password is verified, it indicates that the fingerprint sensor is found. Otherwise, it prints an error message and enters an infinite loop (while (1) { delay(1); }).
The program then reads various parameters from the sensor using finger.getParameters() and prints them out using Serial.print and Serial.println functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
uint8_t readnumber(void) { uint8_t num = 0; while (num == 0) { while (! Serial.available()); num = Serial.parseInt(); } return num; } |
The readnumber() function is a helper function used to read a number from the serial input. It waits for the serial input to be available (while (!Serial.available())) and then reads an integer using Serial.parseInt(). It returns the read number as an 8-bit unsigned integer (uint8_t).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
void loop() { Serial.println("Ready to enroll a fingerprint!"); Serial.println("Please type in the ID # (from 1 to 127) you want to save this finger as..."); id = readnumber(); if (id == 0) {// ID #0 not allowed, try again! return; } Serial.print("Enrolling ID #"); Serial.println(id); while (!getFingerprintEnroll()); } |
The loop() function is called repeatedly in an infinite loop. It prompts the user to enter an ID number for enrolling a fingerprint.
It first prints some messages to the serial monitor to guide the user. It calls the readnumber() function to read the ID number entered by the user and stores it in the id variable.
If the entered ID is 0, it returns from the loop() function, effectively restarting the loop to prompt the user for a valid ID.
If a non-zero ID is entered, it prints a message indicating the ID being enrolled and enters a while loop, repeatedly calling the getFingerprintEnroll() function until the enrollment process is completed.
1 2 3 4 5 |
uint8_t getFingerprintEnroll() { int p = -1; Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id); |
This line indicates that the function is expecting an id value to be available. However, the value of id is not defined in the provided code snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
while (p != FINGERPRINT_OK) { p = finger.getImage(); switch (p) { case FINGERPRINT_OK: Serial.println("Image taken"); break; case FINGERPRINT_NOFINGER: Serial.println("."); break; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); break; case FINGERPRINT_IMAGEFAIL: Serial.println("Imaging error"); break; default: Serial.println("Unknown error"); break; } } |
This while loop waits until a valid finger image is obtained by calling finger.getImage(). The value returned by finger.getImage() is stored in p, and the code inside the switch statement handles different cases based on the value of p. For example, if p is FINGERPRINT_OK, it means an image has been successfully captured and the message “Image taken” is printed. Other cases include FINGERPRINT_NOFINGER (indicating that no finger is detected), FINGERPRINT_PACKETRECIEVEERR (communication error), and FINGERPRINT_IMAGEFAIL (imaging error).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
p = finger.image2Tz(1); switch (p) { case FINGERPRINT_OK: Serial.println("Image converted"); break; case FINGERPRINT_IMAGEMESS: Serial.println("Image too messy"); return p; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); return p; case FINGERPRINT_FEATUREFAIL: Serial.println("Could not find fingerprint features"); return p; case FINGERPRINT_INVALIDIMAGE: Serial.println("Could not find fingerprint features"); return p; default: Serial.println("Unknown error"); return p; } |
Once a valid image is obtained, it is converted into a fingerprint template using finger.image2Tz(1). The returned value is stored in p, and the switch statement handles different cases based on the value of p. If p is FINGERPRINT_OK, it means the image was successfully converted into a template, and the message “Image converted” is printed. Other cases include FINGERPRINT_IMAGEMESS (indicating that the image is too messy), FINGERPRINT_PACKETRECIEVEERR (communication error), FINGERPRINT_FEATUREFAIL (failure to find fingerprint features), and FINGERPRINT_INVALIDIMAGE (failure to find fingerprint features).
1 2 3 4 5 6 7 8 9 10 11 |
Serial.println("Remove finger"); delay(2000); p = 0; while (p != FINGERPRINT_NOFINGER) { p = finger.getImage(); } |
The code prompts the user to remove their finger and waits for 2 seconds (delay(2000)) before attempting to capture an image again. The while loop keeps checking for the absence of a finger (FINGERPRINT_NOFINGER) by calling finger.getImage() until it detects no finger.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
Serial.print("ID "); Serial.println(id); p = -1; Serial.println("Place same finger again"); while (p != FINGERPRINT_OK) { p = finger.getImage(); switch (p) { case FINGERPRINT_OK: Serial.println("Image taken"); break; case FINGERPRINT_NOFINGER: Serial.print("."); break; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); break; case FINGERPRINT_IMAGEFAIL: Serial.println("Imaging error"); break; default: Serial.println("Unknown error"); break; } } |
Once the finger is removed, the code prompts the user to place the same finger on the sensor again. It then enters another loop, similar to the previous one, to capture the image of the finger. The switch statement handles different cases based on the value of p as explained earlier.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
p = finger.image2Tz(2); switch (p) { case FINGERPRINT_OK: Serial.println("Image converted"); break; case FINGERPRINT_IMAGEMESS: Serial.println("Image too messy"); return p; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); return p; case FINGERPRINT_FEATUREFAIL: Serial.println("Could not find fingerprint features"); return p; case FINGERPRINT_INVALIDIMAGE: Serial.println("Could not find fingerprint features"); return p; default: Serial.println("Unknown error"); return p; } |
After capturing the second image, it is again converted into a fingerprint template using finger.image2Tz(2). The switch statement handles different cases based on the value of p as explained earlier.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
p = finger.createModel(); if (p == FINGERPRINT_OK) { Serial.println("Prints matched!"); } else if (p == FINGERPRINT_PACKETRECIEVEERR) { Serial.println("Communication error"); return p; } else if (p == FINGERPRINT_ENROLLMISMATCH) { Serial.println("Fingerprints did not match"); return p; } else { Serial.println("Unknown error"); return p; } |
After converting the second image, the code attempts to create a fingerprint model using finger.createModel(). If p is FINGERPRINT_OK, it means the model was created successfully, and the message “Prints matched!” is printed. Other cases include FINGERPRINT_PACKETRECIEVEERR (communication error) and FINGERPRINT_ENROLLMISMATCH (indicating that the fingerprints did not match).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
Serial.print("ID "); Serial.println(id); p = finger.storeModel(id); if (p == FINGERPRINT_OK) { Serial.println("Stored!"); } else if (p == FINGERPRINT_PACKETRECIEVEERR) { Serial.println("Communication error"); return p; } else if (p == FINGERPRINT_BADLOCATION) { Serial.println("Could not store in that location"); return p; } else if (p == FINGERPRINT_FLASHERR) { Serial.println("Error writing to flash"); return p; } else { Serial.println("Unknown error"); return p; } |
Finally, the code attempts to store the created model using finger.storeModel(id). If the return value p is FINGERPRINT_OK, it means the model was successfully stored, and the message “Stored!” is printed. Other cases include FINGERPRINT_PACKETRECIEVEERR (communication error), FINGERPRINT_BADLOCATION (indicating that the model could not be stored in that location), and FINGERPRINT_FLASHERR (error writing to flash memory).
The function returns true if the execution reaches this point without encountering any errors.
After uploading this code; next open the serial monitor and follow the instructions. If you find it difficult to enroll your fingers then you can follow my video tutorial given at the end of this article. Anyway, I enrolled 4 fingers “the forefinger, middle finger, ringer finger, and small finger” I assigned 3 as an ID to the forefinger, 4 as an ID to my Middle finger, 5, and 6 to the ringer finger and small finger respectively.
After, enrolling all the fingers. Next, download the following code, and replace my finger ids with yours.
ESP32 Fingerprint Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
#include <Adafruit_Fingerprint.h> #include <HardwareSerial.h> int relay1=12; int relay2=13; int relay3=14; int relay4=27; //#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__) // // //#else //// On Leonardo/M0/etc, others with hardware serial, use hardware serial! //// #0 is green wire, #1 is white //#define mySerial Serial1 // //#endif // Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial2); void setup() { pinMode(relay1,OUTPUT); pinMode(relay2,OUTPUT); pinMode(relay3,OUTPUT); pinMode(relay4,OUTPUT); digitalWrite(relay1, LOW); digitalWrite(relay2, LOW); digitalWrite(relay3, LOW); digitalWrite(relay4, LOW); Serial.begin(9600); while (!Serial); // For Yun/Leo/Micro/Zero/... delay(100); Serial.println("\n\nAdafruit finger detect test"); // set the data rate for the sensor serial port finger.begin(57600); delay(5); if (finger.verifyPassword()) { Serial.println("Found fingerprint sensor!"); } else { Serial.println("Did not find fingerprint sensor :("); while (1) { delay(1); } } Serial.println(F("Reading sensor parameters")); finger.getParameters(); Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX); Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX); Serial.print(F("Capacity: ")); Serial.println(finger.capacity); Serial.print(F("Security level: ")); Serial.println(finger.security_level); Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX); Serial.print(F("Packet len: ")); Serial.println(finger.packet_len); Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate); finger.getTemplateCount(); if (finger.templateCount == 0) { Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example."); } else { Serial.println("Waiting for valid finger..."); Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates"); } } void loop() // run over and over again { getFingerprintID(); delay(50); //don't ned to run this at full speed. } uint8_t getFingerprintID() { uint8_t p = finger.getImage(); switch (p) { case FINGERPRINT_OK: Serial.println("Image taken"); break; case FINGERPRINT_NOFINGER: Serial.println("No finger detected"); finger.LEDcontrol(FINGERPRINT_LED_OFF, 0, FINGERPRINT_LED_BLUE); finger.LEDcontrol(FINGERPRINT_LED_OFF, 0, FINGERPRINT_LED_RED); return p; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); return p; case FINGERPRINT_IMAGEFAIL: Serial.println("Imaging error"); return p; default: Serial.println("Unknown error"); return p; } // OK success! p = finger.image2Tz(); switch (p) { case FINGERPRINT_OK: Serial.println("Image converted"); break; case FINGERPRINT_IMAGEMESS: Serial.println("Image too messy"); return p; case FINGERPRINT_PACKETRECIEVEERR: Serial.println("Communication error"); return p; case FINGERPRINT_FEATUREFAIL: Serial.println("Could not find fingerprint features"); return p; case FINGERPRINT_INVALIDIMAGE: Serial.println("Could not find fingerprint features"); return p; default: Serial.println("Unknown error"); return p; } // OK converted! p = finger.fingerSearch(); if (p == FINGERPRINT_OK) { Serial.println("Found a print match!"); finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_PURPLE, 10); delay(1000); if(finger.fingerID==3) { digitalWrite(relay1,!digitalRead(relay1)); delay(1000); } if(finger.fingerID==4) { digitalWrite(relay2,!digitalRead(relay2)); delay(1000); } if(finger.fingerID==5) { digitalWrite(relay3,!digitalRead(relay3)); delay(1000); } if(finger.fingerID==6) { digitalWrite(relay4,!digitalRead(relay4)); delay(1000); } } else if (p == FINGERPRINT_PACKETRECIEVEERR) { Serial.println("Communication error"); return p; } else if (p == FINGERPRINT_NOTFOUND) { finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_RED, 10); delay(1000); Serial.println("Did not find a match"); return p; } else { Serial.println("Unknown error"); return p; } // found a match! Serial.print("Found ID #"); Serial.print(finger.fingerID); Serial.print(" with confidence of "); Serial.println(finger.confidence); return finger.fingerID; } // returns -1 if failed, otherwise returns ID # int getFingerprintIDez() { uint8_t p = finger.getImage(); if (p != FINGERPRINT_OK) return -1; p = finger.image2Tz(); if (p != FINGERPRINT_OK) return -1; p = finger.fingerFastSearch(); if (p != FINGERPRINT_OK) return -1; // found a match! Serial.print("Found ID #"); Serial.print(finger.fingerID); Serial.print(" with confidence of "); Serial.println(finger.confidence); return finger.fingerID; } |
This is the Final Code that I use to lock and unlock the Electronic Door Lock and to control other appliances or loads. If this is your first time using the ESP32 WiFi + Bluetooth module then you will also need to install the ESP32 board in the Arduino IDE. And for this, you can watch my video on the ESP32. Anyway, Let me show you, how I used the IDs.
If finger.fingerID == 3 then turn ON or turn OFF the Relay1. First, it reads the status of the relay1 pin. If it’s OFF then it will be turned ON and if its ON then it will be turned OFF. As you can see I am doing it the same way for the remaining IDs and relays.
Watch Video Tutorial: