Analog Keypad or Multiple Buttons on A single Arduino Pins [code example]

With Analog Keypad library , you can uses Analog Keypad or Multiple buttons easily with ONLY a single Arduino pin.

The below are example code for Analog Keypad. It is similar for multiple buttons.

/*
   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);
  }
}

You can see the library reference and more example code here: Analog Keypad

1 Like

Quite a nice idea packed into a library.

Thank you.


How about including a schematic showing the switch and resistor interconnections for your solderless breadboard layout.

Hi, @IoT_hobbyist

I notice on your very informative and setout website that you mostly give Fritzy images instead of circuit diagrams.
A proper schematic in each project will top off a very good website.

Tom... :smiley: :+1: :coffee: :australia:

Dear @LarryD , @TomGeorge Firstly, Thank you for your suggestion.
Do you mean the schematic like this ?

Hi,
Like this;

A schematic.. with part designators and values.

Tom.... :smiley: :+1: :coffee: :australia:

1 Like

I think the OP has a resistor between +5v and the first switch.

However, I like yours, but the bottom switch is funny.

Thank you all,
I took note. I will consider to your suggestion when I have more time.

Be healthy and happy!
Best regards,

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.