How to Control LED with 4x4 Matrix Keyboard?
1.Introduction
Matrix keypads are the kind of keypads you see on cell phones, calculators, microwaves ovens, door locks, etc. They’re practically everywhere.
However, in DIY electronics, they are a great way to let users interact with your project and are often needed to navigate menus, punch in passwords and control robots.
2.Components Required
|
|
|
|
---|---|---|---|
Keyestudio Plus Mainboard*1 |
4*4 Membrane Matrix Keyboard*1 |
USB Cable*1 |
Jumper Wires |
3.Component Knowledge
4*4 Matrix keyboard:
The keyboard is a device that integrates many keys. As shown in the figure below, a 4x4 keyboard integrates 16 keys.
As with the LED matrix integration, in the 4x4 keyboard, each row of keys is connected to a pin, each column of keys is the same. This connection reduces the use of processor ports. The internal circuit is shown below.
You can use row scan or column scan methods to detect the state of the keys on each column or each line. Take the column scan method as an example. Send a low level to column 4 (Pin4), detect the state of rows 1, 2, 3 and 4, and determine whether the A, B, C and D keys are pressed. Then send the low level to columns 3, 2, 1 in turn, and detect whether other keys are pressed. Then you can get the state of all keys.
4.Read the Value
We start with a simple code to read the values of the 4*4 matrix keyboard and print them in the serial monitor. Its wiring diagram is shown below.
Install the Keypad Library:
We will use the Keypad library in all the following code examples.
Note: The library files are required in the code. If you have already added the “Keypad” library files, ignore the process of adding the library file below.
Decompress the library files in the folder, that is, put the decompressed “Keypad” folder into “Arduino libraries” under the compiler installation directory.
After successful placement, you need to restart the compiler, otherwise the compilation will not work.
e.g:C: \Program Files\Arduino\libraries
/*
4x4_Keypad_display
http//www.keyestudio.com
*/
#include
const byte ROWS = 4; // define the row 4
const byte COLS = 4; // define the row 4
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// connect the port of 4*4 keypad to the corresponding digital port of the control board
byte rowPins[ROWS] = {2,3,4,5};
// connect the port of 4*4 keypad to the corresponding digital port of the control board
byte colPins[COLS] = {6,7,8,9};
// call on the corresponding functions from libraries
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
}
void loop(){
char key = keypad.getKey();
if (key != NO_KEY){
Serial.println(key);
}
}
Upload the code to the Plus mainboard, wire up and power on first. Then open the serial monitor, press the button of the 4*4 matrix keyboard to read the corresponding values in the open serial monitor.
The monitor will show the corresponding key values as you press keys on the keypad.
5. Test Code
Use the above connection diagram. Now we use the 4 * 4 matrix keyboard to control the LED on the pin 13 of the Mainboard.
/*
Control the LED with 4x4 Matrix Keyboard
http//www.keyestudio.com
*/
#include
const byte ROWS = 4; // define row 4
const byte COLS = 4; // define row 4
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
// connect the port of 4*4 keypad to the corresponding digital port of the control board
byte rowPins[ROWS] = { 2, 3, 4, 5 };
// connect the port of 4*4 keypad to the corresponding digital port of the control board
byte colPins[COLS] = { 6, 7, 8, 9 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
byte ledPin = 13;
boolean blink = false;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // set the digital pins to OUTPUT
digitalWrite(ledPin, HIGH); // set LED to light up
keypad.addEventListener(keypadEvent); //add EventListener to the keyboard
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key);
}
if (blink) {
digitalWrite(ledPin, !digitalRead(ledPin));
delay(100);
}
}
//
void keypadEvent(KeypadEvent key) {
switch (keypad.getState()) {
case PRESSED:
switch (key) {
case '#': digitalWrite(ledPin, !digitalRead(ledPin)); break;
case '*':
digitalWrite(ledPin, !digitalRead(ledPin));
break;
}
break;
case RELEASED:
switch (key) {
case '*':
digitalWrite(ledPin, !digitalRead(ledPin));
blink = false;
break;
}
break;
case HOLD:
switch (key) {
case '*': blink = true; break;
}
break;
}
}
Note: Add the“Keypad”library folder we provided in the corresponding folder to the installation directory Arduino compiler library, otherwise the compilation will not work.
6.Result
Upload the code to the Plus mainboard, wire up and power on first.
The key * and # on the keypad can control the on and off of the LED connected to pin 13 on the PLUS board.
Yet, if you hold the key *, the LED connected to pin 13 will flash.