Secret Knock Lock
Secret Knock Lock
A project from
Arduino Project Handbook:
25 Practical Projects to
Get You Started
Project 9: Secret
KnocK LocK
For centu ri eS cLan deStin e
grou PS have uSed Secret
KnocKS to Prevent unautho-
rized entry. Let’S bring
thiS SyStem into modern
timeS, by creating our own
eLectron ic gateKeePer.
PA R T S R E Q U I R E D LIBRARIES
Arduino board
REQUIRED
Breadboard Servo
Jumper wires
Tower Pro SG90 9g
servomotor
Piezo buzzer
3 LEDs
1M-ohm resistor
3 220-ohm resistors
H OW I T W O R K S
In this project, you’ll make a circuit that moves a servo arm to unlock
a box or door when you provide the correct secret knock. So far
we’ve been using a piezo buzzer only to make noise, but we can also
use it as a sensor to detect sounds—in this case, knocks. When a
piezo is struck it rings like a bell, but instead of producing sound it
outputs voltage, which generates a number depending on the force
of the strike. We’ll measure this voltage in numbers, and if the knocks
fall within a certain range, the Arduino will register them as correct. If
three knocks of the correct voltage are detected, you’ve cracked the
code, and the servo arm moves to unlock the box or door.
Here are the two lines of code we’ll use later in the sketch to set
the range for the voltage; if the voltage is between 10 and 100, the
knock will be registered.
If you knock too softly or too hard, the knock won’t register.
You’ll need to do three “correct” knocks to trigger the servo arm to
move. When the correct sequence and strength of knock are regis-
tered, the servo arm swings 90 degrees to “unlock” whatever it is set
up with. The LEDs, shown in Figure 9-1, serve as indicators of your
lock’s status: the red LED lights when the knocks are incorrect and the
servo arm has not moved (that is, the box or door is still locked); the
yellow LED flashes when a knock is registered and a correct code is
sensed; and the green LED lights and the servomotor moves after
three correct knocks.
FIGURE 9-1:
The LED setup
80 P roj ec t 9
For the best result, remove your piezo from its casing and attach
it directly to the inside of a box or outside of a door so it is more sen-
sitive to the vibration of the knock.
THE BUILD
1. Insert a 1M-ohm resistor into your breadboard and connect
the piezo’s red wire to one leg and its black wire to the other.
Connect the black wire to the GND rail, and the red wire to
Arduino pin A0.
P I E ZO ARDUINO
S E R VO ARDUINO
3. Insert the LEDs into your breadboard with the short, negative
legs connected to GND. The positive legs should connect to
the pins via 220-ohm resistors as follows: yellow connects to
Arduino pin 3, green to pin 4, and red to pin 5.
LE D S ARDUINO
81 P roj ec t 9
FIGURE 9-2: THE P I EZO DE TECTS THE KNOC KS A ND OUT PUT S V OLTA GE T O T HE
The circuit diagram for A RDUI NO DEP ENDI NG ON T HE ST R ENGT H OF T HE KNOC K. KNOC K
the secret knock lock THRE E TI ME S W I TH THE R I GHT L EV EL OF F OR C E T O C R A C K T HE C OD E.
T H E S K E TC H
We first call on the Servo library and set Arduino pin 9 to control
the servo. LEDs are attached to Arduino pins 3, 4, and 5, and
these will light depending on the validity of a knock. The piezo acts
as a sensor rather than a buzzer in this project and is attached to
Arduino pin A0. When someone knocks, the knock is sensed by the
piezo and a voltage value is sent to the A0 analog pin of the Arduino
depending on the strength of the knock—the harder the knock, the
higher the value. A knock with a value below 10 is considered too
quiet, and one with a value above 100 too loud, so neither will be
82 P roj ec t 9
accepted as a valid knock. The red LED lights if the knock is not
accepted, and the yellow LED lights if it is. Any knock value between
10 and 100 is accepted as a valid knock and counted, and if three
valid knocks are received, the servomotor moves and the green LED
lights.
As mentioned earlier, these are the two lines of code that set the
parameters for measuring the voltage:
If you were feeling particularly secretive, you could set this range
even tighter to make the code harder to crack. Here’s the sketch:
#include <Servo.h>
Servo servo9; // Pin connected to servo mpo
const int quietKnock = 10; // Set min value that will be accepted
const int loudKnock = 100; // Set max value that will be accepted
boolean locked = false; // A true or false variable
int numberOfKnocks = 0; // Value for number of knocks
void setup() {
servo9.attach(9);
pinMode(yellowLed, OUTPUT); // Set LED pins as outputs
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(switchPin, INPUT); // Set servo pin as input
Serial.begin(9600);
digitalWrite(greenLed, HIGH); // Green LED is lit when the
// sequence is correct
servo9.write(0);
Serial.println("The box is unlocked!");
}
83 P roj ec t 9
void loop() {
if (locked == false) {
switchVal = digitalRead(switchPin);
if (switchVal == HIGH) {
locked = true;
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
servo9.write(90);
Serial.println("The box is locked!");
delay(1000);
}
}
if (locked == true) {
knockVal = analogRead(piezo); // Knock value is read by analog pin
if (numberOfKnocks < 3 && knockVal > 0) {
if (checkForKnock(knockVal) == true) { // Check for correct
// number of knocks
numberOfKnocks++;
}
Serial.print(3 - numberOfKnocks);
Serial.println(" more knocks to go");
}
if (numberOfKnocks >= 3) { // If 3 valid knocks are detected,
// the servo moves
locked = false;
servo9.write(0);
delay(20);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
Serial.println("The box is unlocked!");
}
}
}
84 P roj ec t 9