0% found this document useful (0 votes)
116 views8 pages

Secret Knock Lock

This project creates an electronic lock that can be unlocked with a secret knock code detected by a piezo sensor. When three knocks in the correct voltage range are detected in sequence, a servo motor will move to unlock the door or box. LED lights indicate the locked/unlocked status and whether knocks are correctly detected. The piezo sensor measures the voltage of knocks and sends the value to an Arduino, which checks if the value is between 10-100 to register a valid knock. Three valid knocks in a row will trigger the servo to unlock the device.

Uploaded by

Akhilesh Pande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views8 pages

Secret Knock Lock

This project creates an electronic lock that can be unlocked with a secret knock code detected by a piezo sensor. When three knocks in the correct voltage range are detected in sequence, a servo motor will move to unlock the door or box. LED lights indicate the locked/unlocked status and whether knocks are correctly detected. The piezo sensor measures the voltage of knocks and sends the value to an Arduino, which checks if the value is between 10-100 to register a valid knock. Three valid knocks in a row will trigger the servo to unlock the device.

Uploaded by

Akhilesh Pande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MAKE & COLLABORATE:

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.

const int quietKnock = 10;


const int loudKnock = 100;

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

Red wire A0 via 1M-ohm resistor


Black wire GND via 1M-ohm resistor

2. Connect the servo’s yellow signal wire directly to Arduino pin 9,


its brown wire to GND, and its red wire to +5V.

S E R VO ARDUINO

Yellow wire Pin 9


Red wire +5V
Brown wire GND

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

Pins 3–5 via


Positive legs
220-ohm resistors
Negative legs GND

4. Connect Arduino pin 2 to the positive power rail. In our setup


this is always on, but you could add a switch in the connection
between Arduino pin 2 and the power rail to save power when
the project is not in use.
5. Connect the breadboard rails to Arduino GND and +5V.
6. Make sure your setup matches the circuit diagram in Figure 9-2,
and then upload the code in “The Sketch” on page 82.

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.

THE LEDS LI GHT DEP EN D I NG ON T HE STAT US OF T HE L OC K:

A LI T RED LED ME A NS THE SER V O HA S NOT M OV ED , SO T HE

LOCK I S CLOSED; A LI T GR EEN L ED M EA NS I T ’S OPEN; A ND A

FLA SHI NG YE LLOW LE D M EA NS A KNOC K I S B EI NG D ET EC T ED .

THE SER VO’S YELLOW WI R E C ONNEC T S T O A R D UI NO PI N 9 .

WHEN THE CORRE CT SE QUENC E OF KNOC KS I S R EGI ST ER ED ,

THE SER VO A RM WI LL SW I NG 9 0 D EGR EES A ND OPEN T HE L OC K.

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:

const int quietKnock = 10;


const int loudKnock = 100;

If you were feeling particularly secretive, you could set this range
even tighter to make the code harder to crack. Here’s the sketch:

/* Created 18 September 2012 by Scott Fitzgerald


Thanks to Federico Vanzati for improvements
http://arduino.cc/starterKit
This example code is part of the public domain.
*/

#include <Servo.h>
Servo servo9; // Pin connected to servo mpo

const int piezo = A0; // Pin connected to piezo


const int switchPin = 2; // Pin connected to servo
const int yellowLed = 3; // Pin connected to yellow LED
const int greenLed = 4; // Pin connected to green LED
const int redLed = 5; // Pin connected to red LED

int knockVal; // Value for the knock strength


int switchVal;

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!");
}
}
}

boolean checkForKnock(int value) { // Checks knock value


if (value > quietKnock && value < loudKnock) { // Value needs to be
// between these
digitalWrite(yellowLed, HIGH);
delay(50);
digitalWrite(yellowLed, LOW);
Serial.print("Valid knock of value ");
Serial.println(value);
return true;
}
else { // If value is false then send this to the IDE serial
Serial.print("Bad knock value ");
Serial.println(value);
return false;
}
}

84 P roj ec t 9

You might also like