Fingerprint sensor with ESP32 not working [solved]

Hi
I have an fingerprint sensor just like the one from Adafruit but with red light.
I've connected it to an original Arduino Uno Rev3 via SoftwareSerial (Pins 2&3) and it worked flawlessly.
But when I connect it to an ESP32 DOIT Devkit V1 via HardwareSerial(UART2: Pins 16&17), the ESP32 won't recognize the fingerprint sensor.
I am using the fingerprint sensor library from Adafruit and the fingerprint example sketch from it.

This is my code without void loop:

#include <Adafruit_Fingerprint.h>

// On Leonardo/Micro or others with hardware serial, use those! #0 is green wire, #1 is white
// uncomment this line:
// #define mySerial Serial1
HardwareSerial mySerial(2);

// For UNO and others without hardware serial, we must use software serial...
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
// comment these two lines if using hardware serial
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()  
{
  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);
  
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1) { delay(1); }
  }

  finger.getTemplateCount();
  Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  Serial.println("Waiting for valid finger...");
}

On my Serial Monitor I always get this output:

"Did not find fingerprint sensor : ("

My circuit is shown the image in the attachment. In it I used the LunaNode, but it should be the ESP32 Devkit-board.

How do I get the fingerprint sensor working with the ESP32?

I even tried if the HardwareSerial port was even functioning on the ESP32. So I connected it with an SoftwareSerial port on an Arduino Uno and that worked just fine. So I think the problem is that the library isn't working with the ESP32.

In the image, you have interfaced your fingerprint sensor with TX2 and RX2.

Have you tried it with TX0 and RX0 ? I think it will work there.

Also give a try to software serial.

With HardwareSerial 0 I only got these strange characters : "[I⸮@⸮". So I tried this sketch without the fingerprint sensor connected

HardwareSerial mySerial(0);

void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}

void loop() {
Serial.println("Serial");
mySerial.println("mySerial");
delay(750);
}

and got this output in the Serial Monitor:

Serial
mySerial
Serial
mySerial

So I think HarswareSerial 0 is used to communicate to the PC.

SoftwareSerial is also not available for ESP32. Whe I try to compile this code

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 4);
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}

void loop() {
Serial.println("Test");
mySerial.println("mySerial");
delay(750);
}

I get this error message:

C:\Users\cedric\Documents\Arduino\sketch_nov10a\sketch_nov10a.ino:1:28: fatal error: SoftwareSerial.h: No such file or directory

compilation terminated.

exit status 1
Fehler beim Kompilieren für das Board ESP32 Dev Module.

Unless the below (untested) code works I think that because of the way extra hardware serial ports are constructed on the ESP32 that the only way to get the fingerprint library to work with the ESP32 is to have an overloaded .begin where all 4 parameters needed for extra ESP32 hardware serial ports can be specified.

#include <Adafruit_Fingerprint.h>

HardwareSerial mySerial(1);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup() 
{
  Serial.begin(115200);
  delay(1000);
  Serial.println("\n\nAdafruit finger detect test");

  mySerial.begin(57600, SERIAL_8N1, 16, 17);
  // 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); }
  }

  finger.getTemplateCount();
  Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  Serial.println("Waiting for valid finger...");
}

The code You provided does not work. I get this output when running it:

Adafruit finger detect test
Did not find fingerprint sensor :(

Is the fingerprint reader 5V or 3.3V? 5V RX may damage the ESP32.

Maybe try a loopback test on mySerial to confirm that it is really working as a serial port.

Try swapping the mySerial RX/TX pins over in case they are connected wrong way round.

I've just noticed that my fingerprint sensor has a different shape to the one from Adafruit. So I searched on Amazon for my sensor. So from what I understood from the Amazon description it has 3.3V logic but also works with 5V on the Serial port. I couldn't find a datasheet for it.

I connected TX2 to RX0 and uploaded this sketch:

HardwareSerial mySerial(2);

void setup() {
Serial.begin(57600);
delay(1000);
Serial.println("Begin mySerial");
mySerial.begin(57600, SERIAL_8N1, 17, 16);
//mySerial.begin(57600);
delay(1000);

}

void loop() {
  Serial.println("Loop");
  delay(100);
 mySerial.println("s");
 delay(1000);
 if(Serial.available())
 {
  char c = Serial.read();
  Serial.print(c);
 }

}

and got this output:

Begin mySerial
Loop
sLoop

Loop

Loop
sLoop

Loop

So TX2 works.

When I connect TX0 to RX2 and run this code

HardwareSerial mySerial(2);

void setup() {
Serial.begin(57600);
delay(1000);
Serial.println("Begin mySerial");
mySerial.begin(57600, SERIAL_8N1, 16, 17);
//mySerial.begin(57600);
delay(1000);

}

void loop() {
 Serial.println("s");
 delay(1000);
 if(mySerial.available())
 {
  char c = Serial.read();
  Serial.println(c);
 }

}

I get this output

Begin mySerial
s
⸮
s
⸮

So I tried this sketch on an ESP32 which I never used Serial2 and got the same output.

I tried changing the pins in mySerial.begin and the ESP32 crashed multiple times before it goes on in the code. Here's the output of that:

entry 0x40078a58


Adafruit finger detect test

Stack smashing protect failure!

abort() was called at PC 0x400d3a3c on core 1

Backtrace: 0x4008756c:0x3ffca310 0x4008766b:0x3ffca330 0x400d3a3c:0x3ffca350 0x400d0aeb:0x3ffca370 0x400d0af5:0x3ffca3f0 0x400d0911:0x00000000

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:956
load:0x40078000,len:0
load:0x40078000,len:13076
entry 0x40078a58


Adafruit finger detect test

Stack smashing protect failure!

abort() was called at PC 0x400d3a3c on core 1

Backtrace: 0x4008756c:0x3ffca310 0x4008766b:0x3ffca330 0x400d3a3c:0x3ffca350 0x400d0aeb:0x3ffca370 0x400d0af5:0x3ffca3f0 0x400d0911:0x00000000

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:956
load:0x40078000,len:0
load:0x40078000,len:13076
entry 0x40078a58


Adafruit finger detect test
Did not find fingerprint sensor :(

It sure looks like the serial port is working okay.

You say you have tried the exact same fingerprint module on an UNO with basically the same code (same baud rate, same libraries) using software serial and it works but trying to use hardware serial on ESP32 fails to work.

It seems the default baud rate is 9600 and your using 57600. Have you changed the default?

Does it still work on the UNO? Maybe there is more in the Adafruit library that is not ESP32 compatible but I don't have one to test with.

With this code on the Uno it works just fine:

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
//HardwareSerial mySerial(1);
SoftwareSerial mySerial(2, 3);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
  Serial.begin(115200);
  delay(1000);
  Serial.println("\n\nAdafruit finger detect test");

  mySerial.begin(57600);
  // 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); }
  }

  finger.getTemplateCount();
  Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  Serial.println("Waiting for valid finger...");
}

void loop(){
  
}

I get this output:

Adafruit finger detect test
Found fingerprint sensor!
Sensor contains 1 templates
Waiting for valid finger...

Adafruit provides this Windows software for testing with which I enrolled my fingerprint for testing. Now I can't get it running and don't remember how I connected it. But with the Uno it works just fine.

I successfully got the software running. I don't know if it helps but I atached a screenshot of the software connected to the Uno with fingerprint sensor.

Redstoned_boy:
I successfully got the software running. I don't know if it helps but I atached a screenshot of the software connected to the Uno with fingerprint sensor.

I just noticed a typo with the code I supplied in #4 and the pin changes you applied in #7, so does the below code work with ESP32?

#include <Adafruit_Fingerprint.h>

