Source: https://mokxi.com/learn/arduino-fingerprint-sensor

Learn

# An Arduino fingerprint door lock

Intermediate

Written by the Mokxi team

Start building free Open the editor

Or see every lesson.

- 455 parts on the bench
- 43 boards running now
- 1.00x real time, on every board

Click to open it in the editor

A fingerprint sensor on SoftwareSerial, a servo latch and a traffic light, on a simulated ATmega328P. Fingers 1 and 2 are enrolled.

An optical fingerprint sensor such as the R307 or the AS608 does the hard part by itself. It photographs the finger on its glass, turns the picture into a template, stores up to a few hundred of them in its own memory, and searches them when asked. The Arduino only sends short commands over serial and reads back one answer: which finger it was, or none.

The circuit above is an Uno with a fingerprint sensor, a servo latch and a traffic light module. The door starts locked and red. Hold one of the fingertips under the sensor: fingers 1 and 2 are enrolled and swing the latch open for three seconds on a green light, and finger 3 is a stranger who gets a blinking red. It runs in your browser on the sketch below.

## What you need

- An Arduino Uno
- An optical fingerprint sensor (R307, AS608 or the Adafruit one)
- A servo for the latch
- A traffic light LED module, or three LEDs with resistors
- A breadboard and jumper wires

## Wiring

Part and pin | Goes to | Note

Sensor TX | Pin 2 | Into the Uno: SoftwareSerial’s receive pin

Sensor RX | Pin 3 | Out of the Uno: SoftwareSerial’s transmit pin

Sensor VCC and GND | 5 V and GND | Check your module: some small boards want 3.3 V

Light R, Y, G | Pins 12, 11, 10 | The module has its own resistors

Servo signal | Pin 9 | Servo power from 5 V and GND

## What the sensor does by itself

Inside the sensor is a camera, a processor and flash memory. getImage() asks it to take a picture of whatever is on the glass. image2Tz() turns that picture into a feature file in one of its two buffers. fingerFastSearch() compares the file against every stored template and, if one matches, puts its number in finger.fingerID.

The Uno never sees a fingerprint. It sends commands in small packets at 57600 baud and gets back a status byte: FINGERPRINT_OK, FINGERPRINT_NOFINGER, or an error. That is why a fingerprint lock fits on an Uno with plenty of room to spare.

## The sketch, step by step

The sensor talks over serial, but the Uno’s one hardware serial port is busy with the serial monitor, so the sketch makes a second port on pins 2 and 3 with SoftwareSerial. finger.begin(57600) opens it, and verifyPassword() checks that a sensor answers. Then getTemplateCount() asks how many fingers are stored, and the first line says "Sensor ready, 2 fingers enrolled".

loop() calls getImage() until it returns FINGERPRINT_OK, which means a finger is on the glass. The light turns yellow while it searches. A match prints "Welcome, finger #1", turns the light green and swings the servo to 90 degrees for three seconds, then prints "Locked". No match prints "Access denied" and blinks the red light three times. Either way, the sketch waits for the finger to lift before it looks again, so one touch is one try.

The loop, from the fingerprint door lock

## Enrolling a new finger

A finger has to be enrolled before it can be found. The steps are: take an image and convert it into buffer 1 with image2Tz(1), ask the person to lift and press again, convert the second image into buffer 2 with image2Tz(2), then createModel() to combine the two and storeModel(id) to save it under a number. Adafruit’s library comes with an enroll example that walks through exactly this.

The sensor here starts with fingers 1 and 2 already stored, set by its enrolled property. It models enrolling too: two images of the same finger, combined and stored, are found by a search afterward and the template count goes up. Two different fingers will not combine.

## What the simulation leaves out

A finger here is a number, and two images match when they are the same finger. A real sensor scores how alike two prints are, and a dry, wet or misplaced finger can fail to read or fail to match. Real searches also take a fraction of a second; here every answer comes back after 5 milliseconds. Plan for retries on a real lock.

A real lock also needs a backup way in, such as a key or a code, and a latch that stays shut if the power fails. Those are design choices the simulator cannot make for you.

## Common mistakes

TX and RX swapped. The sensor’s TX goes to the Uno’s receive pin, here pin 2. Swap the two wires in this circuit and the sketch prints "No fingerprint sensor found" and blinks red forever.

The wrong baud rate. Most sensors ship at 57600. If verifyPassword() fails with the wiring right, try 9600 or 115200.

The wrong supply voltage. The R307 and Adafruit’s sensor take 5 V; some small AS608 boards want 3.3 V. Check the listing for yours.

Calling image2Tz() without a fresh getImage(). It converts the last picture, whatever finger that was.

Powering a big servo from the Uno’s 5 V pin. A latch servo under load can pull the voltage down and reset the board. Give it its own supply with a shared ground.

## Questions

How do I connect a fingerprint sensor to an Arduino Uno?

Power and ground, then the sensor’s TX to a pin you use as SoftwareSerial receive (pin 2 here) and its RX to the transmit pin (pin 3). Open it with finger.begin(57600).

Which library works with the R307 and AS608?

The Adafruit Fingerprint Sensor Library. Both sensors use the same command set, so getImage(), image2Tz(), fingerFastSearch() and fingerID work on either. Mokxi includes it.

How many fingerprints can the sensor store?

It depends on the module. Adafruit’s sensor reports 162 templates, and the R307 stores up to 1000. Each one is saved under a number you pick when you enroll it.

Where are the fingerprints stored?

In the sensor’s own flash memory, not on the Arduino. Unplug the Uno and the enrolled fingers are still there; swap the Uno for another and they still work.

Related

## Keep going

Arduino 4x4 Keypad Code Lock with a Relay Arduino Servo Motor with a Potentiometer UART Serial Explained: Baud, Frames and the Wire Arduino Relay Module: Active LOW and Startup Clicks How the fingerprint sensor is modeled The servo, pin by pin The Arduino Uno simulator The project page, with the full sketch Intermediate

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building free Open the editor
