Arduino - Keypad - Servo Motor

In this tutorial, we are going to learn how to use a keypad, servo motor, and Arduino together:

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Keypad
1×Servo Motor
1×5V Power Adapter
1×DC Power Jack
n×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Keypad and Servo Motor

If you do not know about keypad and servo motor (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

Arduino keypad servo motor wiring diagram

Image is developed using Fritzing. Click to enlarge image

Arduino Code - rotates Servo Motor if the password is correct

The below code turns the servo motor to 90° for 5 seconds if the password is correct. After 5 seconds, it turns the servo motor to 0°.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-keypad-servo-motor */ #include <Keypad.h> #include <Servo.h> #define ROW_NUM 4 // four rows #define COLUMN_NUM 4 // four columns #define SERVO_PIN A0 // // the Arduino pin, which connects to the servo motor Servo servo; // servo motor char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password_1 = "ABC1234"; // change your password here const String password_2 = "5642B"; // change your password here const String password_3 = "9765"; // change your password here String input_password; int angle = 0; // the current angle of servo motor unsigned long lastTime; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum password size is 32, change if needed servo.attach(SERVO_PIN); servo.write(0); // rotate servo motor to 0° lastTime = millis(); } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); if (key == '*') { input_password = ""; // reset the input password } else if (key == '#') { if (input_password == password_1 || input_password == password_2 || input_password == password_3) { Serial.println("The password is correct, rotating Servo Motor to 90°"); angle = 90; servo.write(angle); lastTime = millis(); } else { Serial.println("The password is incorrect, try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } if (angle == 90 && (millis() - lastTime) > 5000) { // 5 seconds angle = 0; servo.write(angle); Serial.println("Rotating Servo Motor to 0°"); } }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • On Arduino IDE, Go to Tools Manage Libraries
  • Arduino add library
  • Search “keypad”, then find the keypad library by Mark Stanley, Alexander Brevig
  • Click Install button to install keypad library.
  • Arduino keypad library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Arduino IDE Upload Code
  • Press 12345#
  • Press 5642B#
  • See the result on Serial Monitor and the state of servo motor
  • COM6
    Send
    The password is incorrect, try again The password is correct, rotating Servo Motor to 90° Rotating Servo Motor to 0°
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Code Explanation

Authorized passwords are pre-defined in the Arduino code.

A string is used to store the password inputted by users, called input_password. In keypad, two keys (* and #) are used for special purposes: clear password and terminate password. When a key on keypad is pressed:

  • If the pressed key is not one of two special keys, it is appended to the input_password
  • If the pressed key is *, input_password is clear. You can use it to start or re-start inputing the password
  • If the pressed key is #:
    • The input_password is compared with the pre-defined passwords. If it matched with one of the pre-defined passwords, rotate the servo motor to 90°.
    • Regardless the password is correct or not, the input_password is clear for the next input

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES