Arduino Nano 33 IoT - Keypad 1x4

This guide will show you how to use a 1x4 keypad with an Arduino Nano 33 IoT. We will explain these topics:

Arduino Nano 33 IoT Keypad 1x4

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Keypad 1x4
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter 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 1x4

A 1x4 keypad has four buttons in a row. People use it to type passwords, browse menus, or operate devices.

Pinout

This 1x4 keypad has five pins. The pins are not arranged in the same order as the key labels.

  • Pin 1 goes to key 2
  • Pin 2 goes to key 1
  • Pin 3 goes to key 4
  • Pin 4 goes to key 3
  • Pin 5 connects to every key and is shared by all
Keypad 1x4 Pinout
image source: diyables.io

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT Keypad 1x4

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do not use internal or external pull-down resistors for these pins

Arduino Nano 33 IoT Code

Each key on the 1x4 keypad works like a regular button, so we can use the digitalRead() function to check if a key is pressed. Like other buttons, it has a problem called bouncing, where one press might count as several presses. To fix this, we need to debounce each key. Debouncing all four keys at once without stopping other parts of the code can be hard. Fortunately, the ezBbutton library makes it much easier.

#include <ezButton.h> #define KEY_NUM 4 // the number of keys #define PIN_KEY_1 5 // The Arduino Nano 33 IoT pin connected to the key 1 #define PIN_KEY_2 4 // The Arduino Nano 33 IoT pin connected to the key 2 #define PIN_KEY_3 7 // The Arduino Nano 33 IoT pin connected to the key 3 #define PIN_KEY_4 6 // The Arduino Nano 33 IoT pin connected to the key 4 ezButton keypad_1x4[] = { ezButton(PIN_KEY_1), ezButton(PIN_KEY_2), ezButton(PIN_KEY_3), ezButton(PIN_KEY_4) }; void setup() { Serial.begin(9600); for (byte i = 0; i < KEY_NUM; i++) { keypad_1x4[i].setDebounceTime(100); // set debounce time to 100 milliseconds } } void loop() { int key = getKeyPressed(); if (key) { Serial.print("The key "); Serial.print(key); Serial.println(" is pressed"); } } int getKeyPressed() { for (byte i = 0; i < KEY_NUM; i++) keypad_1x4[i].loop(); // MUST call the loop() function first for (byte i = 0; i < KEY_NUM; i++) { // get key state after debounce int key_state = keypad_1x4[i].getState(); // the state after debounce if (keypad_1x4[i].isPressed()) return (i + 1); } return 0; }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.

To begin using Arduino Nano 33 IoT, follow these steps:

  • If you're new to the Arduino Nano 33 IoT, check out the basic guide on setting up the Arduino Nano 33 IoT in the Arduino IDE.
  • Connect the Arduino Nano 33 IoT to the 1x4 keypad.
  • Plug the Arduino Nano 33 IoT board into your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and the correct COM port.
  • Click the Libraries icon on the left side of the Arduino IDE.
  • Search for ezButton and find the button library from Arduino Nano 33 IoTGetStarted.com.
  • Click the Install button to add the ezButton library.
Arduino Nano 33 IoT button library
  • Copy the code and open it in the Arduino IDE
  • Click the Upload button in the Arduino IDE to send the code to your Arduino Nano 33 IoT
  • Open the Serial Monitor
  • Press each key on the 1x4 keypad one by one
  • Look at the results in the Serial Monitor
COM6
Send
1 2 3 4
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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