Spring Sale: 5% OFF Over $89, Code: Elephant5; 10% OFF Over $149, Code:Elephant10



Arduino Project: 4*4 Button Module

Posted by Fiona Su on

thumb

Introduction:
In the application system of microcontroller, keyboard is essential in man-machine dialogue. When you are short of a button, you can connect one to the I/O port of the controller; but when you need a lot of buttons with limited I/O port resources, this 4*4 Matrix Keypad is no doubt your best choice.
4*4 matrix keypad is the most applied keypad form. We need to master its keypad identification technology as entry to microcontroller world. Here, we will use an examples to illustrate the identification method of 4*4 matrix keypad. The key layout is in matrix form, so with only eight I/O ports, we can identify 16 buttons, saving lots of I/O port resources.

Pin layout for 4*4 Large Button module:
thumb

Hardware required:

  • Arduino board *1
  • USB cable *1
  • 4*4 Button Module*1
  • Active buzzer*1
  • Red M5 LED*1
  • 220Ω resistor*1
  • Breadboard *1
  • Breadboard jumper wires
  • Male to Female Dupont Line

Connection:
Connection for REV4:
thumb

Connection for 2560 R3:
thumb

Sample program:

#include <Keypad.h>
int led = 10;
int buzzer = 11;
const byte ROWS = 4; // define row 4
const byte COLS = 4; // define column 4
char keys[ROWS][COLS] = {
  {'4','8','C','G'},
  {'3','7','B','F'},
  {'2','6','A','E'},
  {'1','5','9','D'}
};
// connect row ports of the button to corresponding IO ports on the board
byte rowPins[ROWS] = {9,8,7,6};	
// connect column ports of the button to corresponding IO ports on the board
byte colPins[COLS] = {5,4,3,2};
// call class library performance function of Keypad 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
}

void loop(){
digitalWrite(led, LOW); 
digitalWrite(buzzer, LOW); 
char key = keypad.getKey();
if (key != NO_KEY){
Serial.println(key);
}
if (key =='1')
{
digitalWrite(led, HIGH); 
delay(1000);
}

if (key =='2')
{
digitalWrite(buzzer, HIGH); 
delay(1000);
}
}

Result:
Upload the program to the board, open serial monitor; press certain button on the module, it will display corresponding value as below picture shows:

thumb
When serial monitor prints 1, LED lights up; when it printing 2, the buzzer rings 1S.

 

0 comments

Leave a comment

Please note, comments must be approved before they are published