ESP8266 - Keypad - Beep

This tutorial instructs you how to use the ESP8266 to produce a beep sound when a keypad is pressed. Specifically, the ESP8266 controls the piezo buzzer and emitting a brief sound for each key press.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Keypad
1×Piezo Buzzer
1×Breadboard
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 Piezo Buzzer

If you are unfamiliar with keypad and Piezo Buzzer (including pinouts, operations, and programming), the following tutorials can provide assistance:

The ezBuzzer library has been created to enable buzzers to beep or play melodies without interfering with other code.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and keypad piezo buzzer

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

/* * 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-beep */ #include <Keypad.h> #include <ezBuzzer.h> #define BUZZER_PIN D0 // The ESP8266 pin connected to the buzzer's pin #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 ); ezBuzzer buzzer(BUZZER_PIN); // create ezBuzzer object that attach to a pin; void setup() { Serial.begin(9600); } void loop() { buzzer.loop(); // MUST call the buzzer.loop() function in loop() char key = keypad.getKey(); if (key) { Serial.print(key); // prints key to serial monitor buzzer.beep(100); // generates a 100ms beep } }

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 the ESP8266 to a computer using a USB cable.
  • Open the Arduino IDE and 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.
ESP8266 NodeMCU keypad library
  • Search for “ezBuzzer”, then locate the buzzer library by ArduinoGetStarted.
  • Press the Install button to install the ezBuzzer library.
ESP8266 NodeMCU buzzer library
  • Copy the code and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to compile and upload the code to ESP8266.
Arduino IDE Upload Code
  • Press certain keys on the keypad.
  • Listen for a beep and then check the Serial Monitor to view the result.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial