Fingerprint sensor only working when plugged in USB (serial1 problem ?)

Hello,

This is my first arduino project and I hop some of you will be able to solve my issue.

My hardware is : Arduino uno R4 wifi

I have bought an Adafruit fingerprint sensor and I managed to make everything works as desired when plugged in to my PC via USB C.
However when I try to only use a power supply (Outup 9-12.6V 4A) the fingerprint is not detected anymore.

Please find my code below :

#include <WiFiS3.h>
#include <PubSubClient.h>
#include <Adafruit_Fingerprint.h>
#include <ArduinoJson.h>
#include <Arduino_LED_Matrix.h>
#include <ArduinoGraphics.h>
#include "SoftwareSerial.h"
ArduinoLEDMatrix matrix;

const char* ssid = "******";
const char* password = "*****";
const char* mqtt_server = "*****";
const char* mqtt_topic = "******";

#define RELAY_PIN 4
#define ACCESS_DELAY 5000  // Keep lock unlocked for 5 seconds

// Define SoftwareSerial for Arduino UNO and similar boards
#define mySerial Serial1

//SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {


  Serial.begin(9600);
  matrix.begin();
  litColumns(12, HIGH);
  delay(100);
  Serial.println("\n\nAdafruit finger detect test");
  litColumns(11, HIGH);
  finger.begin(57600);
  delay(5);

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  litColumns(2, HIGH);

  // Connect to MQTT broker
  client.setServer("192.168.1.27", 1883);
  client.setCallback(callback);
  while (!client.connected()) {
    if (client.connect("ArduinoClient")) {
      client.subscribe(mqtt_topic);
      Serial.println("Connected to MQTT broker");
      blinkLED(2);
    } else {
      Serial.println("Failed to connect to MQTT broker. Trying again in 5 seconds...");
      litColumns(3, HIGH);
      delay(5000);
    }
  }

  


  litColumns(2, HIGH);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    litColumns(0, HIGH);
    while (1) { delay(1); }
  }
  litColumns(10, HIGH);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);

  Serial.println(F("Reading sensor parameters"));
  finger.setSecurityLevel(1);
  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");
  }
  litColumns(1, HIGH);
  
}

void loop() {
  client.loop();
  if (getFingerPrint() != -1) {
    // Send MQTT message when the condition is met
    Serial.println("sending..");
    client.publish(mqtt_topic, "{\"id\":\"Malette\",\"state\":\"SOLVED\"}");
    Serial.println("MQTT message sent.");
    blinkLED(2);
    digitalWrite(RELAY_PIN, HIGH);
    delay(ACCESS_DELAY);
    digitalWrite(RELAY_PIN, LOW);
    Serial.println("Success");
  }
  delay(50);  // don't need 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");
      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!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    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;
}

// returns -1 if failed, otherwise returns ID #
int getFingerPrint() {
  int 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!
  return finger.fingerID;
}


// Function to blink the LED a specified number of times
void blinkLED(int numBlinks) {
  for (int i = 0; i < numBlinks; i++) {
    // Turn on the LED
    finger.LEDcontrol(true);
    delay(500);  // Adjust the delay based on your preference for blink speed

    // Turn off the LED
    finger.LEDcontrol(false);
    delay(500);  // Adjust the delay based on your preference for blink speed
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("Message received on topic: " + String(topic));

  // Parse JSON payload
  DynamicJsonDocument jsonDoc(1024);  // Adjust the size based on your JSON payload
  deserializeJson(jsonDoc, payload, length);

  // Access JSON data
  const char* id = jsonDoc["id"];
  const char* command = jsonDoc["command"];

  // Do something with the data
  Serial.println("ID: " + String(id));
  Serial.println("Command: " + String(command));

  // Check if ID is "Malette" and command is "SOLVED"
  if (strcmp(id, "Malette") == 0 && strcmp(command, "SOLVED") == 0) {
    Serial.println("Triggering relay...");
    digitalWrite(RELAY_PIN, HIGH);  // Turn ON the relay
    client.publish(mqtt_topic, "{\"id\":\"Malette\",\"state\":\"SOLVED\"}");
    delay(5000);                   // Keep the relay ON for 5 seconds (adjust as needed)
    digitalWrite(RELAY_PIN, LOW);  // Turn OFF the relay
    Serial.println("Relay triggered and turned off.");
  }
}

void litColumns(int column, int state) {
  if (column < 0 || column >= 12) {
    Serial.println("Invalid column number");
    return;
  }

  // Define a frame with the specified column lit up
  uint8_t frame[8][12] = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  };

  for (int row = 0; row < 8; row++) {
    for (int col = 0; col < column && col < 12; col++) {
      frame[row][col] = 1;
    }
  }

  // Load and display the frame
  matrix.renderBitmap(frame, 8, 12);
}

I read different topics and I think my issue maybe here :
#define mySerial Serial1
because I read in one post that the Rx/Tx pins on the UNO WiFi Rev4 are Serial1, not Serial.

But I dont understand why it is only working when i am plugged in USB to my PC

Thank you again for your help, sorry in advance I am still learning Arduino and have a good day :slight_smile:

Welcome to the forum

Do yourself a favour and don't use SoftwareSerial on the Uno R4 as it has 2 hardware UARTs

Connect the fingerprint sensor to pins 0 (RX) and 1 (TX) and use the Serial1 interface to communicate with it. This interface is completely separate from the Serial interface used by the Serial monitor and to upload code to the board

Thank you for your answer. I already did the change and i am using serial1 in my code. My problem now is when I use an external power supply (Outup 9-12.6V 4A) the fingerprint is not detected anymore.
As you explained to me well they are 2 UARTs (One in USB and one on pin 0 an 1) and i am wondering if the issue is here. But I am not knowledgable yet to understand fully everything

Thank you again

Please post a schematic showing how the components of your project are connected

Have the fingerprint sensor and the Arduino got a common GND connection ?

Hello,

yes indeed the fingerprint and the 5V relay have a commong GND on the board. I tried to move one to the other GND but it doesnt change anything

Hello everyone,

In the end, I did not find the issue but I used a buck converter to lower my 12V to 5V and then I powered my Arduino via USB-C and it worked :slight_smile:

I am still wondering why it was not working when using the power barrel.. If anyone knows..

Thank you for your help @UKHeliBob