3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Secret Knock Detecting Door Lock

Step 2Program The Arduino

This section assumes that you know how to connect your Arduino microcontroller to you computer, compile and upload a sketch. If you don't know how to do that you probably shouldn't be doing this Instructable. But spending some time on this page and doing some of the examples and tutorials there might bring you up to speed.

We're going to upload our sketch before doing any of the electronics so we can test the electronics as we go.

#1: Download
Download the file secret_knock_detector.pde at the bottom of this section and copy it to your sketchbook. (Or view the text and cut and paste it into a new sketch.)

(Tip: If the name of the downloaded file is something like "BARS5HS13H8SW.tmp" simply rename it to secret_knock_detector.pde. and you're good to go.)

#2:
Open the sketch and compile it.  It should compile properly the first go, but it's good to be sure.

#3:
Connect your Arduino and upload the sketch.

If you have any trouble, check the troubleshooting section at the Arduino site.

Code overview:
For the curious, here's a look at a few bits of code if you're interested in tinkering:
(If you're not curious, go to the next section)

about Line 28: const int threshold = 4;

This is the sensitivity of the knock detector.  If you get a lot of noise, raise this (up to 1023), if you're having a hard time hearing knocks you can lower it (as low as 1).

about Line 29: const int rejectValue = 25;
about Line 30: const int averageRejectValue = 15;

Both of these are used to determine how accurately someone has to knock.  They are percentages and should be in the range of 0-100. Lowering these means someone must have more precise timing, higher is more forgiving.  averageRejectValue should always be lower than rejectValue.

Settings of about 10 and 7 make it hard for two people to knock the same knock even if they know the rhythm. But it also increases the number of false negatives. (ie: You knock correctly and it still doesn't open.)

about Line 31: const int knockFadeTime = 150;

This is a crude debounce timer for the knock sensor.  After it hears a knock it stops listening for this many milliseconds so it doesn't count the same knock more than once.  If you get a single knock counted as two then increase this timer.  If it doesn't register two rapid knocks then decrease it.

about Line 32: const int lockTurnTime = 650;

This is now many milliseconds we run the motor to unlock the door.  How long this should be depends on the design of your motor and your lock.  It's okay if it runs a little bit long since I've designed a simple slip clutch into the design, but it's better for all the parts if it doesn't run too much.

about Line 34: const int maximumKnocks = 20;

How many knocks we record.  20 is a lot.  You can increase this if your secret hideout is protected by devious drummers with good memories.  Increase it too much and you'll run out of memory.

about Line 35: const int knockComplete = 1200;

Also known as the maximum number of milliseconds it will wait for a knock.  If it doesn't hear a knock for this long it will assume it's done and check to see if the knock is any good.  Increase this if you're a slow knocker.  Decrease it if you're a fast knocker and are impatient to wait 1.2 seconds for your door to unlock.

about Line 39: int secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 5.....

This is the default knock that it recognizes when you turn it on.  This is weird rhythmic notation since every value is a percentage of the longest knock.   If you're having a hard time getting it to recognize "shave and a hair cut" change this to {100,100,100,0,0,0...  and a simple sequence of 3 knocks will open it.

Debugging:
about Line 51:  Serial.begin(9600);
about Line 52: Serial.println("Program start.");

Uncomment these lines to see some debug info on the serial port.  There are a few other lines of debugging code set throughout the rest of code that you can uncomment to see what's going on internally.

Be sure to set your serial port to the right speed.

The rest of the code is commented so you can see how it works but you probably won't need to change it if you aren't changing the design.

« Previous StepDownload PDFView All StepsNext Step »
20 comments
Feb 6, 2012. 10:03 AMJaslene100 says:
how did you figure out the array pattern for the "shave and hair cut" rhythm?
Oct 1, 2011. 3:30 PMayanez says:
i cannot download the code :/
May 17, 2011. 10:49 AMJordan arduino says:
Where do i find the code?
May 26, 2011. 4:02 PMJordan arduino says:
Thanks awesome
im 11 years old and i know how to program the arduino
you make the code really precise and pretty good
the last time i  did a progect i got confused but this is
clear:-o 
Mar 10, 2011. 10:11 AMnoobduino says:
would any of these moters work http://www.thesource.ca/estore/category.aspx?language=en-CA&catalog=Online&category=Motors i am Canadian (no radioshack for me only the source)
Mar 1, 2011. 9:07 AMjigsawnl says:
I'm just started with my Arduino Uno. This was a great tutorial to learn more about electronics and programming. After setting up the Arduino i thought about how I could improve it with the little bit of knowledge that i had gethered. I removed the two LED's and put a RGB instead. I added Digital 6 to the code. Now standby shows green. A false knock pattern red. And when you press the button to program it's becomes blue.

Thank you for this great instructable.

PS. i had some problems at first. Happens to be that my knock sensor is really hard to trigger. Even with threshold = 1. It's a really small knocksensor encased in plastic.
Jan 16, 2011. 11:54 AMPingman747 says:
First off, this is a great project. I could see alot of personal use for this project. As such, I'm in the process of recreating this instructable. In doing so I noticed a line of code I have a question about. I noticed around line 45 in the setup routine you setup the knockSensor pin as an OUTPUT. Is this intentional and I'm just being thick, or is this pin supposed to be an INPUT (what I suspsect)?

Thanks so much for the great project. I'll be watching you for any other great ideas.
Jun 28, 2010. 4:56 AMkriscristian says:
pls help.. can I use an atmel 8051 micro?? how would the programming be??
Jun 5, 2010. 8:15 AMeng.7oka says:
hey.. sorry to bother you again.. I just want to know what the function (map(knockReadings[i],0, maxKnockInterval, 0, 100); does..
Jun 5, 2010. 4:39 PMeng.7oka says:
I read that in the comment.. but the thing is I'm programming usuing another language.. so I need to understand the mathimatical algorithm behind it.
May 13, 2010. 1:45 AMeng.7oka says:
Ok..I Think I will use C ... I'm looking for C compiler for my PIC now... Thanks again
May 10, 2010. 1:22 PMeng.7oka says:
Thanks alot man.. it's really useful.. I have a small problem though. I'm using PIC 18F series. How can I modify the program
Dec 13, 2009. 10:05 AMBSprague says:
 Thanks for this fantastic guide, I just modified it so that I have a laser activated door lock, which works by breaking the beam to a set rhythm. It works fantastically, and people can't figure it out simply by hearing it. ;)
Dec 14, 2009. 4:43 PMBSprague says:
 My camera is broken, but I'm picking up a new one in a couple weeks hopefully. I'll definitely record it doing it's thing and I'll send you a link.
Nov 25, 2009. 7:08 AMDemonSpawn says:
 I cant down load the code. it downloads as a tmp. could you post it please

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
187
Followers
14
Author:Grathio(Grathio Labs)
Creative swashbuckler. Writer for MAKE Magazine, presenter of inventions on TV, radio, magazines and newspapers. Professional problem solver. Annoyingly curious. Hacker of all things from computers ...
more »