The matrix keyboard can solve the problem of our lack of ports. Of course, it will be more cost-effective if we use chips to realize the lack of input ports of the matrix keyboard. This article does not use chips to solve the problem for the time being, but uses simple 8 wires to achieve Matrix keyboard, the purpose is to make beginners master the principle. If you want to know how to use chips to solve the shortage of input ports of matrix keyboards, please refer to my " How to expand with few input ports?" Application of 74hc148 or 74ls148 cascaded in Arduino to realize 16 to 4 "
Article directory
Original source of the article: https://haigear.blog.csdn.net/article/details/130027248
First, the principle of the matrix keyboard
1. Principle
Under normal circumstances, our 4x4 keyboard matrix obviously has 16 buttons. Logically speaking, we need 16 ports, but our digital ports only have 14. Even if the analog ports are changed to digital ports, we also need There are not many ports left, and if you need to connect serial ports or other devices, basically an arduino can't do anything.
Therefore, people thought of a good way to use 8 wires to complete the task that can only be done with 16 wires. The principle is scanning. When the output line OUT0 outputs high level, scan one by one to check which port IN0~IN3 is high level, because which port shows high level indicates which button is pressed.
Principle summary: judge which button is pressed through the level circuit, only the level characteristics of the corresponding input can be detected on the corresponding input port connected to the output port, which means that the button connected to them is pressed, that is, where the output port is located On the intersection button of the line where the line and the output port are located.
For example, OUT2 outputs a high level, if we can detect a high level on IN3, it means that BTN12 is pressed.
2. Chipless mode program and circuit
1. Pull-up mode
Since the port of arduino has a built-in pull-up resistor, we can use pinMode to set the pull-up, pinMode(pin,INPUT_PULLUP), so directly connect the port of the keyboard matrix to the digital port of arduino.
int count=0;const int ROWS = 4; // 定义行数
const int COLS = 4; // 定义列数
char keys[ROWS][COLS] = {
// 定义键盘矩阵数组
{
'1', '2', '3', 'A'},
{
'4', '5', '6', 'B'},
{
'7', '8', '9', 'C'},
{
'*', '0', '#', 'D'}
};
// 定义每个引脚的接口
int rowPins[ROWS] = {
11, 10, 9, 8 };
int colPins[COLS] = {
5, 4, 3, 2 };
void setup() {
Serial.begin(9600);
// 设置行引脚为输出模式
for (int i = 0; i < ROWS; i++) {
pinMode(rowPins[i], OUTPUT);
digitalWrite(rowPins[i], HIGH);
}
// 设置列引脚为输入模式
for (int i = 0; i < COLS; i++) {
pinMode(colPins[i], INPUT_PULLUP);
}
}
void loop() {
// 检测键盘按键是否被按下
for (int row = 0; row < ROWS; row++) {
digitalWrite(rowPins[row], LOW);
for (int col = 0; col < COLS; col++) {
if (digitalRead(colPins[col]) == LOW) {
Serial.println(keys[row][col]);
delay(100);
}
}
digitalWrite(rowPins[row], HIGH);
}
}
2. Pull-down mode
From the circuit point of view, we will find that in the pull-down mode, we must connect a pull-down resistor to the input pin, because the arduino pin only has a built-in pull-up resistor, and there is no pull-down resistor, so we can only connect one here. As shown below:
char keys[ROWS][COLS] = {
// 定义键盘矩阵数组
{
'1', '2', '3', 'A'},
{
'4', '5', '6', 'B'},
{
'7', '8', '9', 'C'},
{
'*', '0', '#', 'D'}
};
void setup () {
for(int a=2;a<=5;a++){
pinMode(a,OUTPUT);
}
for(int a=8;a<=11;a++){
pinMode(a,INPUT);
}
Serial.begin(9600);
// TODO: put your setup code here, to run once:
}
void loop() {
for(int o=2;o<=5;o++){
digitalWrite(o,1);
for(int i=8;i<=11;i++){
if(digitalRead(i)){
Serial.println(keys[o-2][i-8]);
delay(10);
}
}
digitalWrite(o,0);
}
}
3. Use the keypad library
We can download this library file here, http://playground.arduino.cc/uploads/Code/keypad.zip
The following code is the code provided by the official website, the URL is as follows:
https://playground.arduino.cc/Code/ Keypad/#Download
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
// 定义键盘矩阵数组
{
'1','2','3','A'},
{
'4','5','6','B'},
{
'7','8','9','C'},
{
'*','0','#','D'}
};
byte rowPins[ROWS] = {
9, 8, 7, 6};
byte colPins[COLS] = {
5, 4, 3, 2};
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);
}
}
After using the keypad, the code is very concise. Of course, we must first understand how to use the keypad library.