Led Matrix and Numeric Keypad Example

This example shows you how use a 8x8 Led Matrix and pilot it by numeric keypad. 
In particular it is used a Led Matrix to print the number pressed on the keypad. 
The matrix used in this sketch is the model: A-1088BS, here is shown the datasheet. 
8x8 Led Matrix 
This matrix has 16 pins that represent the cathodes connected on columns and the anodes connected on rows. 
The pins are put in two lines of 8, the first line is indicated by a little protuberance as shown in figure. 
8x8 Led Matrix, front side 
Warning: in some matrix the anodes are connected on columns and the cathodes on rows. Read the datasheet of your Matrix. 
Down is shown the schematic of Matrix. 
Matrix schematic 
The schematic shows for each the column/row the respective number of pin used on the matrix. 
The other device used in this example is a numeric keypad compounded by 4 rows and the 3 columns, as shown in the figure. 
Numeric keypad 
It has 7 wires one for each column/row.

Hardware Required

  • Arduino Mega 2560 o Mega ADK
  • 8x8 Led Matrix
  • Numeric keypad
  • 8 resistor of 220Ω
  • 2 Breadboard
  • Wires

Circuit

Fritzing circuit 
Connect the seven wires of the keypad to Arduino board from pin 9 to pin 3 (the first four pins represent the rows, the other 3 pins represent the columns of the keypad). 
Do this connections for the Led Metrix: 
-Connect the pin 1 of the Matrix to pin 22 of the board. 
-Connect the pin 2 of the Matrix to pin 24 of the board. 
-Connect the pin 3 of the Matrix in series to a resistor of 220Ω and connect it to pin 26 of the board. 
-Connect the pin 4 of the Matrix in series to a resistor of 220Ω and connect it to pin 28 of the board. 
-Connect the pin 5 of the Matrix to pin 30 of the board. 
-Connect the pin 6 of the Matrix in series to a resistor of 220Ω and connect it to pin 32 of the board. 
-Connect the pin 7 of the Matrix to pin 34 of the board. 
-Connect the pin 8 of the Matrix to pin 36 of the board. 
-Connect the pin 9 of the Matrix to pin 21 of the board. 
-Connect the pin 10 of the Matrix in series to a resistor of 220Ω and connect it to pin 20 of the board. 
-Connect the pin 11 of the Matrix in series to a resistor of 220Ω and connect it to pin 19 of the board. 
-Connect the pin 12 of the Matrix to pin 18 of the board. 
-Connect the pin 13 of the Matrix in series to a resistor of 220Ω and connect it to pin 17 of the board. 
-Connect the pin 14 of the Matrix to pin 16 of the board. 
-Connect the pin 15 of the Matrix in series to a resistor of 220Ω and connect it to pin 15 of the board. 
-Connect the pin 16 of the Matrix in series to a resistor of 220Ω and connect it to pin 14 of the board. 
Now connect the board to PC with a USB cable and upload the sketch.

Schematic

Schematic

Code

