Arduino Nano - Keypad - Servo Motor

This tutorial instructs you how to use Arduino Nano and keypad to control servo motor. In detail:

The Arduino Nano code can accepts multiple passwords.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Keypad
1×Servo Motor
1×5V Power Adapter
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Keypad and Servo Motor

If you are not familiar with keypad and servo motor (including pinout, functioning and programming), the following tutorials can help you:

Wiring Diagram

The wiring diagram between Arduino Nano and keypad servo motor

This image is created using Fritzing. Click to enlarge image

It should be noted that the wiring diagram shown above is only suitable for a servo motor with low torque. In case the motor vibrates instead of rotating, an external power source must be utilized to operate the servo motor. The following wiring diagram demonstrates how to connect the servo motor to an external power source.

The wiring diagram between Arduino Nano and keypad servo motor

This image is created using Fritzing. Click to enlarge image

Please do not forget to connect GND of the external power to GND of Arduino.

Arduino Nano Code - rotates Servo Motor if the password is correct

If the password is correct, the servo motor will be set to 90° for 5 seconds. After this period of time, it will be turned to 0°.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-keypad-servo-motor */ #include <Keypad.h> #include <Servo.h> #define SERVO_PIN 10 // // The Arduino Nano pin connected to the servo motor Servo servo; // servo motor const int ROW_NUM = 4; // four rows const int COLUMN_NUM = 4; // four columns const byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // The Arduino Nano pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // The Arduino Nano pin connected to the column pins of the keypad char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; Keypad keypad = Keypad(makeKeymap(key_layout), pin_rows, pin_column, ROW_NUM, COLUMN_NUM); const String password_1 = "4321"; // change your password here const String password_2 = "09876"; // 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 prev_time; 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° prev_time = 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 correct password! Rotating Servo Motor to 90°"); angle = 90; servo.write(angle); prev_time = millis(); } else { Serial.println("The incorrect password! try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } if (angle == 90 && (millis() - prev_time) > 5000) { // 5 seconds angle = 0; servo.write(angle); Serial.println("Rotating Servo Motor to 0°"); } }

Detailed Instructions

  • Connect a USB cable between the Arduino Nano and the PC.
  • Open the Arduino IDE, select the correct board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “keypad” and locate the keypad library created by Mark Stanley and Alexander Brevig.
  • Then, press the Install button to complete the installation of the keypad library.
Arduino Nano keypad library
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
Arduino IDE Upload Code
  • Enter 12345# into the keypad, followed by 09876#.
  • Check the Serial Monitor and observe the state of the servo motor.
COM6
Send
The incorrect password! try again The correct password! Rotating Servo Motor to 90° Rotating Servo Motor to 0°
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

The valid passwords are pre-defined in the Arduino Nano code. A string, referred to as input_password, is used to store the password inputted by users. On the keypad, two keys (* and #) are used for special purposes: clearing the password and terminating the password. When a key is pressed:

  • If the key is not one of the two special keys, it is added to the input_password.
  • If the key is *, the input_password is cleared. This can be used to start or restart the password input.
  • If the key is #:
    • The Arduino Nano compares input_password with the pre-defined passwords. If it matches one of them, the servo motor is rotated to 90°.
    • Regardless of whether the password is correct or not, the input_password is cleared for the next input.
    • After a timeout, Arduino Nano rotates the servo motor to 0°.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!