Step 2: Program 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.)

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

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.

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.

I have made a prototype, still have to assemble it to my door. I used a self made relay module to power my motor. Thanks for the instructable...... it works like a dream.
<p>My version is installed in a fridge and using an electromagnetic lock - keep those office thieves away! </p><p>http://ink361.com/app/users/ig-240858804/jas_cal/photos/ig-870056726990911200_240858804</p>
<p>Guys - thank you for this tutorial! I used the code to program a secret sliding library door project and it works great. The secret knock is the part people like the best. People giggle when I show it to them. I owe you a beer!</p><p>http://www.instructables.com/id/Automatic-Secret-Library-Door/</p>
Fantastic work! I love how well the bookshelf hides your secret room. Never would have guessed it was a pocket door.<br><br>Thanks for sharing!
<p>Here we are! Finally done after months of procrastination! I added a buzzer and a third led (not in the picture). The code was changed very slightly but it works perfectly. A latch in the back hangs it on the door. I must thank you, however for the genius of this invention.</p>
<p>hi sir, how did you do this one. where do i put the new code for the led and the piezo.. please, needed for a presentation, our prof. did not give us enough time to master this. and it serves as our final exam.. thank you in advance..</p>
<p>Beautiful work! Thanks for sharing!</p>
<p>here is my little one in an iphone box<iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/HhrNM2kSQzk" width="500"></iframe><br></p>
I have modified the Arduino code to add the functionality of ''Users''. What it does is that it let you program a secretCode for four users. For example: If you push the button X times, after 4 seconds the green LED will blink X times and then you enter program mode (simply do your secret code). I also added the functionality to take a picture when your sequence is bad with LabVIEW and a WebCam camera. Then the picture is saved in my folder and pictures are incrementing with numbers.
Looks great, and really nice added features! I had done a version with his &amp; hers knocks, but this goes well beyond that!<br><br>Fantastic job!
I saw your project some time back and I thought it was great. I decided to take it and improve on it, the result was a system that locks and unlocks. Also if a person inputs an incorrect knock, the system will send a signal via bluetooth to a computer terminal to snap a picture and save it.<br> <br> <a href="http://www.youtube.com/watch?v=6k6XwyB8Hh0">http://www.youtube.com/watch?v=6k6XwyB8Hh0</a><br> <br> Check out the vid. Hope you like it.<br>
That really made my week! It looks fantastic and works great. And the bluetooth camera is a great idea of added security.
Hi Steve!<br /> As you know, I already started to do mine, with a few modifications. I uploaded a new video with a new option:<br /> <object height="344" width="425"> <param name="movie" value="http://www.youtube.com/v/d-HtuH90IMw&amp;hl=en_US&amp;fs=1&amp;hd=1" /> <param name="allowFullScreen" value="true" /> <param name="allowscriptaccess" value="always" /><embed allowfullscreen="true" allowscriptaccess="always" height="344" src="http://www.youtube.com/v/d-HtuH90IMw&amp;hl=en_US&amp;fs=1&amp;hd=1" type="application/x-shockwave-flash" width="425" /></object><br /> <br /> As soon as I finish my project I'll post the code online<br />
Did you finish yet?&nbsp; Can't wait to see the code.<br />
Fantastic work with the servo and the unlock option. &nbsp;(And an off&nbsp;switch!)<br /> <br /> It looks like your setup is really sensitive. &nbsp;Really nicely done! Your&nbsp;achievement&nbsp;patch is on the way!<br />
I have been playing around with this code for the past hour or so and found out how you can hard code in your own rhythm if need be.... at the part that looks like this<br /> // If we're recording a new knock, save the info and get out of here.<br /> &nbsp; if (digitalRead(programSwitch)==HIGH){<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i=0;i&lt;maximumKnocks;i++){ // normalize the times<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; secretCode[i]= map(knockReadings[i],0, maxKnockInterval, 0, 100); <br /> <br /> add this little guy<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(secretCode[i]);<br /> and open up your serial monitor....<br /> make sure you un-commented the serial begin up at the setup and then it should spit some numbers back at you... put these numbers into the array and enjoy.<br />
<object height="344" width="425"> <param name="movie" value="http://www.youtube.com/v/zBNTQkVUHvw&amp;hl=en_US&amp;fs=1&amp;" /> <param name="allowFullScreen" value="true" /> <param name="allowscriptaccess" value="always" /><embed allowfullscreen="true" allowscriptaccess="always" height="344" src="http://www.youtube.com/v/zBNTQkVUHvw&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" width="425" /></object>Whoot finally got my relays for my motor and heres a neat little Idea for your visual feedback....<br /> <br />
<object height="344" width="425"> <param name="movie" value="http://www.youtube.com/v/ZGCkVcrUNXM&amp;hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999" /> <param name="allowFullScreen" value="true" /> <param name="allowscriptaccess" value="always" /><embed allowfullscreen="true" allowscriptaccess="always" height="344" src="http://www.youtube.com/v/ZGCkVcrUNXM&amp;hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999" type="application/x-shockwave-flash" width="425" /></object>&nbsp;
Oh, and for your&nbsp;awesome&nbsp;work you have been added as an&nbsp;official&nbsp;Master of Secret Knocks. &nbsp;Your patch is on its way!
&nbsp;This is awesome! &nbsp;Thanks for sharing your code and video. &nbsp;Having the door unlatch too makes it that much more mysterious. &nbsp;And I like the feedback through the peephole.<br /> <br /> Great work!<br /> <br /> And if I ever have any questions about taping something to something else I'll be asking you. :)<br />
<p>Good one!</p><p>manoj</p><p>www.nrdcentre.com</p>
bro do u hav the codingfor the reset button
<p>Can I use a side button to generate a <strong>auto lock</strong> function?</p>
i would use it in the school locker
<blockquote>how to open it <strong>inside ?</strong></blockquote>
<p>how to control inside?</p>
<p>I really want to see a code and parts list . Can you give me a code and parts list? <br><a href="mailto:woong0111@koreatech.ac.kr" rel="nofollow">woong0111@koreatech.ac.kr</a> <br>Thank you</p>
Great job
<p>First of all, congratulations for this nice project.</p><p>Grathio, i'm a Brazilian student and i'm going to develop a similar object using Arduino at school, but i have one doubt. How can i use a digital knock sensor instead an analog piezo sensor? Is it very difficult to convert your code (using analog entries) to a code with digital entries(like ky-031)?</p><p>Thanks</p>
<p> Here's an adjunct idea for those more paranoid. As long as an arduino is employed, one can add an keypad for entering a secret code. But the code is not correct if it is not punched in on the right cadence.</p>
<p>For the lock clamp...why not get a large piece of PVC (large enough to encompass the lock), and then fill the innards with some sort of hardening putty. Press your lock knob into the putty enough to make a perfect indent, then let the putty harden. Then mount the motor to this somehow. Or you could use putty to make a mold of your knob, and make it from something stronger, like cast aluminum.</p>
<p>Geeeze, Just when I was about to give up on instructables because of the endless dumbass projects using duct tape and making fudge this brilliant piece of irreverent mind expansion comes along - faith restored - thanks! </p>
<p>Nice, you should enter it in the home automation contest!</p>
<p>can we use arduino uno for knock detecting door lock system?</p>
<p>Yes, any Arduino should be able to handle this. Are you asking if the Arduino can detect if the lock is engaged/on?</p>
Your schematics is the clearst i have ever seen...u rock..ur r an awesome inventor...love u
<p>Hi, thank you for this instructables !</p><p>I have change the motor with an Servo, and an Arduino Nano in a little box.</p><p>I will use it for a Geocache ;)</p>
<p>Hi, Thank you for great tutorial and walkthrough guide. I am doing the project step by step and checking each step as suggested. Things go normal when running my Arduino Uno via USB Cable or 9V Battery through barrel Jack. My final application is supposed to be working on wall adapter so I am checking each step with a 9V wall adapter too. At this point things go weird. Even on the early stage, checking piezo and led functioning, all things go crazy. Red and Green LEDS are on/off randomly when on wall plug. Attached is the picture of adapter. Am I missing something here? This occurs only when Arduino powered by 9V wall adapter.</p><p>Thanks in advance.</p>
<p>Edit to my post: I figured out that switching to wall adapter results in extra sensitive piezo! Piezo sensing everything (or may be there iselc noise) I tuned the treshold values in the original code and everything works perfect. If you have any other suggestions ( changing the resistor of pieze etc) please let us know. Thank you.</p>
<p>when i try to upload the code this message is shown ''Arduino: 1.6.3 (Windows 7), Board: &quot;Arduino Uno&quot;</p><p>Sketch uses 4,404 bytes (13%) of program storage space. Maximum is 32,256 bytes.</p><p>Global variables use 362 bytes (17%) of dynamic memory, leaving 1,686 bytes for local variables. Maximum is 2,048 bytes.</p><p>avrdude: ser_open(): can't open device &quot;\\.\COM4&quot;: The semaphore timeout period has expired.</p><p>Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.</p><p> This report would have more information with</p><p> &quot;Show verbose output during compilation&quot;</p><p> enabled in File &gt; Preferences. '' what should i do</p>
<p>As it says, there is a problem uploading to the board. (That is, your computer cannot talk to the Arduino.) You should try the suggestions at the link provided, <a href="http://www.arduino.cc/en/Guide/Troubleshooting#upload" rel="nofollow">http://www.arduino.cc/en/Guide/Troubleshooting#upl...</a> </p>
<p>what a cool idea</p>
<p>I need so much help! My science project is due in a week and I don't even understand how to test it on a circuit board or complete the step, &quot;</p><p>Solder a pair of 12&quot; (30cm) leads to the Piezo speaker. Connect it between Analog pin 0 and the ground. Also attach the 1M ohm resistor between Analog pin 0 and the ground.&quot; Please yep me. Email me at <a href="mailto:sharlaebrown@gmail.com" rel="nofollow">sharlaebrown@gmail.com</a> and change the subject to secret knock lock or something. Please help!</p>

About This Instructable


1,496 favorites


Bio: 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 ... More »
More by Grathio: Book Light Reading Light Video: Making your glove work with a touch screen 60 Second Mobile
Add instructable to: