Introduction: Arduino Light & Knock Detecting Door Lock

This is a great gadget for a secret clubhouse and for people like me who always seem to loose their keys! This project is based on the original Secret Knock Lock instructable by Grathio (available below) with the following main modifications:

1) Has a second secret input for when people are around who could overhear your knock. Now a photo-diode can register a "photo knock" (light flash) through the peep hole!

2) Includes an H-Bridge, giving it the ability to both Unlock and Lock a door (changes the direction of voltage across motor)

3) Programing now records the average (background) noise/sound levels and adjusts the sensitivity (threshold values) accordingly

***The information here is adapted from Grathio's instructable. Many thanks and credit go to Grathio Labs for making such a comprehensive guide available to the rest of us! I will take you through the steps for programming and wiring the electronics and then send you to the step in Grathio's instructable for making a PVC door mount (sadly, I am still in the process of creating a functional case)

For Grathio's original Knock-Lock instructable, go to

Step 1: Tools, Supplies, and Skills

To complete this project you should be able to:
    Do basic soldering.
    Read a schematic.
    Have some knowledge of microcontrollers (I'll be using the Arduino.)


    Drill (ideally a drill press) and an assortment of drill bits.
    Saw capable of cutting PVC pipe. (ie: Pretty mcuh any saw.)
    Soldering iron and solder.
    Screw drivers.
    Heat-shrink tubing and/or electrical tape.
    Wire stripper.
    Safety glasses.

Other things you might find handy:  a ruler/tape measure, a multimeter, a breadboard, some tape, a magic marker, sand paper, files, hot glue.  And if you're like me a well stocked first aid kit.



    1  Arduino Uno (or similar) Microcontroller
    1  12v Gear reduction motor with high torque
    1  Small Microphone
    1  SPST momentary pushbutton. (normally "off" / "open")
    1  Red LED
    1  Green LED
    1  Photodiode
    1  Quadruple half H-Bridge SN754410NE chip
    1  100k ohm resistor (1/4 watt)
    1  10k ohm resistor (1/4 watt)
    1  1M ohm resistor (1/4 watt)
    2   560 ohm resistor  (Or whatever will run your red and green LED's at 5v)
    1  small piece of perf board.  5x15 holes or longer.
    1 12V Wall Adapter plug for Arduino

It's also a good idea to have a breadboard for setting up and testing the circuit before you solder it.  We'll be doing this in step 3.

(This design comes directly from Grathio source.)

    20" PVC  Pipe 1/2".
    3   right angle 1/2" PVC connectors.
    1   5-way 1/2" PVC connector. (example)
    2   1/2" PVC end plug.
    3   1 1/2" suction cups. (Available at hardware stores and craft centers.)  NOTE: If your door is unsuitable for suction cups then replace these with three end caps and you can use adhesive strips or screws to mount the lock.
    6"  of 1/2" wide by 1/64" thick metal strip (steel, tin, copper, etc.) (available at hardware, craft, and art supply stores.)
    4.5" of 1" wide metal sheet, 1/32" thick (steel, tin, copper, etc.) (available at hardware, craft, and art supply stores.)
    2  3/32" x 3/8" screws with nuts. (1/8" will work too if you can't find the smaller ones.)
    2  1.6M (metric) 16mm screws. Ideally with countersunk heads if you can find them. (For securing the motor.  Check your motor specs to see what screws it needs. One motor I tried used 1.6M, the other 2M.  You'll probably have to buy long ones and cut them to length.)
   1 Large Suction Cup (for photodiode)

Step 2: Program Your Arduino

You will want to upload our sketch before doing any of the electronics so we can test them as we go. The code is attached below and has comments throughout. You need to have your Arduino software/drivers installed for this step!

1) Download file attached below

2) Connect your Arduino to a USB input on your computer.

3) Open the file in an Arduino Sketch (or if this gives you trouble, change the file to a .txt and copy/paste the code into an empty sketch) and compile/verify it

4) Upload it to your Arduino!

In the steps to come, I will provide troubleshooting tips involving specific parts of the code (certain parameters will need to be tweaked for each system). For anyone interested in the specifics of the code, I've included a summary below. The sketch is well commented on throughout, but it is a sizable file and a bit clumsy to interpret at times (feel free to make this more elegant, and be sure to share any success in the comments!).

Lines 4-33: Defines the Arduino pins, constants, and variables we will use in the code.
-  You will probably want to tweak these for your particular door/system. I will guide you through this

Lines 35-47 (setup): Defines pin modes and initiate serial monitoring

Lines 49-84, (loop): Defines the behavior of the circuit as it waits to hear (see) a knock (flash)
-  This is where we set up the background/averaging feature, which continuously samples data and calibrates threshold values accordingly. If a light or sound signal is registered above the threshold value, then it executes "listenToSecretKnock"

Lines 86-161 (listenToSecretKnock): Records the knocks registered by the microphone
-  Records the time between knocks (delay length) in an array and sends the data to validateKnock. Part of this section is dedicated to directing the LED indicators, which blink along in time with the registered knocks. If the lock is verified, the door will lock or unlock the door when depending on the "doorState" (which tells if our was last locked or unlocked).

Lines 163-238 (listenToSecretPhotoKnock): Records the light flashes registered by the photodiode
-  Basically the same as lines 86-161, but now with the photodiode signal/parameters. This is a good place to start if you're looking to streamline the code a bit more, since it repeats a lot of what is used earlier (on my todo list)

