Introduction: 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

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