fbpixel
Tags:

Whether it’s a calculator or a building keypad, we commonly use numeric keypads. The 4×4 numeric keyboard is a matrix of 16 buttons whose states can be detected by a microcontroller. This module requires only one analog input from the microcontroller unlike the 4×4 digital keyboard.

robotdyn-analog-keypad-4x4-1 Managing a 4x4 Analog Keyboard with Arduino

Material

  • Computer
  • Arduino UNO
  • USB cable A Male/B Male
  • AnalogKeypad4x4
  • cable Dupont F/M x3

Principle of operation

The digital keyboard with analog output is a set of 16 buttons that are mounted in a matrix with pullup resistors of different values. When a button is pressed the circuit will close with the resistor and the voltage value on the analog output will be different for each button. The advantage of this type of assembly is that we can manage 16 buttons with only 1 analog input of the microcontroller. However, it is not possible to press two buttons at the same time.

robotdyn-analog-keypad-4x4-principle Managing a 4x4 Analog Keyboard with Arduino

Scheme

The digital keyboard uses an analog input of the Arduino. So we connect the output of the keyboard to the A0 pin of the microcontroller. The keyboard can be powered by the 5V output of the Arduino.

robotdyn-analog-keypad-4x4-pinout Managing a 4x4 Analog Keyboard with Arduino

Code

To manage the numeric keyboard, the principle is to read the value of the analog input that corresponds to a button. To do this we will compare the value of the analog pin to the value given to each button.

It is important to note that if two buttons are pressed, only the button with the highest value will be retained.

//Constants
#define nbABtn 16

//Parameters
const int abtnPin = A0;
const int valThresh[nbABtn] = {1000, 900, 820, 750, 660, 620, 585, 540, 500, 475, 455, 425, 370, 300, 260, 200};

void setup() {
  //Init Serial USB
  Serial.begin(9600);
  Serial.println(F("Initialize System"));
}

void loop() {
  readAbtn();
}

void readAbtn() { /* function readAbtn */
  //// Read button states from keypad
  int btnId = getABtn();
  if (btnId) {
    Serial.print("Button pressed : "); Serial.println(btnId);
    delay(200);
  }
}

int getABtn() { /* function getABtn */
  //// Read button states from keypad
  int val = analogRead(abtnPin);
  if (val <= 200) {
    return 0;
  } else {
    for (int i = 0; i < 16; i++) {
      if (val > valThresh[i]) return i + 1;
    }
  }
}

Result

When a key on the keyboard is pressed, we observe that the associated value is displayed correctly in the serial monitor.

robotdyn-analog-keypad-4x4-serial-monitor Managing a 4x4 Analog Keyboard with Arduino

Applications

  • Create an interface with several buttons
  • Create a lock operated by digicode
  • Develop an alarm that can be activated or deactivated with a password
  • Developing a MIDI interface

Sources

Retrouvez nos tutoriels et d’autres exemples dans notre générateur automatique de code
La Programmerie