Project 12: Knock Lock

In this project we’ll build our own lock system that only can be unlocked by knocking 3 times.

For this project we need 3 different colour Led, some resistors, a switch, a capacitor and a servomotor.

By pressing the button the system will get locked, which will be indicated by the red LED. The knocks will be captured by the buzzer (remember that the buzzer can act like a speaker and a vibration sensor), and each time it detects a knock, the yellow LED will blink once. When we had knocked 3 times, the servomotor will turn 90º and the green led will be on. Indicating that the system is unlocked.

We will know the current state of the system because it will be displayed (if it’s un/locked, knocks left…).

knock-lock-disec3b1o-de-protoboard

We need to connect our buzzer to an analogic pin, due it generates a wide data range, not only 1 or 0.

The 3 LED’s will be connected to digital outputs, and the signal pin of our servomotor will be connected to a digital pin too. But remember to connect it to pin that can act like an analogic output trough PWM (as seen on previous projects).

The capacitor’s function is to stabilize the electric signal.

The main objective of the program is to turn 90º the servomotor and turn the red light on when the button is pressed. Then, it will start the knock countdown. Each time the buzzer detects a knock the yellow light will blink. When the system has detected 3 knocks will return the servo to its natural position and turn the green light on.

// import the library
 #include <Servo.h>
 // create an instance of the servo library
 Servo myServo;
 const int piezo = A0;
 const int switchPin = 2;
 const int yellowLed = 3;
 const int greenLed = 4;
 const int redLed = 5;             
//defines LED's and piezo's pins.
// variable for the piezo value
 int knockVal;
 // variable for the switch value
 int switchVal;
// variables for the high and low limits of the knock value
 const int quietKnock = 10;
 const int loudKnock = 100;
// variable to indicate if locked or not
 boolean locked = false;
 // how many valid knocks you've received
 int numberOfKnocks = 0;
void setup(){
 // attach the servo to pin 9
 myServo.attach(9);
// make the LED pins outputs
 pinMode(yellowLed, OUTPUT);
 pinMode(redLed, OUTPUT);
 pinMode(greenLed, OUTPUT);
// set the switch pin as an input
 pinMode(switchPin, INPUT);
// start serial communication for debugging
 Serial.begin(9600);
// turn the green LED on
 digitalWrite(greenLed, HIGH);
// move the servo to the unlocked position
 myServo.write(0);
// print status to the serial monitor
 Serial.println("the box is unlocked!");
 }
void loop(){
// if the box is unlocked
 if(locked == false){
// read the value of the switch pin
 switchVal = digitalRead(switchPin);
// if the button is pressed, lock the box
 if(switchVal == HIGH){
 // set the locked variable to "true"
 locked = true;
// change the status LEDs
 digitalWrite(greenLed,LOW);
 digitalWrite(redLed,HIGH);
// move the servo to the locked position
 myServo.write(90);
// print out status
 Serial.println("the box is locked!");
// wait for the servo to move into position
 delay (1000);
 }
 }
// if the box is locked
 if(locked == true){
// check the value of the piezo
 knockVal = analogRead(piezo);
// if there are not enough valid knocks
 if(numberOfKnocks < 3 && knockVal > 0){
// check to see if the knock is in range
 if(checkForKnock(knockVal) == true){
// increment the number of valid knocks
 numberOfKnocks++;
 }
// print status of knocks
 Serial.print(3 - numberOfKnocks);
 Serial.println(" more knocks to go");
 }
// if there are three knocks
 if(numberOfKnocks >= 3){
 // unlock the box
 locked = false;
// move the servo to the unlocked position
 myServo.write(0);
// wait for it to move
 delay(20);
// change status LEDs
 digitalWrite(greenLed,HIGH);
 digitalWrite(redLed,LOW);
 Serial.println("the box is unlocked!");
 }
 }
 }
// this function checks to see if a 
 // detected knock is within max and min range
 boolean checkForKnock(int value){
 // if the value of the knock is greater than
  // the minimum, and larger than the maximum
 if(value > quietKnock && value < loudKnock){
 // turn the status LED on
 digitalWrite(yellowLed, HIGH);
 delay(50);
 digitalWrite(yellowLed, LOW);
 // print out the status
 Serial.print("Valid knock of value ");
 Serial.println(value);
return true;
 }
 // if the knock is not within range
 else {
 // print status
 Serial.print("Bad knock value ");
 Serial.println(value);
return false;
 }
 }

Once coded and assembled everything is time to check our knock lock system out.

Leave a comment