Click here to Skip to main content
12,953,743 members (51,673 online)
Click here to Skip to main content
Add your own
alternative version


Posted 10 Jan 2014

ADKeyboard Library for Arduino

, 10 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A library for 5-button analog keypad


I have just bought a 5-button keypad from cooking-hacks. I like it, because it is useful for simple tasks and costs only 1 analog pin on your Arduino. The usage is also simple. You read different values from the analog pin for each button, therefore you can understand which button is pressed. Here is the keyboard and its sample code. What I did is to package them up into a library.

Using the Code

When you place the library files (AdKeyboard.cpp, AdKeyboard.h) in your libraries folder of Arduino IDE, you are ready to use it in your project.

// include the header file
#include <AdKeyboard.h>
// instantiate it on Analog0 of the arduino board
AdKeyboard adKeyboard(0); 

You can attach your codes to the events by providing callback functions to the keyboard object in the setup. There are 3 events that I have implemented.

  • Click event: Just simple press & release event.
  • Press event: Press & hold event. When you hold a key for a while (default value is 2sec.), attached callback function is executed if provided.
  • Two-press event: Like press event, but you have to hold down two keys concurrently to fire this event. There is a tricky point here. You should press higher-indexed key first, because lower-indexed key masks higher ones because of the hardware implementation.

Let's attach callback functions to the adKeyboard object.

void setup() {

And the callback functions:

void clickHandler(int key) {
  Serial.print("clickHandler: ");
void pressHandler(int key) {
  Serial.print("pressHandler: ");
void twoPressHandler(int k1, int k2) {
  Serial.print("twoPressHandler: ");
  Serial.print(" ");

There is one point left. You have to monitor keyboard in your main loop.

void loop() {

That is it. Now you have a 5-button keypad for your projects. Enjoy it!

You can download the source code from the link at the top of this tip.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vedat Ozan Oner
Turkey Turkey
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Member 1201422123-May-17 6:19
memberMember 1201422123-May-17 6:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 10 Jan 2014
Article Copyright 2014 by Vedat Ozan Oner
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid