ezAnalogKeypad Library - Analog Button Array Example

When your project uses multiple buttons but IO pins are not enough ⇒ buy or build a analog button array, which connects to a single analog input pin. The analog button array allows you to use only one analog input pin. This tutorials will teach you how to the analog button array with Arduino. It also works with ESP32, ESP8266, or other platform.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
n×Button
n×10k ohm resistor
1×Jumper Wires
1×Breadboard
1×(Optional) Analog Buttons board (5 buttons)
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 Button Array

You can build an analog buttons array by yourself or buy a ready-to-use one.

Arduino multiple button one analog pin

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

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

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

  • How buttons are wired
  • The value of resistors
  • The voltage supplies for the array of buttons
  • 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

  • Self-made button array
Arduino multiple button one analog pin Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • Ready-to-use buttons module
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 button is pressed. This value will be put in keypad.setNoPressValue(value) later
  • Press button one by one and write down the analog value for each button. 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 AnalogButtonArray example
/* Created by ArduinoGetStarted.com This example code is in the public domain Tutorial page: https://arduinogetstarted.com/library/arduino-analog-button-array-example This example reads the pressed button from an array of buttons connected to single analog pin and prints it to Serial Monitor. */ #include <ezAnalogKeypad.h> ezAnalogKeypad buttonArray(A0); // create ezAnalogKeypad object that attach to pin A0 void setup() { Serial.begin(9600); // MUST READ: You MUST run the calibration example, press button one-by-one to get the analog values // The below values is just an example, your button's value may be different buttonArray.setNoPressValue(0); // analog value when no button is pressed buttonArray.registerKey(1, 512); // analog value when the button 1 is pressed buttonArray.registerKey(2, 341); // analog value when the button 2 is pressed buttonArray.registerKey(3, 256); // analog value when the button 3 is pressed buttonArray.registerKey(4, 205); // analog value when the button 4 is pressed buttonArray.registerKey(5, 170); // analog value when the button 5 is pressed // ADD MORE IF HAS MORE } void loop() { unsigned char button = buttonArray.getKey(); if (button) { Serial.print("The button "); Serial.print((int)button); Serial.println(" is pressed"); } }
  • 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 button one by one
  • See the result on Serial Monitor
COM6
Send
The button 1 is pressed The button 2 is pressed The button 3 is pressed The button 4 is pressed The button 5 is pressed
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