Evan Rust
Published © GPL3+

Control 7-Segment with Keypad and Arduino

Use Elegoo's Arduino Mega Kit to set up and control a 7-segment display with a 4x4 matrix keypad.

BeginnerFull instructions provided1 hour17,680
Control 7-Segment with Keypad and Arduino

Things used in this project

Hardware components

Elegoo Ultimate Arduino Mega Kit
×1
Arduino Mega 2560 (from kit)
×1
4 digit 7 segment display
×1
4x4 Matrix Keypad
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

7 segment pinout

Code

Arduino Code

C/C++
#include <SevSeg.h>
#include <Keypad.h>

#define ROWS 4
#define COLS 4

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

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9,8,7,6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
SevSeg sevseg; //Instantiate a seven segment controller object

int keyNum = 0;

void setup()
{
  byte numDigits = 4;   
  byte digitPins[] = {30, 33, 34, 48}; //Digits: 1,2,3,4 <--put one resistor (ex: 220 Ohms, or 330 Ohms, etc, on each digit pin)
  byte segmentPins[] = {31, 35, 50, 52, 53, 32, 49, 51}; //Segments: A,B,C,D,E,F,G,Period

  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
  sevseg.setBrightness(10); //Max is 100
}

void loop()
{

  char key = keypad.getKey();
  if(key){
    if(key=='*'){
      keyNum = 0;
    }
    else{
      if(keyNum<=999){
        keyNum = (keyNum*10) + (int(key)-48);
      }
    }
  }
  
  sevseg.setNumber(keyNum,0);
  
  sevseg.refreshDisplay(); // Must run repeatedly; don't use blocking code (ex: delay()) in the loop() function or this won't work right
}

Credits

Evan Rust

Evan Rust

120 projects • 1053 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments