Make: Projects

Keyless Lock Box

A wave of your finger opens this magic treasure chest.

  • By
  • Time Required: Example: 15 to 30 minutes
  • Difficulty: Moderate
Keyless Lock Box

That piggy bank is looking mighty low-tech these days, and you have to bash it open to get your cash. One piggy, one withdrawal. And then there are those cheesy, tin ”Wild West” lock boxes with the red combination dial. Not only do they lack wow factor, your granny could crack one in 30 seconds.

But stick an Arduino in a wooden box, along with a finger-operated sensor and small motor, and you’ve made a 21st-century treasure chest that’s suitable for a daily diary, petty cash, or even those special Rice Krispies recipes that your snoopy neighbor wants to steal.

There’s no key in this keyless electronic combination lock box; you just move your finger across a small optical window, and it’s “open sesame.” The combination to the lock is a secret movement pattern only you know.

How It Works

The keyless lock box uses a unique optical finger navigation (OFN) sensor as a combination decoder. The OFN sensor works much like an optical mouse, except it’s intended to be used in direct contact with your finger. They are used in handheld devices where a trackpad would be too large, but because they are more expensive than trackballs, they’re not common in consumer products.

Movement across the small surface of the sensor is converted to X and Y distance measurements — up, down, left, and right. Sequences of these movements make up the combination of the lock.

For this project I’m using the Parallax OFN module, which puts a commercial OFN sensor on a breakout board that provides connectors for power (3.3V to 5V), ground, and 6 signal lines. The OFN module uses 2-wire I2C to communicate with a microcontroller, and has additional I/O pins for such things as the momentary pushbutton switch that engages when you push the optical sensor down.

The locking mechanism uses a standard-size R/C servomotor that’s glued into the bottom of the box. To lock the box, the turning servo engages a metal arm attached inside the box’s lid. Turning the other way, it frees the bar, letting you open the lid.

An Arduino microcontroller works as the main brain of the lock box, handling all the communications with the OFN module, controlling the servo, and even making musical tones on a small piezo speaker.

For my box, I used a plain 8″ square cigar box from a craft store — no need to smoke a bunch of stogies. The wood is unfinished; stain or paint to suit. You don’t get Fort Knox with these boxes, but they’ll keep out the casual thief.



Step #1: Prepare the box.

Keyless Lock BoxKeyless Lock BoxKeyless Lock Box
  • To make cutting and drilling easier, detach the box lid by removing the top screws in the 2 hinges on the back. Use a hacksaw or razor saw to cut out a small chunk on the left side of the box for the OFN module, just wide enough to fit. Gently pry loose the knockout piece, being careful not to crack the bottom of the box.
  • Drill 1/8" mounting holes for the OFN module. To prevent the bit from pushing through and splintering the wood, press or clamp a piece of wood behind the hole while drilling.
  • Measure and mark the center of a 1/2" hole in the rear of the box, positioned so you can insert a barrel plug through the hole and into the Arduino’s power jack. First drill a pilot hole with a 1/8" bit. Follow that with a 1/4" bit, and finish with a step bit, stopping at the 1/2" mark.
  • Referring to Image 2, temporarily position the Arduino in the rear left of the box, on top of 1/8" nylon spacers. Make sure the Arduino power jack is in line with the rear hole you just drilled. Use a small nail or sharp pencil to mark the location for the 2 mounting holes indicated. Remove the Arduino and set aside.

Step #2: Make the locking mechanism.

Keyless Lock BoxKeyless Lock BoxKeyless Lock Box
  • Alignment of the locking parts is critical, so take this step slowly. Use a hacksaw to carefully remove the mounting flanges on both sides of the servo. Thread a 4-40×3/4" machine screw near the end of the included single-arm servo horn and use a 4-40 self-locking (nylon insert) nut to tightly secure the screw in place.
  • Attach the horn to the servo and slowly rotate the motor shaft to the center of movement. You’ll make fine-tune adjustments later, so leave off the screw that holds the horn to the servo shaft. Use a hot glue gun to mount the servo in the front left corner of the box.

Step #3:

Keyless Lock BoxKeyless Lock BoxKeyless Lock Box
  • To avoid unsightly screws on the top of the box, fasten the locking bar to a mounting block made of 1/4" aircraft-grade plywood or PVC inside the lid. The large surface area of the flat block will enable a strong glue joint.
  • Follow Image 1 to cut and drill a length of 1/2"-wide × 0.064"-thick brass strip, then cut the block to size, and drill holes matching those in the strip. Fasten the brass strip and block together with 4-40 flat-head machine screws. Bend down the front of the strip at a 15°–20° angle. You can adjust the angle later to achieve better locking action.
  • Stick a single 1/2" square piece of florist tape (putty) to the top of the block, and using your best guess, line up the locking bar so it engages with the screw attached to the servo horn. The putty keeps the mounting block in place until you can test the best placement on the lid. Reattach the lid to its hinges.

Step #4: Calibrate the servo.

Keyless Lock BoxKeyless Lock BoxKeyless Lock Box
  • Wiring makes or breaks a project. I used pre-crimped male/male and male/female jumpers, 1×1 header shells, and double-long breakaway header pins for robust and easily pluggable connections.
  • You could theoretically use the pin sides of 3 male/female jumpers to connect to the servo extension cable, but the double-long headers make a stronger friction-fit connection. Snap shells onto the jumpers to make 9 male/male jumpers with shells at one end, and 3 male/female jumpers with shells on both ends.
  • I needed to add a bypass capacitor to reduce electrical noise from the servo that caused the Arduino to keep resetting itself. Solder a 47µF tantalum capacitor between the middle and one side pin of a 3-pin double-long header.
  • Be absolutely sure that the + lead of the capacitor is connected to the center pin! These will connect to the servo’s voltage (V+) and ground (Gnd) pins.
  • To calibrate the servo so that it points in the desired direction, download and unpack the project code, then verify (compile) and upload the ServoCalibrate sketch to your Arduino. Visit the same link for instructions on how to upload programs to an Arduino.

Step #5:

Keyless Lock Box
  • Remove the horn from the shaft and plug in the 3-pin connector you just made, orienting the bypass cap on the black or brown wire (ground) side. Use 3 male/female jumpers to connect the servo and Arduino’s 5V and ground together, and the servo’s control (white or yellow) to Arduino digital pin 8.
  • Briefly depress the reset switch on the Arduino. When the ServoCalibrate sketch restarts, the servo first moves to its extremes, then centers itself at its midpoint and stops. After it’s done, unplug the Arduino from the PC, then reattach the horn to the servo so that it points straight up, and secure it in position with the small included screw.
  • CAUTION! Warning! Whoa, Nellie! Do not cross the polarity of the wires or your servo may be permanently damaged.

Step #6: Align the locking mechanism.

Keyless Lock Box
  • To align the servo horn with the brass strip, reconnect the Arduino to your PC and upload the ServoLock sketch.
  • Thread the servo cable through the opening in the side of the box, manually move the servo arm toward the front of the box, and close the lid.
  • Click the reset button to run the sketch and listen for when the servo stops moving. Then test the lock by trying to lift the lid.
  • Should the servo horn and screw not properly engage over the brass strip — it makes clunking or scraping noises — pop the mounting block off the lid, reposition it, and try again. When you get the position just right, unplug the servo and mark around the block with a pencil for gluing later.

Step #7: Install the OFN board, speaker.

Keyless Lock Box
  • The OFN module doesn’t have any direct means for mounting, so you need to solder it to a small circuit board that you can then attach to the side of the lock box using 4-40 machine screws and nuts. Cut the board to size and drill 2 holes for mounting.
  • I used a board with component layout, in which long horizontal bus traces alternate with 3-hole vertical segments. This layout makes it easier for making wire connections on the backside of the board with the copper foil, which is necessary because the OFN module will take up all the space on the front.
  • Following the Step 7 diagram, drill a 1/8" hole in the middle of the long trace at the top, to break the connection. This will separate the voltage and ground connections to the OFN module, which is very important.
  • Then solder the OFN module to the board from the top, non-foil side (the leads will poke through to the foil side). Orient it with the LED on the drilled trace side, along with voltage and ground connections, and the 6 signal lines along the opposite side.

Step #8:

Keyless Lock BoxKeyless Lock BoxKeyless Lock BoxKeyless Lock Box
  • Cut eight 1" lengths of 1/8"-diameter heat-shrink tubing, and slip one each over 8 of your prepared male/female jumpers. Solder the bare pin end of each jumper to its corresponding connecting point on the foil side of the board 6 signal lines in a row, and the power and ground on the other side, as seen in the diagram.
  • The pin will poke through; cut it flush with the front of the board after soldering. After all jumpers are soldered, push the heat-shrink tubing over the metal and apply heat to shrink.
  • Mount the OFN module and board to the side of the box using 4-40 fasteners.

Step #9:

Keyless Lock BoxKeyless Lock BoxKeyless Lock Box
  • Plug in the 8 jumpers from the OFN into the Arduino digital pins D2–D7 and analog pins A4–A5 as shown in Figure K. Be absolutely sure not to swap pins D6 and D7, or you’ll reverse the power to the OFN and possibly damage it.
  • Cut the remaining male/male jumper in half. Strip 1/8" of insulation from the cut ends, and solder them to the terminals of the piezo speaker. Use double-sided foam tape to mount the speaker to the bottom of the box, and attach the 2 jumpers to Arduino pins D11 and D12. The innards of your lock box should look like the 2nd image.

Step #10: Program it.

Keyless Lock Box
  • Reconnect your Arduino and then verify and upload the LockBox sketch. It includes several files in the same folder; make sure they remain in the folder or the sketch won’t work. You need version 1.0 (or later) of the Arduino IDE, or the sketch will not compile.
  • For now, keep the lid open so you can see what happens when the box locks and unlocks. Keep the Arduino connected to the computer and open the Serial Monitor window (Tools menu) so you can review the debugging messages.
  • On Arduino startup or reset, the servo should go into lock (straight up) position, and you should see “Device ready” in the Serial Monitor window. If instead it reads, “Device not found,” it means the Arduino can’t find the OFN module, and you should recheck the wiring.
  • Swipe your finger across the sensor of the OFN module sensor, then press it down. You’ll hear a series of tones indicating a bad combination entry.
  • Move your finger across the sensor again, this time using the pre-stored combination: Left-Right-Left. Click down the sensor, and this time the lock opens. After 7 seconds the lock automatically closes.
  • See the comments in the LockBox sketch on where to change the combination. It can be any length sequence of swipes up, down, left, and right. Once the lock is reprogrammed, unplug the Arduino from your computer. Reattach the Arduino to the bottom of the lock box.
  • Now’s a good time to glue the locking bar mounting plate permanently to the lid. Hot glue or wood glue works best. After the glue sets, stash your best secrets in your new keyless combination box. Whenever you need access, let your fingers unlock the mysteries within.


Lock Box Power!

The Keyless Lock Box is designed to use external power for unlocking, but it can sit in a locked state for indefinite periods without any power. To power the lock and open it, insert the plug into the hole in the back, enter the combination, then lift the lid.

The box will automatically relock after 7 seconds. If the lid is still open, you can relock the box by manually pushing the servo arm toward the front of the box, pressing the Arduino’s reset button, and closing the lid. When the sketch restarts, it automatically moves the servo to its lock position. You may then remove the power plug.

If you forget the combination or can’t get it to work, here’s a “secret” override procedure that will briefly unlock the box:

Unplug power to the Arduino. Press and hold down the OFN sensor while you plug the Arduino back in. Quickly open the lid when you hear the servo move to its unlock position. You’ll have only 1.5 seconds before the servo relocks the box.

You can remove or comment out the override code in LockBox.ino if you don’t want this behavior.

Keep the box looking good by applying paint or finish to the bare wood, starting with a base coat. For a cleaner look, remove the clasp in front of the cigar box, and fill in the holes with wood putty. Sand for a smooth finish. Or replace the clasp with something fancier.

This project first appeared in MAKE Volume 32, page 110.

Gordon McComb

Gordon McComb

Gordon McComb has been building robots since the 1970s and wrote the bestselling Robot Builder’s Bonanza. You can read his plans to take over the world with an army of mind-controlled automatons, along with other musings, at

  • rob

    i am just starting on home projects but i have had prior experience with electronics on a larger scale, but what i was wondering is if you could give me links to where i could get all of the components, if so that would be awesome

  • Erotic Ev

  • I am interested in adding some pictures to my comments. Is there a way to do that?

  • not only that you can substitute this as a piggy bank, but like a safe as well.. great for keeping important things, even documents, money, jewelry, etc.. simple project that brings up more ideas from a mere storage substitute to anything that’s wanted to be locked out or something.. thumbs up!

  • iso2013

    The code to download for this project is found here:

  • Julian

    how much do all the components to build this cost? roughly

  • Tim

    I can not find the links for the programming. Can you tell me where I can find them. or can you post them.

  • hossam

    hi, i written the programming but it did not work and i did not found OFN module in the serial monitor. can you help me please. the code is here: /*
    This code connects to and reads data over I2C from a Parallax OFN module.

    Device Info:,ProductName

    Arduino A4 connected to SDA
    A5 connected to SCL
    5V connected to VCC
    GND Connected to GND
    Removed these b/c they were causing errors with OFN (add ’em back if you can’t get it working)
    –> 10K Resitor connected from SDA to VCC and from SCL to VCC

    Parallax OFN Vdd connected to VCC
    GND Connected to GND
    CLK connected to SCL
    SDA connected to SDA

    Parallax OFN Register Table
    Addr Register Read/Write Default Value
    0x00 Product_ID R 0x83
    0x01 Revision_ID R 0x01
    0x02 Motion R/W 0x00
    0x03 Delta_X R Any
    0x04 Delta_Y R Any
    0x11 Res Config R/W 00000000=500cpi 10000000=1000cpi
    etc …

    The values returned when set to 1000CPI resolution will cover the range from -127 to -127
    *see below on how to change resolution by setting register 0x11

    Trying a new I2C library that simplifies communication compared to wire.h
    more info:
    //each device has a custom ID (parallax Unit is 0x33)
    char deviceID = 0x33;
    int result = 0;
    boolean MOT = false; //is motion bit state
    boolean OVF = false; //is OVF bit state
    char deltaX = 0; //holds Delta_X reading from OFN
    char deltaY = 0; //holds Delta_Y reading from OFN
    int deltaXI = 0; //holds corrected (signed) Delta_X reading
    int deltaYI = 0; //holds corrected (signed) Delta_Y reading

    void setup()

    I2c.begin(); // join i2c bus
    Serial.begin(9600); // start serial for output

    //if you need to chage the resoution of teh sensor uncomment the corresponding line below:
    //I2c.write(deviceID, 0x11, B10000000); //Set the Resolution to 1000CPI
    //I2c.write(deviceID, 0x11, B00000000); //Set the Resolution to 500CPI

    void loop()
    //read the Motion register to evaluate if MOT is set
    I2c.write(deviceID, 0x02); //send Register 2 address which is what we’d like to read I think, 1);
    result = I2c.receive(); //read the byte from the motion register

    //check if MOT bit is set
    //this works by shifting the bits over 7 spaces and checking if the result is 1 or 0
    if ((result & (1 << 7)))
    //Debug to see if MOT bit test is working
    //Serial.println("The Motion Bit is set.");
    MOT = true;
    } else MOT = false;

    //check if OVF bit is set
    //this works by shifting the bits over 4 spaces and checking if the result is 1 or 0
    if ((result & (1 < 127) deltaXI = deltaX – 255; //working 256
    else deltaXI = deltaX; //try 255 to get more sensitiveity?

    if (deltaY > 127) deltaYI = deltaY – 255;
    else deltaYI = deltaY;

    Serial.print(“delataX, DeltaY: “);
    //Serial.print(deltaX, BIN);
    Serial.print(deltaXI, DEC);
    //Serial.println(deltaY, BIN);
    Serial.println(deltaYI, DEC);

    } //end if

    delay(50); //delay for a moment to reduce the amount of readings


  • Prabhaker Nigam

    Hey, I am also working on a similar project and having problem with the code, do you have the working code?

  • Chris Cassone

    Can’t someone out there just make me one?