This sketch includes the keypad.h library to use the function "char key = keyp.getKey()" and to get the character pressed on keypad. 
But before it needs define the arrays columns and rows, create the matrix called "Keys"and instantiate the keypad object called "keyp". 
Regarding the Led Matrix, are defined the arrays containing the pins, the columns and the rows used to turn on/off the Led. 
A led turns on when its cathode (column) is connected to ground and its anode (row) is connected to 5V, then if you put the respective column to LOW and the respective row to HIGH the led will turn on. 
For this it is used the function "digitalWrite()". 
Also are defined the function to turn off all led(AllLedOFF()), the function to turn on a single led(LedON(r,c) or a single column (columnON(c))and the functions to print the specific number. 
The read value by keypad is stored into variable "key" and passed to the construct "switch" to call the respective function and to print on the Led Matrix the number pressed. 
Note: 
The variable "Key" is a character and its value is a number between 48 to 57, that represent in ASCII code the number between 0 to 9.

The complete code and its detailed description are shown down.

 
 
/*Matrix 8x8 piloted by numeric keypad*/
#include <Keypad.h>//include keypad library
byte colPins[3] = {5,4,3}; // insert the pins connected to columns of the keypad
byte rowPins[4] = {9,8,7,6}; // insert the pins connected to rows of the keypad
char Keys[4][3]= //create the matrix of keypad
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
Keypad keyp = Keypad(makeKeymap(Keys), rowPins, colPins,4,3);//create Keypad object called keyp
 
int pins[17]= {-1, 22, 24, 26, 28, 30, 32, 34, 36, 21, 20, 19, 18, 17, 16, 15, 14};// insert the pins conected to Led matrix (the first pin is fictitious and it is used to start the array with index 1)
int cols[8] = {pins[13], pins[3], pins[4], pins[10], pins[06], pins[11], pins[15], pins[16]}; //insert columns of the Led matrix
int rows[8] = {pins[9], pins[14], pins[8], pins[12], pins[1], pins[7], pins[2], pins[5]};//insert rows of the Led matrix
/*Function to turn off all leds*/
void AllLedOFF()
{//the diodes are polarized inverse (anodes to ground and cathodes to 5V)n
for (int i = 1; i <= 8; i++)
{
      digitalWrite(cols[i - 1], HIGH);
      digitalWrite(rows[i - 1], LOW);
}
}
/*Function to turn on a led on row r and column c*/
void LedON(int r, int c)
{
digitalWrite(cols[c - 1], LOW);
digitalWrite(rows[r - 1], HIGH);
AllLedOFF();
}
/*Function to turn on the column c*/
void columnON(int c)
{
  for (int r=1; r<=8; r++)
      digitalWrite(rows[r - 1], HIGH);
  digitalWrite(cols[c-1], LOW);
  AllLedOFF();
}
/*Print zero*/
void zero(){
  for (int i=3; i<7; i++){
        LedON(1,i);
        LedON(8,i);}
  LedON(2,4);
  LedON(2,5);
  LedON(7,4);
  LedON(7,5);
  for (int i=1; i<7; i++)
      digitalWrite(rows[i],HIGH);
  digitalWrite(cols[1],LOW);
  digitalWrite(cols[2],LOW);
  digitalWrite(cols[5],LOW);
  digitalWrite(cols[6],LOW);
}
/*Print one*/
void one(){
columnON(4);
columnON(5);
LedON(2,3);
for(int i=3;i<7;i++){
    LedON(7,i);
    LedON(8,i);}
}
/*Print two*/
void two(){
for(int i=3; i<7; i++)
    LedON(1,i);
LedON(3,2);
LedON(3,3);
LedON(4,7);
for(int i=3; i<7; i++)
    LedON(i,10-i);
for(int i=3; i<7; i++)
    LedON(i,9-i);
for (int i=1; i<7; i++)
    digitalWrite(cols[i],LOW);
digitalWrite(rows[1],HIGH);
digitalWrite(rows[6],HIGH);
digitalWrite(rows[7],HIGH);
}
/*Print three*/
void three(){
for(int i=3;i<7;i++)
    LedON(i,6);
for(int i=2;i<5;i++)
    LedON(i,7);
LedON(6,7);
LedON(7,7);
LedON(2,2);
LedON(3,2);
LedON(6,2);
LedON(7,2);
LedON(3,3);
LedON(6,3);
LedON(4,5);
LedON(5,5);
for(int i=2; i<6; i++)
    digitalWrite(cols[i],LOW);
digitalWrite(rows[0],HIGH);
digitalWrite(rows[1],HIGH);
digitalWrite(rows[6],HIGH);
digitalWrite(rows[7],HIGH);
}
/*Print four*/
void four(){
for(int i=3;i<9;i++){
    LedON(6,i);
    LedON(7,i);}
columnON(6);
columnON(7);
for(int i=2;i<6;i++){
    LedON(6-i,i);
    LedON(7-i,i);
    LedON(8-i,i);}
}
/*Print five*/
void five(){
LedON(6,2);
for(int i=2;i<8;i++){
    LedON(1,i);
    LedON(2,i);
    LedON(7,i);}
for(int i=5;i<9;i++){
    LedON(i,6);
    LedON(i,7);}
for(int i=3;i<8;i++){
    LedON(8,i);}
for(int i=4;i<7;i++){
    LedON(4,i);
    LedON(5,i);}
for(int i=2;i<4;i++){
    LedON(3,i);
    LedON(4,i);}    
}
/*Print six*/
void six(){
for(int i=3;i<8;i++){
    LedON(1,i);
    LedON(2,i);}
for(int i=2;i<7;i++){
    LedON(4,i);
    LedON(5,i);}
for(int i=3;i<7;i++){
    LedON(7,i);
    LedON(8,i);}  
for(int i=2;i<8;i++){
    LedON(i,2);
    LedON(i,3);}
for(int i=5;i<8;i++){
    LedON(i,6);
    LedON(i,7);}  
}
/*Print seven*/
void seven(){
LedON(3,6);
LedON(3,7);
LedON(8,2);
LedON(8,3);
for(int i=2;i<8;i++){
    LedON(1,i);
    LedON(2,i);}
for(int i=4;i<8;i++){
    LedON(i,9-i);
    LedON(i,10-i);
    LedON(i,11-i);}  
}
/*Print eight*/
void eight(){
  for(int i=3;i<7;i++){
      LedON(1,i);
      LedON(2,i);
      LedON(4,i);
      LedON(7,i);
      LedON(8,i);}
  for(int i=2;i<4;i++){
      LedON(2,i);
      LedON(3,i);
      LedON(5,i);
      LedON(6,i);
      LedON(7,i);}
  for(int i=6;i<8;i++){
      LedON(2,i);
      LedON(3,i);
      LedON(5,i);
      LedON(6,i);
      LedON(7,i);}
}
/*Print nine*/
void nine(){
  LedON(7,2);
  for(int i=3;i<7;i++){
      LedON(1,i);
      LedON(2,i);
      LedON(4,i);
      LedON(5,i);
      LedON(7,i);
      LedON(8,i);}
for(int i=2;i<4;i++){
    LedON(i,2);
    LedON(i,3);}
for(int i=2;i<8;i++){
    LedON(i,6);
    LedON(i,7);}
}
/*setup*/
void setup()
{
for (int i = 1; i <= 16; i++)
      pinMode(pins[i], OUTPUT);//define the pins of matrix as output
}
void loop()
{
AllLedOFF();//turn off the leds
char key = keyp.getKey();//get the character pressed on keypad
  switch(key){
    case 48://zero in ASCII
    for(int i=0;i<250;i++){
            AllLedOFF();//turn off leds
            zero();//call the function: zero
            }
            break;
    case 49://one in ASCII
          for(int i=0;i<250;i++){
            AllLedOFF();//turn off leds
            one();//call the function: one
            }
         
            break;
    case 50://two in ASCII
            for(int i=0;i<250;i++){
            AllLedOFF();//turn off leds
            two();//call the function: two
            }
            break;
    case 51://three in ASCII
            for(int i=0;i<250;i++){
            AllLedOFF();//turn off leds
            three();//call the function: three
            }
            break;
    case 52://four in ASCII
            for(int i=0;i<150;i++){
            AllLedOFF();//turn off leds
            four();//call the function: four
            }
            break;
    case 53://five in ASCII
            for(int i=0;i<100;i++){
            AllLedOFF();//turn off leds
            five();//call the function: five
            }
            break;
    case 54://six in ASCII
            for(int i=0;i<100;i++){
            AllLedOFF();//turn off leds
            six();//call the function: six
            }
            break;  
    case 55://seven in ASCII
            for(int i=0;i<150;i++){
            AllLedOFF();//turn off leds
            seven();//call the function: seven
            }
            break;
    case 56://eight in ASCII
            for(int i=0;i<100;i++){
            AllLedOFF();//turn off leds
            eight();//call the function: eight
            }
            break;
    case 57://nine in ASCII
            for(int i=0;i<100;i++){
            AllLedOFF();//turn off leds
            nine();//call the function: nine
            }
            break;
        }            
}