Automatic Gate Keypad Lock




About: I'm a hobbyist and DIYer at home, and an IT Director at a non-profit for work. I love repairing / repurposing electronics and computers. My wife and I love riding horses and driving nasty on closed courses.

Goal: Figure out a way to lock our double-swing gate from both sides, securing the yard. The locking mechanism should stay unlocked, because we often leave the gate open when working all day in the yard.

Background: We have a double-swing gate, meaning that the gate will swing outward and inward. You can use this instructable to secure a gate that only swings inward (or outward) too - the project is actually simpler to perform.

End Result: The keypad on the outside of the gate functions to unlock and lock the gate. The gate stays unlocked until the lock button is pushed. There is a hidden button inside the gate that functions to lock/unlock the gate from the inside. The gate stays locked when the power is off, and the locking mechanism can be moved manually (from the inside) in case of emergency or power outage.


  • Arduino Uno board (or whatever Arduino board you happen to have)
    • Needs to have enough pins to accommodate 1 or 2 servos (digital I/O) and your keypad (7 digital I/O for 3x4 aka 12-button keypad)
  • Arduino shield - better than my solution of jamming the wires into the Arduino itself
  • Servos to operate your latche(s). I have a 6-foot-wide gate, so I used 2 latches with 2 servos. You might be able to get away with just 1 on a smaller gate/door.
    • Make sure you get servos that work with the Arduino libraries. I tried using some aviation servos from my neighbor, and they wouldn't work correctly.
    • These "waterproof" servos worked well for me and have Futaba-sized heads.
    • You would also need a pack of servo horns - I used some like these
    • I recommend a heavy-duty plastic servo horn, so that you can drill the holes out if you need a larger size.
  • Heavy gauge wire to attach the servo to the latch. I used the heaviest gauge wire I could purchase at my local hardware store.
  • Keypad - like this black one - or this membrane one
  • Plastic enclosure to mount your keypad in - I made mine out of scrap vinyl.
  • Plastic box to house your Arduino (or you could drill a hole in the side of your house and place it inside)
  • 5V power supply - maybe this usb one or a bit higher power one
    • I had a pretty puny usb power supply that wouldn't cut it. I had to use a 1.5A power supply, but you might be able to get away with a 1A
  • Wood or plastic to make your servo mounts (I used scrap Trex boards - basically plastic wood - but you could use anything)
  • Gate latch(es) - hardware store
  • Pushbutton switch - for opening from the inside - they also sell waterproof ones if you'll be leaving it exposed to the elements.
  • Rare-earth Magnets (if you're doing a double-swing gate - if it swings one way, you're probably better off with just a spring auto-closer)
  • Something to go between the rare earth magnets - I recommend a sheet of plexiglass or vinyl or similar. Do not use sheet metal - it will disrupt the magnetic alignment.
  • Mounting hardware for magnets - recommended to use wood and screws/bolts

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Create Servo Mount and Attach Servo to Latch

