Keypad shield for the Arduino UNO

FunduinoPublished on

Display und sechs Taster ohne eine einzige Kabelverbindung

Ein LCD-Display an den Arduino zu verkabeln kostet schnell ein Dutzend Steckkabel. Das Keypad-Shield nimmt dir diese Arbeit ab: Du steckst es auf den Mikrocontroller auf, und Display sowie sechs Taster sind sofort angeschlossen.

In dieser Anleitung erfährst du, welche Pins das Shield belegt, welche dir noch bleiben und wie du die Tasten im Sketch ausliest – denn die hängen alle an einem einzigen analogen Pin.

Keypad shield with blue LCD display and six buttons
Keypad shield with 1602 display for the Arduino UNO

You need this

LCD Keypad Shield 1602 - 16x2 display, 5 buttons, for Arduino UNO LCD Keypad Shield 1602 - 16x2 display, 5 buttons, for Arduino UNO 4,10 € *Item no. F23107306About the product — LCD Keypad Shield 1602 - 16x2 display, 5 buttons, for Arduino UNO
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino

All 2 parts together 15,22 € *

* All prices include statutory VAT, plus shipping costs.

Material

For this project, you need:

  • 1× Arduino UNO or MEGA with a compatible USB cable
  • 1× keypad shield with a 1602 display

What the keypad shield can do

A keypad shield saves you the hassle of wiring an LCD and also gives you six buttons. Simply plug it onto the microcontroller, and the display and buttons are connected.

The special thing is that all the buttons are connected to a single analog pin, A0, via resistors of different values. The sketch reads a voltage value there and determines which button was pressed. Since A0 is used this way, there’s no socket for it on the shield.

Keypad shield with blue LCD display and six buttons
Keypad shield with 1602 display for the Arduino UNO

Attach the shield

Place the shield on the microcontroller so that the power supply pin header fits exactly into the corresponding female header on the board. The pin labeled VIN is a good reference point: it must line up with the VIN pin on the Arduino.

Press the shield down evenly until all the pins are fully seated. The shield fits both the Arduino UNO and the MEGA board.

Which pins remain free

The display uses digital pins 4, 5, 6, 7, 8, and 9; pin 10 controls the backlight. Digital pins 0, 1, 2, 3, 11, 12, and 13 remain free.

These pins are grouped together in a separate row at the top right of the shield. You’ll find the unused analog pins below the display, each labeled individually. To use them, solder a female header there.

Pins 0 and 1 are also used for the serial interface. If anything is connected to them, uploading the sketch often fails. So use pins 2, 3, 11, 12, and 13 first.

The program code

The sketch displays “Funduino GmbH” on the first line of the display. The name of the currently pressed button appears on the left of the second line, and the program's runtime in seconds appears on the right.

The LiquidCrystal library is already included in the Arduino IDE and does not need to be installed separately.

The numerical values in the Button Status program block (50, 195, 380, 555, 790) correspond to the resistors on the shield. They may vary slightly depending on the production batch. If a button is detected incorrectly, display the analog value in the Serial Monitor and adjust the thresholds.

Sketch: Read the Keypad Shield buttons
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Specify the required pins

// Create some variables
int Taster = 0;
int Analogwert = 0;

#define Tasterrechts 0
#define Tasteroben 1
#define Tasterunten 2
#define Tasterlinks 3
#define Tasterselect 4
#define KeinTaster 5

// From here on, a new program block named "Tasterstatus" is created. It checks only which button is pressed.
int Tasterstatus()
{
  Analogwert = analogRead(A0); // Read the buttons on analog pin A0.
  if (Analogwert > 1000) return KeinTaster;
  if (Analogwert < 50)   return Tasterrechts;
  if (Analogwert < 195)  return Tasteroben;
  if (Analogwert < 380)  return Tasterunten;
  if (Analogwert < 555)  return Tasterlinks;
  if (Analogwert < 790)  return Tasterselect;
  return KeinTaster; // Output when no button is pressed.
}
// End of the "Tasterstatus" program block.

void setup()
{
  lcd.begin(16, 2);            // Start the program library.
  lcd.setCursor(0, 0);         // Set the cursor to the top left.
  lcd.print("Funduino GmbH");  // Display the text on the first line.
}

void loop()
{
  lcd.setCursor(12, 1);        // Move the cursor to position 12 in line 2 (0 = line 1, 1 = line 2).
  lcd.print(millis() / 1000);  // Display the seconds since the program started.
  lcd.setCursor(0, 1);         // Move the cursor to position 0 in line 2.

  Taster = Tasterstatus();     // Here the loop jumps to the "Tasterstatus" section specified above and reads which button is pressed.

  switch (Taster) // The following switch-case statement decides what is displayed on the LCD depending on which button is pressed.
  {
    case Tasterrechts: // If the right button was pressed ...
      {
        lcd.print("Right      "); // ... this line appears. The spaces after the text ensure that the previous text on the line is cleared.
        break;
      }
    case Tasterlinks:  // If the left button was pressed ...
      {
        lcd.print("Left       "); // ... this line appears, and so on.
        break;
      }
    case Tasteroben:
      {
        lcd.print("Up        ");
        break;
      }
    case Tasterunten:
      {
        lcd.print("Down       ");
        break;
      }
    case Tasterselect:
      {
        lcd.print("SELECT      ");
        break;
      }
    case KeinTaster:
      {
        lcd.print("No button ");
        break;
      }
  } // End switch-case statement
} // End loop

3D-printed enclosure

Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.