banner-3

I wanted to introduce servos into my programming and thought an automated door lock would be a simple and effective use of this cool piece of hardware. It looks cool anyway!

I thought I could apply this to a box to help motivate students to discover its contents using the answers to maths-style questions – an answer represents a digit(s) of the key code.

The materials I used were:

  • An Arduino Nano
  • A 4×4 key switch Membrane Matrix KeyPad
  • Jumper wires
  • A 180 degree servo
  • A gear and rack – I dismantled an old DVD drive for this.
  • MDF
  • A drawer slider
  • Metal brackets.
  • 9v battery holder
  • an LED – optional

Step 1

STEP1-fixings

I took the drawer slider and fixed it to a piece of MDF. To strengthen it I attached some metal brackets in a way so the drawer slider would still move freely beneath it, yet would not come away from the rails during a pull of force – this is the door part and will be pulled open after all.

Step 2

STEP2-track

Next I dismantled an old DVD drive and used the gear and track (rack), sticking the gear to the servo and the rack to the drawer slider, then aligned it using some wooden brackets.

Step 3

STEP3-assembly

Then I began adding all the component pieces – the Nano, using standoffs as it won’t fix flush, the keypad, and the 9v battery holder, then I wired it all together.

Step 4

The code…


#include <Password.h> //http://playground.arduino.cc/uploads/Code/Password.zip //tells to use password library
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip  //tells to use keypad library
#include <Servo.h> //tells to use servo library


Servo myservo; //declares servo
Password passwordopen = Password( "0000" ); //password to unlock, can be changed
Password passwordclose = Password( "C" ); //password to unlock, can be changed

const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 6, 7, 8 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 9, 10, 11, 12 };


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int pos = 0;    // variable to store the servo position
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);  // light
  delay(200);
  myservo.attach(3); //servo on digital pin 9 //servo
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop() {
  keypad.getKey();
  myservo.write(pos);  // set servo to mid-point
}
//take care of some special events
void keypadEvent(KeypadEvent eKey) {
  switch (keypad.getState()) {
    case PRESSED:

      Serial.print("Enter:");
      Serial.println(eKey);
      delay(10);

      switch (eKey) {
        case '*': checkPassword(); delay(1); digitalWrite(13, HIGH);
          delay(100); digitalWrite(13, LOW); break;

        case '#': passwordopen.reset(); delay(1); passwordclose.reset(); delay(1); digitalWrite(13, HIGH);
          delay(100); digitalWrite(13, LOW); break;

        default: passwordopen.append(eKey); delay(1); passwordclose.append(eKey); delay(1); digitalWrite(13, HIGH);
          delay(100); digitalWrite(13, LOW);
      }
  }
}
void checkPassword() {

  if (passwordopen.evaluate()) { //if password is right open
    Serial.println("Accepted");
    myservo.write(160); //PUT YOUR SWEEP CODE HERE
    delay(2000);
    pos = 160;
  }

  if (passwordclose.evaluate()) { //if password is right open
    Serial.println("Accepted");
    myservo.write(0); //PUT YOUR SWEEP CODE HERE
    delay(2000);
    pos = 0;
  }

  else {
    myservo.write(0); //PUT YOUR SWEEP CODE HERE
  }
}

Step 5

STEP5-the-box

I then used MDF to create a box around the door. The door fits flush on one side so a ‘catch’ can be created on one edge to receive the lock (drawer slider). Additionally I fixed corner brackets for extra strength and also that rugged look.

Finished Article

September 2nd, 2016

Posted In: Arduino, Classroom, Computer Science, computing

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>