Attach the servo horn to the servo (don't attach the screw in the center), and spin it. Typical servos will spin about 180 degrees (half-circle). You'll want to re-position the servo horn so that when it is vertical, it is in the middle of that half circle. This will leave you plenty of range-of-motion when you calibrate the servo.

The white plastic piece attached to the green wire is an extension of the servo horn. I happened to have a large pile of vinyl fence scraps around, so I cut one into a rectangle to extend the servo horn and provide a convenient way to attach the wire which moves the latch back and forth. You could use anything lightweight, like a strip of aluminum, steel, or plexiglass.

The size of this extension isn't critical, and depending upon your placement of the servo, may need to be longer or shorter. I made mine longer because of the latch being mounted "upside down" ...

My friend mounted the latch so that the "locking points" are down, and I wanted to avoid putting more holes into my metal gate hardware by moving it. You would want to mount it so the locking points are up, so that the latch can't "fall" into them and get stuck.

You will need to attach an extension to the 3 wires that come from the servo. Typically, these are black, red, and white. Run the wires back to the box where you're going to house your Arduino board.

Black - attach to ground (negative power terminal)

Red - attach to positive (+5V)

White - attach to your Arduino board on one of the digital I/O ports

Step 2: Gate-alignment Device

If your gate only swings one way, you could just put a spring-hinge on it to ensure it closes automatically. If so, you can just skip this step.

If you want a double-swing gate, you will probably want some form of alignment device to keep it in place while locking and unlocking it. The servos are powerful, but they are no match against a heavy gate pushing the latches against their catch.

I used 2 rare earth magnets I found on ebay, with convenient holes in them for mounting. Now, what they don't tell you is that the magnets are polarized, and if you want to use them the way I did, you'll want to get a matching pair so that the countersunk holes are on the correct sides. Oops.

The magnets usually come epoxy coated, a coating that will easily crack and fall off. You don't want these magnets slapping against each other, because they will wear the coating down and fall apart. Thus, I manufactured a vinyl "buffer" between the magnets.

The "buffer" is also necessary because you need some sort of friction to prevent the gate from wobbling back and forth perpetually as it tries to center itself.

Please let me know if you come up with a better solution for this - I considered some sort of spring-loaded roller, but couldn't find anything for purchase.

Step 3: Keypad

You will want to follow the Good Instructions for attaching the keypad. This would be the Keypad Tutorial.

Once you figure out the pinout of the keyboard, you can attach it to the Arduino. It is helpful to have a meter to diagnose the keypad pinout, unless you know it already.

Step 4: Hidden Button

You may want a button on the inside of the gate to lock/unlock the gate. I coded the Arduino so that it would keep track of whether the gate was unlocked or locked, so the button can toggle between the two modes.

Follow the button tutorial, and use the schematic there to attach your button to Ground, +5V, a resistor, and the digital pin on the Arduino you wish to use (I used pin 12).

Step 5: Connect It All Together! Calibrate the Servos.

Assemble all the pieces together, and attach them to your gate.

It's time to calibrate the servos. I hope you have a laptop or a really long USB cord!

Arduino has a nifty bit of code that will read the servo position (an integer between 0 and 180) to make it easy for you to determine what position your servo needs to be in for the locked and unlocked position.

ServoRead code

You will want to do this individually for both servos, as they probably have different values. Write down the values for locked and unlocked position.

Step 6: Add Code to Your Device

The attached code relies on a few Arduino libraries:

  • Servo.h
  • Keypad.h

There are several steps to getting your equipment working:

  1. Specify unlocked / locked positions for the servos:
    const int Locked = 69;
    const int Unlocked = 92;
    Take the values that you collected in the prior "servo calibration" step and put them into the code.
  2. Specify the pins that the servos attach to
    myservo.attach(9); // attaches the servo on pin 9 to the servo object
  3. Specify the pins that the keypad rows and columns are connected to on the arduino
    byte rowPins[ROWS] = { 5, 4, 3, 2 };
    byte colPins[COLS] = { 8, 7, 6 };
  4. Specify the pin that your button is attached to (for unlocking/locking the gate from the inside)
    const int ButtonPin = 12;
  5. Specify the unlock codes:
    char unlockcode[5] = "1234";
    char Guestunlockcode[5] = "1111";
  6. That should be it! You may want to uncomment the serial commands so that you can test your keypad. Just be aware that you need to uncomment them once the device is put into "production" ... otherwise it will hang waiting to hear the status from the serial port.
Move It

Participated in the
Move It

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    8 Discussions

    Sai SamarthT

    3 years ago

    If anybody need the code for only 1 servo.. plz refer below. I just made the changes.

    #include <Keypad.h>

    #include <Servo.h>

    Servo myservo;

    const int ButtonPin = 12; // the number of the pushbutton pin

    const int Servo1Pin = 9; // the number of the first servo pin

    const int Locked = 69;

    const int Unlocked = 92;

    const byte ROWS = 4; // Four rows

    const byte COLS = 3; // Three columns

    char keys[ROWS][COLS] = {






    char lastfour[5] = " "; // initialize our character string (array) that holds the last 4 keys pressed

    // List of unlock codes

    char unlockcode[5] = "1234";

    char Guestunlockcode[5] = "1111";

    // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.

    byte rowPins[ROWS] = { 5, 4, 3, 2 };

    // Connect keypad COL0, COL1 and COL2 to these Arduino pins.

    byte colPins[COLS] = { 8, 7, 6 };

    // Create the Keypad

    Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

    //Variables to hold the button state:

    int ButtonState = 0; // variable for reading the pushbutton status

    int LockState = 0; // variable for saving the lock state. Locked = 1, Unlocked = 0

    void setup()


    // pinMode(ledpin,OUTPUT);

    // digitalWrite(ledpin, HIGH);

    // Serial.begin(9600);

    // attach the servos to their pins


    // lock the servos by default


    // Serial.println("We begin!");

    // Initialize the unlock / lock button (goes inside the gate)

    pinMode(ButtonPin, INPUT);

    // Initialize the lock state - locked when started

    LockState = 1;


    void loop()


    char key = kpd.getKey();

    if(key) // Check for a valid key.


    // Move the values up the string to make room for our keypress

    lastfour[0] = lastfour[1];

    lastfour[1] = lastfour[2];

    lastfour[2] = lastfour[3];

    lastfour[3] = key;

    // Serial.println(key);

    // Serial.println(lastfour);

    switch (key)


    case '*':

    // Serial.println("Locked");



    LockState = 1;


    case '#':

    // Serial.println("Locked");



    LockState = 1;



    // check to see if our keypress resulted in a valid code

    if (checker(lastfour,unlockcode) ==1 or checker(lastfour,Guestunlockcode)==1)




    // Serial.println("Open Sesame!");

    lastfour[3] = ' ';

    LockState = 0;



    // Pushbutton Controls

    ButtonState = digitalRead(ButtonPin);

    // Check to see if the button was pressed.

    if (ButtonState == HIGH) {

    if (LockState == 0) {

    // Lock was previously unlocked. Lock it.



    LockState = 1;


    else {

    // Lock was previously locked. Unlock it.



    LockState = 0;


    delay(2000); // Don't accept any other input for another 2 seconds.



    // procedure used to check the code entered vs our unlock codes

    int checker(char input[],char check[])


    int i,result=1;

    for(i=0;input[i]!='\0' && check[i]!='\0';i++){






    return result;


    1 reply

    4 years ago

    Delokaver, if your gate only needs to swing one direction, the 12v solenoid locking mechanism from Adafruit is nice ... especially if your hinges have a spring closer so it re-locks automatically. You wouldn't need a magnet to hold it in place that way.


    4 years ago on Introduction

    nice one, I am actually build the similar locking system for my front gate but it come with double LCD (front and inside), but for now isn't installed yet cause I am still looking for the suitable SLIDING LOCK MECHANISM, any idea ?


    Reply 4 years ago on Introduction

    I do like the RFID option, and I saw the Arduino RFID shield. For my personal needs, I didn't want to have to carry around an RFID button or tag whenever I want to go into my backyard, so I went with the keypad interface.

    I think that if I was going to do RFID, I would want to re-design to use some sort of door lock / deadbolt hardware. Most people who want to use RFID are trying to secure something with more security than my hacked-up servo creation :-)

    There is a nice door-style solenoid available on Adafruit:

    I just wish we had some hardware that fits well into a standard door lock hole.