实验7——按键单模块实验

实验七 : 按键单模块

一、实验目的

用ESP32和按键模块,通过矩阵法来实现对矩阵键盘的按键读取

二、实验内容

1.连接ESP32和按键模块的引脚
2.触摸按键,ESP32获取到所触摸的按键数字,并打印到串口监视器中

三、实验设备

1.ESP32-WROOM-32D 开发板
2.3*4按键模块
3.杜邦线

四、实验步骤

1) 连接引脚
3*4矩阵键盘

按键模块
引脚依次为: 7 6 5 4 3 2 1(从左往右)

ESP32

ESP32的引脚图:
ESP32

连接

连接图

2) 环境配置

1.打开Arduino IDE,选择左上角 工具–开发板,选择对应的开发板(ESP32 Dev Module)
2.选择 工具–端口 , 选择对应的端口

3) 测试代码
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {33, 27, 14, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 0, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
}
  
void loop(){
  char key = keypad.getKey();
  
  if (key){
    Serial.println(key);
  }
}
4) 测试结果

​ 按键上的按钮为0-9,*和#。按过一次相应按钮,会在串口显示器上显示相应数字。
结果显示

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值