ESP8266 - Keypad - Relay

This tutorial instructs you how to use an ESP8266 and a keypad to control a relay. When the user enters the correct password on the keypad, the ESP8266 will activate the relay.

The tutorial additionally provides the ESP8266 code that activates a relay for a certain duration and then deactivates it without utilizing the delay() function. The ESP8266 code also accepts multiple passwords.

By attaching a relay to an Electromagnetic Lock, a Solenoid Lock, a Linear Actuator, a Heating Element, a Pump, or a Fan ... We can then use a keypad to control them.

ESP8266 NodeMCU keypad relay

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Keypad
1×Relay
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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 Relay

If you are unfamiliar with keypad and relay (including pinout, operation, programming, etc.), the following tutorials can help:

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and keypad relay

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.

ESP8266 Code - turn relay on if the password is correct

If the password is correct, the following code will activate a relay.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-keypad-relay */ #include <Keypad.h> #define RELAY_PIN D0 // The ESP8266 pin connected to the IN pin of relay #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 ); 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; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum password size is 32, change if needed pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, LOW); } 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! Turning ON relay"); digitalWrite(RELAY_PIN, HIGH); } 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 } } }

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 a USB cable to the ESP8266 and the PC.
  • Open the Arduino IDE, select the appropriate 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 add the keypad library.
ESP8266 NodeMCU keypad library
  • Look for “ezOutput” and locate the ezOutput library from ArduinoGetStarted.
  • Press the Install button to install the ezOutput library.
ESP8266 NodeMCU output library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the ESP8266.
Arduino IDE Upload Code
  • Tap in 1234 and then press #.
  • Afterwards, enter 9765 and hit #.
  • Check the Serial Monitor for the output and the state of the relay.
COM6
Send
The incorrect password! try again The correct password! Turning ON relay
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Authorized passwords are pre-defined in the ESP8266 code. A string is used to store the password inputted by users, referred to as input string. On the keypad, two keys (* and #) are used for special purposes: to clear the password and to terminate the password. When a key on the keypad is pressed:

  • If the pressed key is not either of the two special keys, it is added to the input string.
  • If the pressed key is *, the input string is cleared. This can be utilized to start or restart entering the password.
  • If the pressed key is #:
    • The ESP8266 checks if input string match with one of the pre-defined passwords, the relay is turned on.
    • Regardless of whether the password is correct or not, the input string is cleared for the next input.

ESP8266 Code - turn a relay on in a period of time if the password is correct

If the password is correct, the relay is switched on for 5 seconds. After that, the relay is turned off.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-keypad-relay */ #include <Keypad.h> #include <ezOutput.h> #define RELAY_PIN D0 // The ESP8266 pin connected to the IN pin of relay #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 ); ezOutput relay(RELAY_PIN); 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; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum password size is 32, change if needed relay.low(); } void loop() { relay.loop(); // MUST call the loop() function first 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! Turning ON relay"); relay.low(); // set low before making a high pulse relay.pulse(RELAY_ON_TIME); // turn on relay in RELAY_ON_TIME duration and then turn off } 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 } } }

Video Tutorial