HardwareSerial mySerial(2);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
  Serial.begin(115200);
  delay(1000);
  Serial.println("\n\nAdafruit finger detect test");

  mySerial.begin(57600, SERIAL_8N1, 17, 16);
  // 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); }
  }

  finger.getTemplateCount();
  Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  Serial.println("Waiting for valid finger...");
}

I tested your code and it still doesn't work.
Thats still my output:

Adafruit finger detect test
Did not find fingerprint sensor :(

Redstoned_boy:
I tested your code and it still doesn't work.
Thats still my output:

Adafruit finger detect test

Did not find fingerprint sensor :frowning:

I dug out an ESP32, run a loopback test and noticed the first character read was corruption (probably left from setting up the serial port)
What happens if you load the below code, put a link wire between pins 16 & 17 and with the serial monitor window open, run it. If it's working okay then it should look like...

Serial
6D m
79 y
53 S
65 e
72 r
69 i
61 a
6C l

If it works then put the extra lines in setup to flush the mySerial buffers in the fingerprint test sketch and try it again.

  while(mySerial.available())           // Flush stray characters from mySerial
  {
    mySerial.read();
  }

If it still does not work then try swapping the wires over on pins 16/17. If that does not work then I would have to suspect something in the Adafruit library (though I cannot see why) or the fingerprint reader is not 3.3V compatible.

#define BAUD 115200

HardwareSerial mySerial(2);

void setup() {
  Serial.begin(BAUD);
  delay(1000);
  Serial.println();
  Serial.println("Serial");
  
  mySerial.begin(BAUD, SERIAL_8N1, 16, 17);
  delay(1000);
  while(mySerial.available())           // Flush stray characters from mySerial
  {
    mySerial.read();
  }
  mySerial.print("mySerial");
}

void loop() {
  while(mySerial.available())           // If anything comes in Serial2 (pins 17 & 16)
  {
    byte x = mySerial.read();
    Serial.print(x, HEX);
    Serial.print("\t");
    Serial.write(x);                    // read it and send it out Serial (USB)
    Serial.print("\n");
  }
  
  while(Serial.available())             // If anything comes in Serial (USB)
  {
    mySerial.write(Serial.read());      // read it and send it out Serial2 (pins 17 & 16)
  }
}

I get the output with 16&17 connected just like you do.

The extra code in the fingerprint sketch doesn't fix the problem. The sensor still doesn't get recognized.

So the only option is to upload the fingerprint sketch to an arduino and connect the arduino to the ESP32?

Did you try swapping the fingerprint wires over between pins 16/17? If so then I wonder if the sensor is not truly 3.3V compatible.
If the problem is voltage then maybe try a voltage level shifter like this to convert 3.3V to/from 5V.

Yes, with all of my tests I always swapped RX an TX wires.

I will buy the voltage level shifter. As soon as I get these, I will share my results (aprox. 3 days)

Redstoned_boy:
I will buy the voltage level shifter. As soon as I get these, I will share my results (aprox. 3 days)

3 days is quick, obviously not a slow boat from China like I would use.

I have been looking at the ESP32 DOIT Devkit V1 and the NCP1117 voltage regulator it uses should be fine for 1 amp or more so it does not look like it should be a cause of power dropout.
I also looks at the FPM10A datasheet that says it's 3.6v -6.0v compatible so power is probably the cause of failure assuming your using 3.3V power and not plugging into the 5V.
Hopefully with the level shifters you can power the module from 5V and they will convert the serial signals 3.3V/5V so the ESP32 does not get damaged.

With the signal voltage shifter board I got it working. I've powered the fingerprint sensor with both 5V and 3.3V and both worked. I thinkt the reason is that the fingerprint sensor doesn't recognize 3.3V as HIGH on the TX pin. the RX pin I connected directly to the ESP32. I just upshift the TX signal. That got it working.
Thank You! :wink: