DEV Community

Kyle Trahan
Kyle Trahan

Posted on

Knock Lock

Finally hopping back into the Arduino chair after not doing very much with it lately. I found a setup to create a knock lock, which essentially has a button switch that will turn a lock to the closed position. Then using a piezo the lock will open back up according to the specified knock requirements, in this example it's just three knocks that register within the given range of values on the piezo.

This was one of the more complicated circuit systems I have put together so far, but it just required a little bit more focus. I had a really good time doing it and played with a few different knocking combinations.

The different components required for this are a servo motor, 3 LEDs (preferably different colors), 1 piezo, 1 button switch, 1 capacitor, and all the wiring/resistors required for each to connect to ground, power, and a pin in the breadboard.

Before the setup function you will want to declare all of the different variables you are using. It's a lot easier to read through your code when pin7, which is hooked up to the yellow LED, is called something like yellowLED. Here's a list of all the variables I made in order to do this project:

#include <Servo.h>
Servo myServo;

const int piezo = A0;
const int switchPin = 2;
const int yellowLED = 7;
const int greenLED = 9;
const int redLED = 13;

int knockVal;
int switchVal;

const int minKnock = 10;
const int maxKnock = 100;

boolean locked = false;
int numberOfKnocks = 0;
Enter fullscreen mode Exit fullscreen mode

Next up you are going to want to set what all of your pins are actually doing, ie are they an output pin or an input pin. Along with declaring what pin your servo motor is connected to. Heres that code as well:

void setup() {
  myServo.attach(11);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(switchPin, INPUT);
  Serial.begin(9600);
  digitalWrite(greenLED, HIGH);
  myServo.write(0);
}
Enter fullscreen mode Exit fullscreen mode

Finally we are going to want to describe what happens in our loop function. This will be where all the logic is actually happening in order to control our lock. We will set up an if statement in order to change to the locked position like so:

if ( locked == false) {
    switchVal = digitalRead(switchPin);
    if (switchVal == HIGH ) {
      locked = true;
      digitalWrite(greenLED, LOW);
      digitalWrite(redLED, HIGH);
      myServo.write(90);
      delay(1000);
    }
  }
Enter fullscreen mode Exit fullscreen mode

We just check to see that the lock is open, if it is then we check to see if the value of our button switch is HIGH, aka on. After that we make some adjustments to the visual part of the board to let you know that the lock did in fact lock. Turn the green light off and the red light on. Also rotating the servo in order to move the lock into place.

Next up we set up the functionality to read the incoming knocks from the piezo like so:

if ( locked == true ) {
    knockVal = analogRead(piezo);
    if ( numberOfKnocks < 3 && knockVal > 0 ) {
      if ( checkForKnock(knockVal) == true ) {
        numberOfKnocks++;
      }
    }
    if ( numberOfKnocks >= 3 ) {
      locked = false;
      myServo.write(0);
      delay(20);
      digitalWrite(greenLED, HIGH);
      digitalWrite(redLED, LOW);
      numberOfKnocks = 0;
    }
  }
Enter fullscreen mode Exit fullscreen mode

Now we check that our locked state is true. If so we will set the knockVal to the value coming in on the piezo. If our current knock count is less than the required amount, 3, and the knockVal is greater than 0, or in other words the piezo registered a sound. Then we will run the function to check if the knock registered in between our min and max knock values. That function looks like this:

boolean checkForKnock(int value) {
  if ( value > minKnock && value < maxKnock ) {
    digitalWrite(yellowLED, HIGH);
    delay(50);
    digitalWrite(yellowLED, LOW);
    return true; 
  }
  else {
    return false;
  }
} 
Enter fullscreen mode Exit fullscreen mode

If the knock registers between our min and max values then we will increment our knock count. The yellow LED is there to let you know if a knock registers or not by lighting up upon successful knocks.

Once the knock count reaches the required amount then the lock turns back to false and we turn our servo motor back to its unlocked position.

This was a bit of a longer winded post then most of my Arduino post, but I feel like this could be implemented in some cool ways. Maybe you want to lock your bedside table or with the right materials your front door! You might want a more secure knock code for your front door though! Hope this was an enjoyable read. Thanks for tuning in and have a great day!

Top comments (0)