ezAnalogKeypad Library - Analog Keypad Example

When your project uses a keypad but IO pins are not enough, you can use an Analog Keypad (three pins). The analog keypad allows you to use only a single analog input pin. This tutorials will teach you how to the analog keypad with Arduino. It also works with ESP32, ESP8266, or other platform.

Analog Analog Keypad One Button Pin

If you want to use the standard keypad, see this Arduino - Keypad tutorial

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Analog Keypad (5 keys)
1×(Optional) Analog Keypad (12 keys)
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About ezAnalogKeypad Library

About Analog Keypad

Pinout

An analog keypad has three pins:

  • VCC pin: connect this pin to VCC (5V or 3.3v)
  • GND pin: connect this pin to GND (0V)
  • Output pin: outputs the voltage according to the key press.
Analog Keypad Pinout

The analog keypad connects to a single analog input pin of Arduino, ESP32, ESP8266... When a key is pressed, the analog value is changed to a specific value. This value is diffrent from each key. The ezAnalogKeypad library reads the analog value determine which key is pressed by finding a key that has a nearest value.

⇒ What we needs to do first: determine the analog value for each key when it is pressed, and also the analog value when no key is pressed

The analog value when a key is pressed is depending on the following factors:

  • How keys are wired
  • The value of resistors
  • The voltage supplies for the keypad
  • The voltage reference of ADC
  • The resolution of ADC (e.g. Arduino Uno is 10-bit ADC, ESP32 is 12-bit ADC)

⇒ The simplest way to find the analog values is run a test for callibration. By doing this way, we do not need to care about the above factors. We will learn how to do callibration in the next steps.

Wiring Diagram

  • 12-key analog keypad (3x4)
Arduino Analog Keypad Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • 5-key analog keypad (12 key)
Arduino Analog button Wiring Diagram

Arduino Code

Calibration

  • Install ezAnalogKeypad library. See How To
  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • On Arduino IDE, Go to File Examples ezAnalogKeypad Calibration example
/* This code is for calibration. Please do step by step: - Upload the code to Arduino or ESP32 - Without pressing any key/button, write down the value on Serial Monitor - Update this value on your code at setNoPressValue(value) function - Press each key one by one and write down the value each time press - Update these values on your code at registerKey(key, value) NOTE: when no press or press one key/button, the values on Serial Monitor may be NOT consistent, they may be slighly different => use a central value */ #define ANALOG_PIN A0 void setup() { Serial.begin(9600); } void loop() { Serial.println(analogRead(ANALOG_PIN)); delay(100); }
  • Click Upload button on Arduino IDE to upload code to Arduino
Arduino IDE Upload Code
  • See the output on Serial Monitor
  • Write down the analog value when no key is pressed. This value will be put in keypad.setNoPressValue(value) later
  • Press key one by one and write down the analog value for each key. These value will be put in keypad.registerKey(key, value) later

※ NOTE THAT:

analogValue can be fluctuated a little. You can choose a middle value. The library does not compare the analogValue equally. Instead, the library find the nearest match value.

Update the example code

  • On Arduino IDE, Go to File Examples ezAnalogKeypad AnalogKeypad example
/* Created by ArduinoGetStarted.com This example code is in the public domain Tutorial page: https://arduinogetstarted.com/library/arduino-analog-keypad-example This example reads the pressed key from analog keypad and prints it to Serial Monitor. */ #include <ezAnalogKeypad.h> ezAnalogKeypad keypad(A0); // create ezAnalogKeypad object that attach to pin A0 void setup() { Serial.begin(9600); // MUST READ: You MUST run the calibration example, press key one-by-one to get the analog values // The below values is just an example, your keypad's value may be different keypad.setNoPressValue(0); // analog value when no key is pressed keypad.registerKey('1', 100); // analog value when the key '1' is pressed keypad.registerKey('2', 150); // analog value when the key '2' is pressed keypad.registerKey('3', 200); // analog value when the key '3' is pressed keypad.registerKey('4', 250); // analog value when the key '4' is pressed keypad.registerKey('*', 450); // analog value when the key '*' is pressed keypad.registerKey('0', 500); // analog value when the key '0' is pressed keypad.registerKey('#', 550); // analog value when the key '#' is pressed // ADD MORE IF YOUR KEYPAD HAS MORE } void loop() { unsigned char key = keypad.getKey(); if (key) { Serial.println(key); } }
  • Update the value you wrote down in the previous callibration process into the example code
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Press key one by one
  • See the result on Serial Monitor
COM6
Send
1 2 3 4 5 6 7 8 9 * 0 #
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Read the line-by-line explanation in comment lines of source code!

※ NOTE THAT:

To save memory, the maximum keys can be registered is 20 by default. If you want to increase or decrease the maximum keys (to save more memory), define ezAnalogKeypad_MAX_KEY before including the library. For example:

#define ezAnalogKeypad_MAX_KEY 20 #include <ezAnalogKeypad.h>

The Best Arduino Starter Kit

※ OUR MESSAGES