Blog How to Lock a Servo/Motor/Relay with Arduino

How to Lock a Servo/Motor/Relay with Arduino

In our everyday life, we may enter and exit places through this and that door. Some with lock, some without. And some, with a password lock. Enter the right word, you're permitted to go through. But, like in the movie, if you enter a wrong number, arrows will shoot out, alarm will sound aloud, and yes, you'll get caught. Just kidding! We all know in general situations, it will just happen that you won't be able to enter through the door. So here I will make a simple experiment about how to start the servo by this lock of Arduino. The servo can also be replaced with a motor relay or stepper motor.

You can check the video here:

Step 1: Prepare the Materials:

SunFounder Mars/Arduino Uno board

4x4 keypad 


Breadboard (with wire) 

2 x LED (one red, one green)

2 x Resistor (220Ohm, to protect the LED)

Several jumper wires and Dupont wires

Type-C USB cable


Step 2: Make Clear the Key Values

Check the internal key connection which will be needed later on in the coding.

PS: Detailed information about using this keypad can be found on:

Step 3: Wire up the Devices


Wire the keypad to the Mars board: pin X1-X4 and Y1-Y4 to pin 6-9 and 2-5 respectively. 

  1. Then the servo to the board: black wire to GND, red to 5V, and brown to pin 10.
  2. Next the LEDs: for each LED, first connect a 220 Ohm resistor to anode, then the cathode to GND. Green one's cathode to pin 11of the Mars, and red LED to pin 12.

So all the wiring is done!

Step 4: Program the Mars

Write the program, plug in the USB cable, burn the code to the Mars board. After upload is done, open the Serial Monitor.

Step 5: Try the Password!


Then on the keypad, press * to start input, the correct password as you set in the code, and at last #. All the entered value will be shown in the serial monitor window.

You can see the green LED light up and the servo start to spin for a while.

If a wrong psw is entered, BOOM! Er, just kidding…unlike in the movie which may be alarm sounding loud security rushing toward you, here, just the red LED lights up. 

Also published on Instructables: