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

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

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

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 <Servo.h> //tells to use servo library

Servo myservo; //declares servo

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 };

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
}

void loop() {
myservo.write(pos);  // set servo to mid-point
}
//take care of some special events
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;

delay(100); digitalWrite(13, LOW); break;

delay(100); digitalWrite(13, LOW);
}
}
}

Serial.println("Accepted");
myservo.write(160); //PUT YOUR SWEEP CODE HERE
delay(2000);
pos = 160;
}

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

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