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.

Remove these adsRemove these ads by Signing Up

when i try to upload the code this message is shown ''Arduino: 1.6.3 (Windows 7), Board: "Arduino Uno"

Sketch uses 4,404 bytes (13%) of program storage space. Maximum is 32,256 bytes.

Global variables use 362 bytes (17%) of dynamic memory, leaving 1,686 bytes for local variables. Maximum is 2,048 bytes.

avrdude: ser_open(): can't open device "\\.\COM4": The semaphore timeout period has expired.

Problem uploading to board. See for suggestions.

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences. '' what should i do

Grathio (author)  ahmedmagdy88996 days ago

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,

jkctech1 year ago
How to program the knock?
masonson2 years ago
cool next project
eng.7oka4 years ago
hey.. sorry to bother you again.. I just want to know what the function (map(knockReadings[i],0, maxKnockInterval, 0, 100); does..
Grathio (author)  eng.7oka4 years ago
Arduino reference for the map() function.

This normalizes the data so the longest time between knocks gets "100" and the rest are adjusted proportionately.  We do this so it doesn't matter if you knock fast or slow as long as you get the rhythm correct.
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.
Grathio (author)  eng.7oka4 years ago
The best description of what it does is in the link I provided to the description of the function in the Arduino Reference manual.
this is the function

long map(long x, long in_min, long in_max, long out_min, long out_max)
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
bmondor2 years ago
where do the positive ends of the LEDs go. also i only have 2 wires that go into the digital pin in the arduino. i cant figure out how to get it to work even though i have all the wires in which seem to be the right places.
Jaslene1003 years ago
how did you figure out the array pattern for the "shave and hair cut" rhythm?
ayanez3 years ago
i cannot download the code :/
Where do i find the code?
Grathio (author)  Jordan arduino3 years ago
Read #1 above.

If you're still having trouble after following those instructions post again.
noobduino4 years ago
would any of these moters work i am Canadian (no radioshack for me only the source)
Grathio (author)  noobduino4 years ago
Nope, those are all high speed, low torque motors. This project needs low speed, high torque motors.

The motor I link in Step 1 is sold by a Canadian company so shipping costs, etc should be minimal.
jigsawnl4 years ago
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.
Pingman7474 years ago
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.
Grathio (author)  Pingman7474 years ago
Yes, you are correct.  Good catch!

Fortunately that line doesn't actually -do- anything so it's not super critical.

It looks like I haven't updated the PDE here in several versions. You can get the latest here:

I'll also try to update the coke embedded above, but the Instructables interface doesn't make it easy.
pls help.. can I use an atmel 8051 micro?? how would the programming be??
eng.7oka4 years ago
Ok..I Think I will use C ... I'm looking for C compiler for my PIC now... Thanks again
eng.7oka4 years ago
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
BSprague5 years ago
 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. ;)
Grathio (author)  BSprague5 years ago
 That's a great idea!  I'd love to see a photo or video of it  in action.
 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.
Spydamonky5 years ago
 I cant down load the code. it downloads as a tmp. could you post it please