Arduino Nano - Keypad - Beep

This tutorial instructs you how to use Arduino Nano to generate a beep when the keypad is pressed. In detail, Arduino Nano controls the piezo buzzer to emit a short sound for each key press.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Keypad
1×Piezo Buzzer
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board 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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Keypad and Piezo Buzzer

If you are unfamiliar with keypad and Piezo Buzzer (pinout, how it works, how to program ...), the following tutorials can provide guidance:

The ezBuzzer library is created to enable buzzers to beep or play a tune without interfering with other code.

Wiring Diagram

The wiring diagram between Arduino Nano and keypad piezo buzzer

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code

/* * 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-beep */ #include <Keypad.h> #include <ezBuzzer.h> const int BUZZER_PIN = 2; const int ROW_NUM = 4; // four rows const int COLUMN_NUM = 3; // three 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}; // The Arduino Nano pin connected to the column pins of the keypad char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; 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

  • Connect a USB cable between the Arduino Nano 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.
  • Click the Install button to complete the installation of the keypad library.
Arduino Nano keypad library
  • Search for “ezBuzzer” and locate the buzzer library by ArduinoGetStarted.
  • Then, press the Install button to install the ezBuzzer library.
Arduino Nano buzzer 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 Arduino Nano.
Arduino IDE Upload Code
  • Press certain keys on the keypad.
  • Listen for the beep and observe the output in the Serial Monitor.

Code Explanation

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

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!