# Biometric Lock Box

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/003/267/medium800/biometric_2012_12_28_IMG_1097-1024.jpg?1396794656)

The Biometric Security Box uses the Adafruit Fingerprint Sensor as well as a numeric pass-code and a mechanical key-lock to provide triple-security. &nbsp;This tutorial is based on a 4x6 card file box, but can be easily adapted to larger lock-boxes&nbsp;– or even a full-size door!

![](https://cdn-learn.adafruit.com/assets/assets/000/003/268/medium800/biometric_2012_12_28_IMG_1099-1024.jpg?1396794663)

# Biometric Lock Box

## Parts and Preparation

Materials:

- Lock Box&nbsp;- This tutorial uses a "Vaultz" 4x6" card file box. &nbsp;But it can be adapted to fit other sizes or brands.  
- [Adafruit Menta](https://www.adafruit.com/products/795) - or other Arduino compatible microcontroller.  
- [Fingerprint Sensor](https://www.adafruit.com/products/751)&nbsp;  
- [Membrane Keypad](https://www.adafruit.com/products/419)  
- [Lighted Switch](https://www.adafruit.com/products/482)  
- [12v power adapter](http://www.adafruit.com/products/798)  
- [Panel mount power jack](http://www.adafruit.com/products/610 "Link: http://www.adafruit.com/products/610")  
- [MOSFET](https://www.adafruit.com/products/355)  
- [Diode](https://www.adafruit.com/products/755)  
- [Small push-pull solenoid](https://www.adafruit.com/products/412 "Link: https://www.adafruit.com/products/412")  
- Brass Strip - 1/2" x .032"&nbsp;&nbsp;  
- Pop Rivets - 1/4"  
- [Wire](https://www.adafruit.com/index.php?main_page=adasearch&q=hook-up+wire)  
- [Header strip](https://www.adafruit.com/products/392)
- [Heat-shrink tubing](https://www.adafruit.com/products/344 "Link: https://www.adafruit.com/products/344")  
- Wood  
- Cardboard  
- Adhesive Felt  
- Misc tape, screws, cable ties as needed.

## Remove lining from Lid
Carefully peel the fabric lining from the inside of the box lid. &nbsp;You can discard the lining.  
![biometric_2012_12_28_IMG_1114-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/246/medium640/biometric_2012_12_28_IMG_1114-1024.jpg?1396794482)

## Lay out components
Arrange the major components in the lid and mark their positions. &nbsp;Be sure to leave room for wiring and spacers to support the false lid.  
![biometric_2012_12_28_IMG_1117-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/247/medium640/biometric_2012_12_28_IMG_1117-1024.jpg?1396794492)

## Measure and mark cutouts
Mark the cutouts for the fingerprint sensor, power switch and keypad cable on the top of the lid. &nbsp;Also mark the position for the power jack on the back of the box.  
![biometric_2012_12_28_IMG_1115-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/248/medium640/biometric_2012_12_28_IMG_1115-1024.jpg?1396794498)

## Cut the cable slot
Use an X-Acto or utility knife to cut a rectangular slot for the keypad cable.![biometric_2012_07_02_IMG_0356-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/249/medium640/biometric_2012_07_02_IMG_0356-1024.jpg?1396794507)

## Drill the holes
Drill holes for fingerprint sensor, power switch and power jack.![biometric_2012_07_02_IMG_0354-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/250/medium640/biometric_2012_07_02_IMG_0354-1024.jpg?1396794515)

## Cut a false lid
We will use a false lid to hide the circuitry inside the lid of the box. &nbsp;Cut a piece of 1/8" (3mm)&nbsp;plywood or hardboard to fit the inside of the lid. &nbsp;Also cut some 0.85" (22mm) spacer blocks to go under&nbsp;the false lid.  
![biometric_2012_07_04_IMG_0369-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/251/medium640/biometric_2012_07_04_IMG_0369-1024.jpg?1396794525)

# Biometric Lock Box

## Circuitry

## The Microcontroller
The box in the photographs uses a&nbsp;[Menta](http://www.adafruit.com/products/795)&nbsp;without the mint tin (i.e. a&nbsp;demented Menta). &nbsp;However, the code and circuitry will also work with an [UNO](https://www.adafruit.com/products/50),&nbsp;&nbsp;[Boarduino](http://www.adafruit.com/products/72)&nbsp;or any other Arduino compatible microcontroller. &nbsp;If you are using the Menta&nbsp;or Boarduino, first build it according to the appropriate tutorial.  
## The Circuit
Connect the components as shown in the diagram below. &nbsp;  
  
If you are using a Menta, the pin locations are the same as for the UNO in the diagram, you can mount the MOSFET and Diode and a header for the keypad connector in the prototyping area.  
  
If you are using a Boarduino, be aware that the pin locations will be different from the diagram.

![](https://cdn-learn.adafruit.com/assets/assets/000/003/245/medium800/biometric_Biometric_Box.jpg?1396794474)

# Biometric Lock Box

## Pre-Assembly

## Install the keypad and switch
If the adhesive on the keypad&nbsp;does not stick well to the textured box surface, you can fix the corners down with small screws or nylon snap-rivets.![biometric_2012_07_28_IMG_0385-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/256/medium640/biometric_2012_07_28_IMG_0385-1024.jpg?1396794577)

## Install Circuitry
Position components inside the lid and glue spacer blocks around them.&nbsp;  

- Use foam tape to hold the fingerprint sensor in place.  
- Place extra spacer blocks in the center&nbsp;under the solenoid location.  
- Use tape and/or cable ties to hold wires in place.

![biometric_2012_12_28_IMG_1107-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/260/medium640/biometric_2012_12_28_IMG_1107-1024.jpg?1396794599)

## Fit the false lid
Make cutouts for the power cable, FTDI header and clearance for the switch.  
  
_ **Note** : &nbsp;A smaller switch would fit better in a box this size, but these metal lighted switches look SO awesome we couldn't resist!_![biometric_2012_12_29_IMG_1132-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/261/medium640/biometric_2012_12_29_IMG_1132-1024.jpg?1396794607)

## <u></u>Cover the false lid
Apply adhesive felt to the false lid. &nbsp;Cut flaps around the FTDI header access and&nbsp;the switch and leave a cutout for the solenoid footprint.![biometric_2012_12_28_IMG_1110-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/254/medium640/biometric_2012_12_28_IMG_1110-1024.jpg?1396794559)

## Install the false lid and solenoid
  
Postion the solenoid with the tip of the shaft just even with the inner edge of the box lid.  
  
Use wood screws to fasten the solenoid. &nbsp;Make sure they are long enough to go through the false lid and fasten securely to the space blocks underneath.![biometric_2012_12_28_IMG_1105-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/255/medium640/biometric_2012_12_28_IMG_1105-1024.jpg?1396794567)

# Biometric Lock Box

## Program and Test

## Download Fingerprint and Keypad&nbsp;Libraries

- Download the [Adafruit Fingerprint Sensor Library](https://github.com/adafruit/Adafruit-Fingerprint-Sensor-Library) and install it according to the instructions in readme.txt.  
- Download the Keypad Library and install according to the instructions [here](http://playground.arduino.cc/Code/Keypad).

## Enroll Fingers

- Load the "enroll" example into the Arduino IDE.  
- Change the mySerial pin definitions to:&nbsp;"SoftwareSerial mySerial(13, 12);"  
- Follow the [tutorial for enrolling fingerprints](http://learn.adafruit.com/adafruit-optical-fingerprint-sensor/enrolling-with-arduino "Link: http://learn.adafruit.com/adafruit-optical-fingerprint-sensor/enrolling-with-arduino").

## Load code

- Load the Biometric Box sketch at the bottom of this page.  
- Edit the "secretCode" string to define your passcode.  
- Compile and upload the sketch.  

## Test the Latch Operation

- Power on the box - the led on the power switch should flash a few times, then start to&nbsp;'breathe'.  
- Enter your passcode - the fingerprint sensor should turn on and glow red.  
- Place your finger on the sensor and you should hear the solenoid click.

```auto
/*************************************************** 
  Biometric Box Sketch for the optical Fingerprint sensor
  This sketch implements a two-level security scheme requiring the
  user to enter a passcode via the keypad before scanning a fingerprint
  for access.

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Bill Earl for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/
#include <Keypad.h>
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

// Define the states for the lock state machine
#define LOCKED 2
#define PASSWORD_OK 1
#define UNLOCKED 0

// State Variables:   Initialize to the locked state
int LockState = LOCKED;
long StartTime = 0;
int position = 0;

// Define your password key sequence here
char* secretCode = "1423";

// Keypad key matrix:
const byte rows = 4; 
const byte cols = 3; 
char keys[rows][cols] = 
{
   {'1','2','3'},
   {'4','5','6'},
   {'7','8','9'},
   {'*','0','#'}
};

// Keypad pin definitions
byte rowPins[rows] = {2, 3, 4, 5}; 
byte colPins[cols] = {6, 7, 8};  

// Instantiate the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

// More pin definitions:
int LedPin = 10;
int SolenoidPin = 11;

// Define a Fingerprint sensor on pins 12 & 13
int getFingerprintIDez();
SoftwareSerial mySerial(13, 12);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);


void setup()                    
{
   pinMode(LedPin, OUTPUT);
   pinMode(SolenoidPin, OUTPUT);
   
   // Flash hello
   for (int i = 0; i < 3; i++)
   {
     digitalWrite(LedPin, HIGH);
     delay(100);
     digitalWrite(LedPin, LOW);
     delay(100);
   }
   
   // Initialize state and communicatins
   setLockState(LOCKED); 
   Serial.begin(9600); 
   finger.begin(57600);

   // Connect to the sensor
   if (finger.verifyPassword()) 
   {
      Serial.println("Found fingerprint sensor!");
   } 
   else 
   {
      Serial.println("Did not find fingerprint sensor :(");
      while (1);
   }
}


void loop()                    
{
   // Run the state machine:
   
   // Locked State - Monitor keypad for valid Password code entry
   if (LockState == LOCKED)
   {
      char key = keypad.getKey();

      if (key == '*' || key == '#')
      {
         position = 0;
         setLockState(LOCKED);
      }
      if (key != 0)
      {
         if (key == secretCode[position])  // Valid key in Password sequence
         {
            Serial.print("Matched ");   
            Serial.print(key);   
            Serial.print("-at-");   
            Serial.println(position);   
            position ++;
         }
         else  // Invalid key - start all over again
         {
            Serial.println("Invalid Code!");   
            position = 0;
         }
      }

      // Let the LED 'breathe' while we wait
      analogWrite(LedPin, sin((millis() % 3142) / 1000.0) * 255);

      if (position == 4)  // Password successfully entered - advance state
      {
         setLockState(PASSWORD_OK);
         position = 0;
      }
      delay(100);
   }

   // PASSWORD_OK state - Now wait for a valid fingerprint reading
   else if (LockState == PASSWORD_OK)
   { 
      if (getFingerprintIDez() != -1)
      {
         setLockState(UNLOCKED); // Valid fingerprint - advance state to UNLOCKED
      }
      if (millis () - StartTime > 5000) 
      {
         setLockState (LOCKED); // Time-out - go back to the LOCKED state
      }
   }

   // UNLOCKED state - hold the solenoid open for a limited time
   else if (LockState == UNLOCKED)
   { 
      for (int i = 0; i < 30; i++)
      {
         // Flash the led to indicate the lock is open
         digitalWrite(LedPin, LOW);
         delay(50);
         digitalWrite(LedPin, HIGH);
         delay(50);
      }
      setLockState (LOCKED);  // Time-out - go back to the locked state.
   }
}



// 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; 
}


// Set the state and the time of the state change
void setLockState(int state)
{
   LockState = state;
   StartTime = millis ();
   if (state == LOCKED)
   {
      Serial.println("Locked!");
      digitalWrite(LedPin, HIGH);
      digitalWrite(SolenoidPin, LOW);  
   }

   else if (state == PASSWORD_OK)
   {
      Serial.println("PASSWORD_OK!");
      digitalWrite(LedPin, LOW);  
   }    
   else if (state == UNLOCKED)
   {
      Serial.println("Unlocked!");
      digitalWrite(LedPin, LOW);
      digitalWrite(SolenoidPin, HIGH);      
   }
}
```

# Biometric Lock Box

## Final Assembly

## Make the strike.
Cut a strip of brass about 2.5" (64mm) long.  

- Measure the distance from the top of the solenoid shaft to the edge of the box lid - typically&nbsp;about 0.44" (11mm).  
- Drill a .31" (8mm)&nbsp;hole in one end for the solenoid shaft. &nbsp;The edge of the hole should be 11mm from the end of the strip.  
- Drill 2 more 1/8" (3mm) holes for rivets.  
- Bend the strike above the hole&nbsp;as shown below.

![biometric_Lockbox_Strike.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/269/medium640/biometric_Lockbox_Strike.jpg?1396794582)

## Temporarily install the strike

- Position the strike on the front of the box and align the top with the edge of the box as shown.  
- Fasten it temporarily with machine screws. &nbsp;Make sure you can unfasten it from outside the box. &nbsp;We will need to open the box for adjustments&nbsp;if the latch binds.  

![biometric_2012_12_28_IMG_1104-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/258/medium640/biometric_2012_12_28_IMG_1104-1024.jpg?1396794590)

## Test latch operation
If the latch binds or fails to engage the strike, shim the solenoid and/or adjust the position of the strike as needed.![biometric_2012_12_28_IMG_1097-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/262/medium640/biometric_2012_12_28_IMG_1097-1024.jpg?1396794613)

## Rivet the strike
Once you have verified that everything is positioned for proper latch operation, make it permanent and tamper-proof&nbsp;by replacing the screws with pop-rivets.![biometric_2012_12_29_IMG_1127-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/263/medium640/biometric_2012_12_29_IMG_1127-1024.jpg?1396794620)

# Biometric Lock Box

## Use It!

## Turn it on

- Press the power button until it clicks in the on position.  
- The led in the power button will blink three times, then start to 'breathe'.  

![biometric_2012_12_28_IMG_1095-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/264/medium640/biometric_2012_12_28_IMG_1095-1024.jpg?1396794629)

## Enter the passcode

- Type in the passcode you programmed on the keypad.&nbsp;  
- If you enter it successfully, the fingerprint sensor will light up.  

![biometric_2012_12_28_IMG_1097-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/265/medium640/biometric_2012_12_28_IMG_1097-1024.jpg?1396794638)

## Scan your fingerprint

- Place your finger on the glass window of the sensor. &nbsp;  
- If the sensor recognizes a finger that you enrolled, you will hear the solenoid click and&nbsp;the led will start flashing rapidly. &nbsp;  
- You will have 3 seconds to open the box.  

![biometric_2012_12_28_IMG_1099-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/266/medium640/biometric_2012_12_28_IMG_1099-1024.jpg?1396794646)


## Related Guides

- [Bluetooth Controlled NeoPixel Lightbox](https://learn.adafruit.com/bluetooth-neopixel-lightbox.md)
- [Crickit Lab Shaker](https://learn.adafruit.com/crickit-lab-shaker.md)
- [Darth Faders](https://learn.adafruit.com/darth-faders.md)
- [reef-pi Guide 6: pH Monitoring](https://learn.adafruit.com/reef-pi-guide-7-ph-monitoring.md)
- [LED Protest Sign](https://learn.adafruit.com/led-protest-sign.md)
- [Robotic Xylophone with Adafruit Grand Central](https://learn.adafruit.com/robotic-xylophone-with-circuit-python.md)
- [DC, Servo, Stepper Motors and Solenoids with the Pico](https://learn.adafruit.com/use-dc-stepper-servo-motor-solenoid-rp2040-pico.md)
- [Adafruit Optical Fingerprint Sensor](https://learn.adafruit.com/adafruit-optical-fingerprint-sensor.md)
- [Bass MIDI Stomp Box](https://learn.adafruit.com/midi-bass-synth-stomp-box.md)
- [Glowing Interactive Crystal Staff ](https://learn.adafruit.com/glowing-interactive-crystal-staff.md)
- [Planetary Gear Dreidels](https://learn.adafruit.com/planetary-gear-dreidels.md)
- [LED Bullwhip with Motion & Sound Reactivity](https://learn.adafruit.com/led-bullwhip.md)
- [reef-pi Guide 5: Light Controller](https://learn.adafruit.com/reef-pi-lighting-controller.md)
- [MIDI Solenoid Drum Kit](https://learn.adafruit.com/midi-solenoid-drum-kit.md)
- [reef-pi Guide 7: Dosing Controller](https://learn.adafruit.com/reef-pi-guide-5-dosing-controller.md)
