Published

Simple Keypad

A simple keypad controlling a single digit display

IntermediateShowcase (no instructions)345
Simple Keypad

Things used in this project

Hardware components

Keypad
×1
Arduino Mega 2560
Arduino Mega 2560
×1
Single digit Number Display
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Keypad Diagram

It is the diagram of how to set up the keypad

Code

Keypad

C/C++
The code for this project
#include <Keypad.h>


const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns

char keys[ROWS][COLS] = { // Define the Keymap
  {'D','C','B','A'},
  {'#','9','6','3'},
  {'0','8','5','2'},
  {'*','7','4','1'}
};

byte rowPins[ROWS] = { 2, 3, 4, 5 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.

byte colPins[COLS] = { 6, 7, 8 ,9}; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.


Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );// Create the Keypad

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting");
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
    case '1':
    Serial.println("You've pressed one");
    digitalWrite(10, HIGH);
    delay(3000);
    digitalWrite(10, LOW);
    break;

    case '2':
    Serial.println("You've pressed two");
    digitalWrite(11, HIGH);
    delay(3000);
    digitalWrite(11, LOW);
    break;

    case '3':
    Serial.println("You've pressed three");
    digitalWrite(12, HIGH);
    delay(3000);
    digitalWrite(12, LOW);
    break;

    case '4':
    Serial.println("You've pressed four");
    digitalWrite(13, HIGH);
    delay(3000);
    digitalWrite(13, LOW);
    break;

    case '5':
    Serial.println("You've pressed five");
    digitalWrite(22, HIGH);
    delay(3000);
    digitalWrite(22, LOW);
    break;

    case '6':
    Serial.println("You've pressed six");
    digitalWrite(24, HIGH);
    delay(3000);
    digitalWrite(24, LOW);
    break;

    case '7':
    Serial.println("You've pressed seven");
    digitalWrite(26, HIGH);
    delay(3000);
    digitalWrite(26, LOW);
    break;

    case '8':
    Serial.println("You've pressed eight");
    digitalWrite(28, HIGH);
    delay(3000);
    digitalWrite(28, LOW);
    break;

    case '9':
    Serial.println("You've pressed nine");
    digitalWrite(30, HIGH);
    delay(3000);
    digitalWrite(30, LOW);
    break;

    case '0':
    Serial.println("You've pressed zero");
    digitalWrite(32, HIGH);
    delay(3000);
    digitalWrite(32, LOW);
    break;
    
    
      default:
      Serial.println(key);
    }
  }
}

Credits

Comments