Lines 239-255 (triggerDoorUnlock): Turns the motor on to unlock the door
-  Understanding this section has to do with knowing the behavior of an h-bridge, which dictates the direction of voltage across the motor. You may want to look up the speck sheet for your chip to understand more (this was the one I used:

Lines 257-272 (triggerDoorLock): Runs the motor in reverse to lock the door
-  This section also repeats some coding and might benefit from a little cleaning up.

Lines 274-349 (validateKnock): Checks if the recorded pattern is a match to the key.
-  This normalizes each knock to the longest pause and compares the relative intervals between knocks. This means you can do the same pattern slow or fast, and it should still trigger the motor.

Step 3: Testing the Circuit on a Breadboard

We're going to hook up all the electronics to a breadboard and check that everything works as we go. You can find the breadboard schematic and a layout diagram attached in the images above. BE CAREFUL NOT TO LET ANY WIRES TOUCH UNINTENTIONALLY AND REMEMBER TO UNPLUG YOUR ARDUINO/CIRCUIT FROM POWER SOURCES WHILE WIRING!

1) Wire the Microphone
Solder a pair of 12" (30cm) leads to the microphone.  Connect it between Analog pin 0 and the ground.  Also attach the 1M ohm resistor between Analog pin 0 and the ground.

TEST: With your Arduino plugged into your computer, open the Serial Monitor window (on the top right of sketch window). You should see the text "Program start."  Tap the microphone and it should say "knock starting" and "knock" each time you tap it.  Stop for a second or two and you'll probably see "Secret knock failed." Try tapping Shave and a Haircut and see if you can get the "Door Unlocked!" message! Next, the arduino is listening for a signal to lock the door. The default for this (changeable in the code) is four equally spaced knocks.

-  If you see nothing or junk from serial monitoring, make sure your serial port is set to 9600 baud and reset the power
-  To adjust the sensitivity of recognizing/validating a knock pattern, play with the values of "rejectValue" and "averageRejectValue" (lines 13 and 14). If an individual knock is off by more than the rejectValue (percentage) or if the average timing of the knocks is off by more than the averageRejectValue, the arduino won't verify the knock or trigger the motor
-  If a single knock is being registered more than once, or two knocks are registered as the same knock, adjust "knockFadeTime" (line 15) which sets the milliseconds we allow a knock to fade before we listen for another one (an effective debounce timer).

2) Wire the Photodiode
Connect the anode side (usually the one with a shorter wire) to 5V and the cathode to analog pin 3 and to ground through a 100K resistor (see schematic).

TEST: Shine a flash of light on your photodiode and in the serial montor window you should see the text "photo knock starting" and "photo knock" each time you flash the light. Try shining the light flashes in the same "Shave and a Haircut" pattern as you did with the microphone taps.

-  If you don't see anything when you flash a light on the diode, try testing the voltage across the 100k resistor and make sure it is behaving as expected (voltage goes up to +5V when saturated with light and to 0 when dark).
-  If a single flash is being registered more than once, or two flashes are registered as one, adjust "photoknockFadeTime" (line 16)

3) Wire up the LEDs
Lets wire up some LEDs so we don't have to use a serial cable to see what's going on. Connect the red LED to digital pin 4 and green LED to digital pin 5 with their corresponding 560* ohm resistors in line.

TEST: If you power the circuit the green LED should light and dim momentarialy for each knock.  If the arduino verifies the correct sequence, the green led should blink a few times. The wrong sequence should blink the red one.

-  Check the polarity on your LEDs and all of your connections. Your LEDs might require different resistance to run on 5V, so if they are too dim or off you can try lowering the resistance.

4) Wire the programming button
Solder 8" leads to the button.  Connect one side of the button to +5v.  With the other pin on the button, connect to digital pin 2 and through a 10K resistor to Ground.

TEST: Apply power.  When you press the button the red light should come on.  Hold down the button and tap a simple sequence. When tapping while programming both LEDs should blink.  When you're done the pattern you just tapped should repeat on both lights.  After playback is complete, the new knock code is saved and the lights wil alternate red and green to tell you so.

-  Make sure your button is usually open ("momentarially closed") by using an ohmmeter to record the resistance with the button unpressed. If this returns zero or a low value, you have the wrong type of pushbutton.

5) Wire in the h-bridge and motor
For the h-bridge, connect: pins 1 and 16 to +5V; pins 4 and 5 to GND, pin 2 to Arduino digital pin 3, pin 7 to arduino digital pin 6, and pin 8 to arduino Vin. Solder 8" of leads to the motor and connect these to the h-bridge pins 3 and 6.

TEST: Power the circuit.  Correctly tap the default "Shave and a Haircut" knock, and the motor should run. Tap the correct pattern again and the motor should turn in the opposite direction.

-  Make sure you're chip is powered and grounded.
-  Check which way the motor turns.  The first time it executes, it should turn the same way as you turn your deadbolt to unlock it.  If not, switch the motor's leads which should reverse the motor.
-  To get your motor to turn 180 degrees adjust "lockTurnTime" (line 17) till motor turns 1/2 revolution each time it is triggered

Congratulations!  You have made a working secret knock detector circuit!

Step 4: Building a Case and Photodiode Attachment

For detailed steps on making the door mount/case go here:

My mount is based on this design with a few modifications (I had access to some less common tools through my school, Pomona College). I stuck with the PVC design since it was lightweight and portable. I had access to equipment which allowed me to print a circuit board, so not many wires needed to be stuffed in the casing. After soldering the final circuit board, I taped the underside (which faces the arduino) to make sure nothing touched the arduino that wasn't supposed to. My motor didn't have gear reduction included, so we constructed a set of gears and an aluminum gear case. The microphone is secured to the end of the long metal strip (covered in pink tape for decoration).

TO MAKE PHOTODIODE ADAPTER, drill a hole in a suction cup slightly smaller than the size of your diode (so suction wont be compromised) and place the photodiode looking out of suction cup toward door surface. Leave long leads on the photodiode so it can span the distance between your peephole and the deadbolt.