Analog keypad on Arduino

FunduinoPublished on

Fünf Taster über einen einzigen analogen Eingang auslesen

Fünf Taster und trotzdem nur ein einziger belegter Pin: Das AD-Keyboard-Modul legt alle Taster auf einen analogen Ausgang und unterscheidet sie über den gemessenen Wert.

In dieser Anleitung schließt du das Modul an, lernst die Analogwerte der fünf Taster kennen und lässt dir den gedrückten Taster im seriellen Monitor anzeigen.

You need this

Analog keyboard module - 5 buttons, 5 V Analog keyboard module - 5 buttons, 5 V 3,00 € *Item no. F23106483About the product — Analog keyboard module - 5 buttons, 5 V
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
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 3 parts together 15,86 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

The analog keyboard module

The AD keyboard module has five buttons, all read through a single analog input. This saves pins: instead of five digital inputs, you only need analog pin A0.

In this guide, each time you press a button, you’ll print both the measured analog value and the button’s label in the serial monitor.

Connections and analog values

Wiring is simple; the module has only three pins:

  • OUT – the analog output, connected to pin A0 on the microcontroller
  • VCC – the power supply, connected to the 5V pin on the microcontroller
  • GND – connected to the GND pin on the microcontroller

You can determine which button was pressed from the analog value. With a 5 V supply voltage, the value is 1023 as long as no button is pressed. Each button lowers the value to a different range:

  • Button 1: about 0
  • Button 2: about 30
  • Button 3: about 85
  • Button 4: about 163
  • Button 5: about 348

If you power the module with a voltage other than 5 V, these values will shift. First display the analog value in the Serial Monitor, then adjust the thresholds in the sketch.

The sketch

The sketch checks the analog value in a separate program block called Tasterabfrage(). The checks are sorted in descending order: First, it checks whether the value is above 1000 - if so, no button is pressed. The limits for the five buttons then follow from bottom to top.

In the loop section, call Tasterabfrage() and output the corresponding message in the serial monitor, depending on the return value.

Analog values between 400 and 1000 fall through all the checks and return KeinTaster. This is intentional as a fallback.

Detect a button press and display it in the Serial Monitor
int Taster;
int Analogwert = 0;
#define Taster1 1
#define Taster2 2
#define Taster3 3
#define Taster4 4
#define Taster5 5
#define KeinTaster 0
//From here, a new program block called "Tasterstatus" is created. It only checks which button is pressed.
int Tasterabfrage()
{
Analogwert = analogRead(A0); // Read the button module on analog pin A0.
Serial.println(Analogwert); //This line displays the read analog value in the serial monitor. This can be useful if the button module is powered with a voltage other than 5V, as this would result in different analog readings. Otherwise, this line can be omitted.
if (Analogwert > 1000) return KeinTaster; // As long as no button is pressed, the analog value is 1023
if (Analogwert < 15) return Taster1; //The analog value for button 1 is approx. 0
if (Analogwert < 50) return Taster2; //The analog value for button 2 is approx. 30
if (Analogwert < 120) return Taster3; //The analog value for button 3 is approx. 85
if (Analogwert < 300) return Taster4; //The analog value for button 4 is approx. 163
if (Analogwert < 400) return Taster5; //The analog value for button 5 is approx. 348
return KeinTaster; // Output when no button is pressed.
}
// This ends the program block "Tasterstatus".
void setup()
{
Serial.begin(9600); //Serial port communication is started. This is needed to display the read value in the serial monitor.
}
void loop()
{
Taster=Tasterabfrage(); //Here, the loop jumps to the program section "Tasterstatus" above and reads the pressed button.
if (Taster==0) Serial.println("No button was pressed"); // This line can be omitted if output should appear in the serial monitor only when a button is pressed.
if (Taster==1) Serial.println("Button 1 was pressed");
if (Taster==2) Serial.println("Button 2 was pressed");
if (Taster==3) Serial.println("Button 3 was pressed");
if (Taster==4) Serial.println("Button 4 was pressed");
if (Taster==5) Serial.println("Button 5 was pressed");
} //End loop
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.