ESP8266 - Keypad - Servo Motor

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

The ESP8266 code also supports multiple passwords.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Servo Motor
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

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 unfamiliar with keypad and servo motor (including pinout, how it works, and how to program), the following tutorials can help:

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and keypad servo motor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

Please note 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 provide more power for the servo motor. The below demonstrates the wiring diagram with an external power source for servo motor.


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

ESP8266 Code - rotates Servo Motor if the password is correct

If the password is correct, the servo motor will be set to 90° for a period of 5 seconds. Subsequently, it will be rotated back to 0°.

/* * This ESP8266 NodeMCU code was developed by * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #include <Keypad.h> #include <Servo.h> #define SERVO_PIN D0 // The ESP8266 pin connected to the servo motor #define ROW_NUM 4 // 4 rows #define COLUMN_NUM 3 // 3 columns char key_layout[ROW_NUM][COLUMN_NUM] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte pin_rows[ROW_NUM] = {D1, D2, D3, D4}; // The ESP8266 pins connect to the row pins byte pin_column[COLUMN_NUM] = {D5, D6, D7}; // The ESP8266 pins connect to the column pins Keypad keypad = Keypad( makeKeymap(key_layout), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); Servo servo; // servo motor 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 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

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect an USB cable to the ESP8266 and the PC.
  • Open the Arduino IDE, choose 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.
  • Press the Install button to install the keypad library.
ESP8266 NodeMCU keypad library
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
Arduino IDE Upload Code
  • Enter 12345# into the keypad.
  • Then, type in 5642B#.
  • Check the Serial Monitor and observe the servo motor's status.
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  

Code Explanation

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

  • If it is not one of the two special keys, it will be appended to the input_password.
  • If the pressed key is *, then the input_password will be cleared, allowing the user to start or re-start inputting the password.
  • If the pressed key is #:
    • The ESP8266 checks if the password is valid by comparing input_password with the pre-defined passwords. If it matches one of them, the ESP8266 rotates the servo motor to 90°.
    • Regardless of whether the password is correct or not, the input_password will be cleared for the next input.
    • After a timeout, ESP8266 rotates the servo motor to 0°.

Video Tutorial


  • 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!