Arduino - Keypad 键盘
Arduino - Keypad
Arduino - Keypad
The keypad is widely used in many devices such as door lock, ATM, calculator…
键盘广泛应用于门锁、ATM、计算器等多种设备中。
In this tutorial, we will learn:
在本教程中,我们将学习:
- How to use keypad 3x4 and keypad 4x4 with Arduino.
如何将键盘 3x4 和键盘 4x4 与 Arduino 一起使用。 - How to read value from keypad 3x4 and keypad 4x4 with Arduino.
如何使用 Arduino 从键盘 3x4 和键盘 4x4 读取值。 - How to verify the password inputted from keypad
如何验证从键盘输入的密码
About Keypad 关于键盘
The keypad is a set of buttons arranged in rows and columns (called matrix). Each button is called key
键盘是一组按行和列排列的按钮(称为矩阵)。每个按钮都称为键
Keypad has various types. Two popular types for DIY projects are keypad 3x4 (12 keys) and keypad 4x4 (16 keys).
键盘有多种类型。DIY 项目的两种流行类型是键盘 3x4(12 键)和键盘 4x4(16 键)。
Pinout 引脚排列
Keypad pins are divided into two groups: row and column.
键盘引脚分为两组:行和列。
Keypad 3x4 has 7 pins: 4 row-pins (R1, R2, R3, R4) and 3 column-pin (C1, C2, C3).
键盘 3x4 有 7 个引脚:4 个排引脚(R1、R2、R3、R4)和 3 列引脚(C1、C2、C3)。
Keypad 4x4 has 8 pins: 4 row-pins (R1, R2, R3, R4) and 4 column-pin (C1, C2, C3, C4).
键盘 4x4 有 8 个引脚:4 个排引脚(R1、R2、R3、R4)和 4 列引脚(C1、C2、C3、C4)。
How It Works 它是如何工作的
This section is the in-depth knowledge. DON’T worry if you don’t understand. Ignore this section if it overloads you, and come back in another day. Keep reading the next sections.
本节是深入的知识。如果您不明白,请不要担心。如果它使您超负荷,请忽略此部分,并在另一天再回来。继续阅读下一节。
The process of detecting the key pressing is called scanning keypad.
检测按键的过程称为扫描键盘。
It is called “scanning” because it checks one key by one key.
它被称为“扫描”,因为它逐个键检查一个键。
Row-pins are connected to Arduino’s output pins
Row-pin连接到Arduino的输出引脚
Column pins are connected to Arduino’s input pins (INPUT_PULLUP, in this state, the value of the input pin is HIGH if the key is not pressed).
列引脚连接到Arduino的输入引脚(INPUT_PULLUP,在此状态下,如果未按下该键,则输入引脚的值为HIGH)。
For each row: 对于每一行:
- Sets all row-pins is HIGH.
将所有行引脚设置为高电平。 - Sets only the current row-pin to LOW.
仅将当前行引脚设置为低电平。 - Reads the state of each column.
读取每列的状态。- If a column-pin is HIGH ⇒ key at (row, column) is NOT pressed.
如果列引脚为高电平,则不按下 (行、列) 处⇒键。 - If a column-pin is LOW ⇒ key at (row, column) is pressed.
如果列引脚为低电平,则按下 (行、列) 处⇒键。
- If a column-pin is HIGH ⇒ key at (row, column) is NOT pressed.
- Repeats the above process for the next row-pins.
对下一个行引脚重复上述过程。
※ NOTE THAT: ※ 注意事项:
The above is one of the methods to scan keypad. We can invert all HIGH to LOW and all LOW to HIGH to scan keypad.
以上是扫描键盘的方法之一。我们可以将所有高电平反转为低电平,将所有低电平反转为高电平以扫描键盘。
Why does keypad is arranged and connected as a matrix? This makes the scanning process complicated. Why do not use each key as an independent button, then the state of the key is simply determined by reading the state of a button?
为什么键盘是以矩阵的形式排列和连接的?这使得扫描过程变得复杂。为什么不把每个键都当成一个独立的按钮,那么键的状态就是通过读取一个按钮的状态来确定的呢?
⇒ As we know, an independent button requires one Arduino’s pin and GND. Let’s take keypad 4x4 as an example. If we each key as an independent button, it requires 16 Arduino pin for 16 keys plus GND pin. If we arranged a connected key in matrix form, we just need to use 8 Arduino’s pin, so we can save Arduino’s pin. In short, the answer is: to save the Arduino pins.
⇒ 众所周知,一个独立的按钮需要一个Arduino的引脚和GND。让我们以键盘 4x4 为例。如果我们每个按键作为一个独立的按钮,它需要 16 个 Arduino 引脚用于 16 个按键加上 GND 引脚。如果我们以矩阵形式排列一个连接的密钥,我们只需要使用 8 个 Arduino 的引脚,这样我们就可以保存 Arduino 的引脚。简而言之,答案是:保存Arduino引脚。
Wiring Diagram 接线图
This image is created using Fritzing. Click to enlarge image
此图像是使用 Fritzing 创建的。点击放大图片
How To Program For Keypad 如何为键盘编程
Thanks to Keypad library, using keypad with Arduino is a piece of cake, no matter whether you understand how the keypad works or not.
多亏了键盘库,无论您是否了解键盘的工作原理,使用带有 Arduino 的键盘都是小菜一碟。
Arduino Code Arduino代码
Keypad 3x4 键盘 3x4
#include <Keypad.h>const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columnschar keys[ROW_NUM][COLUMN_NUM] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}
};byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypadKeypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );void setup(){Serial.begin(9600);
}void loop(){char key = keypad.getKey();if (key){Serial.println(key);}
}
Keypad 4x4 键盘 4x4
#include <Keypad.h>const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columnschar keys[ROW_NUM][COLUMN_NUM] = {{'1','2','3', 'A'},{'4','5','6', 'B'},{'7','8','9', 'C'},{'*','0','#', 'D'}
};byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypadKeypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );void setup(){Serial.begin(9600);
}void loop(){char key = keypad.getKey();if (key){Serial.println(key);}
}
Quick Steps 快速步骤
- Navigate to the Libraries icon on the left bar of the Arduino IDE.
导航到 Arduino IDE 左侧栏上的 Libraries 图标。 - Search “keypad”, then find the keypad library by Mark Stanley, Alexander Brevig
搜索“键盘”,然后找到 Mark Stanley、Alexander Brevig 的键盘库 - Click Install button to install keypad library.
单击“安装”按钮安装键盘库。
- Copy the above code and open with Arduino IDE
复制上面的代码并使用Arduino IDE打开 - Click Upload button on Arduino IDE to upload code to Arduino
单击Arduino IDE上的“上传”按钮,将代码上传到Arduino - Open Serial Monitor 开放式串行监视器
- Press some keys on keypad
按键盘上的一些键 - See the result in Serial Monitor
在串行监视器中查看结果
Keypad and Password 键盘和密码
A popular application of keypad is the password input. In this application, we specify two special keys:
键盘的一个流行应用是密码输入。在此应用程序中,我们指定了两个特殊键:
- A key to start/re-start the password input. For example, key ""
用于启动/重新启动密码输入的密钥。例如,键“” - A key to terminate the password input. For example, key “#”
用于终止密码输入的键。例如,键“#”
The password will be a string that contains the remaining keys, except for two selected special keys.
密码将是一个字符串,其中包含其余密钥,但两个选定的特殊密钥除外。
When a key is pressed.
按下某个键时。
- If the key is NOT neither "" nor “#”, append the key to the user’s input password string.
如果密钥既不是“”也不是“#”,则将密钥追加到用户的输入密码字符串中。 - If the key is “#”, compare the user’s input password string with the password to determine the input password is correct or not, and then clear the user’s input password string
如果密钥为“#”,则将用户的输入密码字符串与密码进行比较,以确定输入密码是否正确,然后清除用户的输入密码字符串 - If the key is "", clear the user’s input password string
如果密钥为“”,请清除用户的输入密码字符串
Keypad - Password Code 键盘 - 密码代码
/** Created by ArduinoGetStarted.com** This example code is in the public domain** Tutorial page: https://arduinogetstarted.com/tutorials/arduino-keypad*/#include <Keypad.h>const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columnschar keys[ROW_NUM][COLUMN_NUM] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}
};byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypadKeypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );const String password = "1234"; // change your password here
String input_password;void setup(){Serial.begin(9600);input_password.reserve(32); // maximum input characters is 33, change if needed
}void loop(){char key = keypad.getKey();if (key){Serial.println(key);if(key == '*') {input_password = ""; // clear input password} else if(key == '#') {if(password == input_password) {Serial.println("password is correct");// DO YOUR WORK HERE} else {Serial.println("password is incorrect, try again");}input_password = ""; // clear input password} else {input_password += key; // append new character to input password string}}
}
- Run above code 运行上述代码
- Open Serial Monitor 开放式串行监视器
- Press “123456” keys and press “#”
按“123456”键,按“#” - Press “1234” keys and press “#”
按“1234”键,按“#” - See the result on Serial Monitor
在串行监视器上查看结果
Video Tutorial 视频教程
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
我们正在考虑制作视频教程。如果您认为视频教程是必不可少的,请订阅我们的 YouTube 频道,为我们制作视频提供动力。
Additional Knowledge 其他知识
- How to use the multiple passwords for keypad
如何使用键盘的多个密码 - How to input a multiple digits number using the keypad
如何使用键盘输入多位数字
Challenge Yourself 挑战自我
- Display the pressed key of the keypad on LCD. Hint: Refer to Arduino - LCD
在 LCD 上显示键盘的按键。提示:请参阅Arduino - LCD - Make a door lock with password protection using the keypad.
使用键盘制作带有密码保护的门锁。
相关文章:

Arduino - Keypad 键盘
Arduino - Keypad Arduino - Keypad The keypad is widely used in many devices such as door lock, ATM, calculator… 键盘广泛应用于门锁、ATM、计算器等多种设备中。 In this tutorial, we will learn: 在本教程中,我们将学习: How to use key…...

国产芯片方案/蓝牙咖啡电子秤方案研发
咖啡电子秤芯片方案精确值可做到分度值0.1g的精准称重,并带有过载提示、自动归零、去皮称重、压低报警等功能,工作电压在2.4V~3.6V之间,满足于咖啡电子秤的电压使用。同时咖啡电子秤PCBA设计可支持四个单位显示,分别为:g、lb、oz、…...

reactjs18 中使用@reduxjs/toolkit同步异步数据的使用
react18 中使用reduxjs/toolkit 1.安装依赖包 yarn add reduxjs/toolkit react-redux2.创建 store 根目录下面创建 store 文件夹,然后创建 index.js 文件。 import { configureStore } from "reduxjs/toolkit"; import { counterReducer } from "…...

剧本杀小程序:助力商家发展,提高游戏体验
近几年,剧本杀游戏已经成为了当下年轻人娱乐的游戏社交方式。与其他游戏相比,剧本杀游戏具有强大的社交性,玩家在游戏中既可以推理玩游戏,也可以与其他玩家交流互动,提高玩家的游戏体验感。 随着互联网的发展…...

pikachu靶场 利用Rce上传一句话木马案例(工具:中国蚁剑)
目录 一、准备靶场,进入RCE 二、测试写入文件 三、使用中国蚁剑 一、准备靶场,进入RCE 我这里用的是pikachu 打开pikachu靶场,选择 RCE > exec "ping" 测试是否存在 Rce 漏洞 因为我们猜测在这个 ping 功能是直接调用系统…...

CenterOS7安装java
CenterOS7安装java #进入安装目录 cd /usr/local/soft/java#wget下载java8 #直接进入官网选择相应的版本进行下载,然后把下载链接复制下来就可以下载了 #不时间的下载链接不一样 wget http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c9…...
react 重新加载子组件
在React中,要重新加载某个子组件,你可以通过改变该组件的key属性来强制它重新渲染。这是因为React会在key变化时销毁旧的组件实例并创建一个新的实例。 多的不说直接上代码 import React, { useState } from react; import ChildComponent from ../chil…...

从零开始使用WordPress搭建个人网站并一键发布公网详细教程
文章目录 前言1. 搭建网站:安装WordPress2. 搭建网站:创建WordPress数据库3. 搭建网站:安装相对URL插件4. 搭建网站:内网穿透发布网站4.1 命令行方式:4.2. 配置wordpress公网地址 5. 固定WordPress公网地址5.1. 固定地…...
浅谈chrome引擎
Chrome引擎主要包括其浏览器内核Blink、JavaScript引擎V8以及其渲染、网络、安全等子系统。下面我将对这些关键部分进行简要说明分析 1. Blink浏览器内核 Blink是Google开发的浏览器排版引擎,自Chrome 28版本起替代了Webkit作为Chrome的渲染引擎。Blink基于Webkit…...
【常用知识点-Java】创建文件夹
Author:赵志乾 Date:2024-07-04 Declaration:All Right Reserved!!! 1. 简介 java.io.File提供了mkdir()和mkdirs()方法创建文件夹,两者区别:mkdir()仅创建单层文件夹,如…...
【JavaScript脚本宇宙】颜色处理神器大比拼:哪款JavaScript库最适合你?
提升设计与开发效率:深入解析六大颜色处理库 前言 在现代前端开发中,颜色处理是设计和用户体验的重要组成部分。无论是网页设计、数据可视化还是图形设计,都需要强大的颜色处理功能来实现多样化的视觉效果。本文将探讨几种流行的JavaScript…...

怎么录制电脑内部声音?好用的录音软件分享,看这篇就够了!
如何录制电脑内部声音?平时使用电脑工作,难免会遇到需要录音的情况。好用的录音软件有很多,也有部分录屏工具也支持录音功能。 那么如何录制电脑内部声音呢?本文整理了几个录制电脑内部声音的方法,如果你需要在电脑上录…...
ios CCNSDate.m
// // CCNSDate.h // CCFC // // Created by xichen on 11-12-17. // Copyright 2011年 ccteam. All rights reserved. //#import <Foundation/Foundation.h>interface NSDate(cc)// 获取系统时间(yyyy-MM-dd HH:mm:ss.SSS格式)(NSString *)getSystemTimeStr;// prin…...

Windows系统安装SSH服务结合内网穿透配置公网地址远程ssh连接
前言 在当今的数字化转型时代,远程连接和管理计算机已成为日常工作中不可或缺的一部分。对于 Windows 用户而言,SSH(Secure Shell)协议提供了一种安全、高效的远程访问和命令执行方式。SSH 不仅提供了加密的通信通道,…...

虚拟机与主机的联通
本地光纤分配地址给路由器--》连结路由器是连结局域网--》由路由器分配IP地址 因此在网站上搜索的IP与本机的IP是不一样的 1.windows查看主机IP地址 在终端输入 2.linux虚拟机查看ip 3.主机是否联通虚拟机ping加ip...

2024年中国网络安全市场全景图 -百度下载
是自2018年开始,数说安全发布的第七版全景图。 企业数智化转型加速已经促使网络安全成为全社会关注的焦点,在网络安全边界不断扩大,新理念、新产品、新技术不断融合发展的进程中,数说安全始终秉承科学的方法论,以遵循…...
Linux脚本自动安装 docker
使用官方安装脚本自动安装 需使用 root 或sudu 权限账户安装 安装命令如下: curl -fsSL https://test.docker.com -o install-docker.shsudo sh install-docker.sh脚本中指令: –version 安装指定版本 Use the --version option to install a specific version, f…...
【计算智能】遗传算法(一):基本遗传算法(SGA)理论简介
前言 本系列文章架构概览: 编辑 本文将介绍基本遗传算法在解决优化问题中的应用,通过实验展示其基本原理和实现过程:选取一个简单的二次函数作为优化目标,并利用基本遗传算法寻找其在指定范围内的最大值。 1. 遗传算法(GA&…...
win10系统让当前用户拥有管理员权限
本方法应该也适用于win11 大家在安装系统的时候,如果开始你不重新建立一个账号。直接使用默认的administror登录,那么这个时候电脑只有1个账户,但是如果你在刚开始的时候建立了一个新的,比如你姓李 名字叫帅哥,那么这…...

Redis持久化的三种方式(RDB、AOF和混合)
Redis持久化的三种方式(RDB、AOF和混合) 目录 Redis持久化的三种方式(RDB、AOF和混合)介绍RDB示例1.配置文件2.触发 RDB 快照保存3.验证 AOF示例1.配置文件2.校验 混合型持久化存储配置文件 介绍 Redis数据主要存储与内存中,因此如果服务器意外重启、宕机、崩溃&am…...

eNSP-Cloud(实现本地电脑与eNSP内设备之间通信)
说明: 想象一下,你正在用eNSP搭建一个虚拟的网络世界,里面有虚拟的路由器、交换机、电脑(PC)等等。这些设备都在你的电脑里面“运行”,它们之间可以互相通信,就像一个封闭的小王国。 但是&#…...
【网络】每天掌握一个Linux命令 - iftop
在Linux系统中,iftop是网络管理的得力助手,能实时监控网络流量、连接情况等,帮助排查网络异常。接下来从多方面详细介绍它。 目录 【网络】每天掌握一个Linux命令 - iftop工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景…...
React hook之useRef
React useRef 详解 useRef 是 React 提供的一个 Hook,用于在函数组件中创建可变的引用对象。它在 React 开发中有多种重要用途,下面我将全面详细地介绍它的特性和用法。 基本概念 1. 创建 ref const refContainer useRef(initialValue);initialValu…...
在Ubuntu24上采用Wine打开SourceInsight
1. 安装wine sudo apt install wine 2. 安装32位库支持,SourceInsight是32位程序 sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine32:i386 3. 验证安装 wine --version 4. 安装必要的字体和库(解决显示问题) sudo apt install fonts-wqy…...

Windows安装Miniconda
一、下载 https://www.anaconda.com/download/success 二、安装 三、配置镜像源 Anaconda/Miniconda pip 配置清华镜像源_anaconda配置清华源-CSDN博客 四、常用操作命令 Anaconda/Miniconda 基本操作命令_miniconda创建环境命令-CSDN博客...

什么是VR全景技术
VR全景技术,全称为虚拟现实全景技术,是通过计算机图像模拟生成三维空间中的虚拟世界,使用户能够在该虚拟世界中进行全方位、无死角的观察和交互的技术。VR全景技术模拟人在真实空间中的视觉体验,结合图文、3D、音视频等多媒体元素…...

VisualXML全新升级 | 新增数据库编辑功能
VisualXML是一个功能强大的网络总线设计工具,专注于简化汽车电子系统中复杂的网络数据设计操作。它支持多种主流总线网络格式的数据编辑(如DBC、LDF、ARXML、HEX等),并能够基于Excel表格的方式生成和转换多种数据库文件。由此&…...

ZYNQ学习记录FPGA(一)ZYNQ简介
一、知识准备 1.一些术语,缩写和概念: 1)ZYNQ全称:ZYNQ7000 All Pgrammable SoC 2)SoC:system on chips(片上系统),对比集成电路的SoB(system on board) 3)ARM:处理器…...

Linux 下 DMA 内存映射浅析
序 系统 I/O 设备驱动程序通常调用其特定子系统的接口为 DMA 分配内存,但最终会调到 DMA 子系统的dma_alloc_coherent()/dma_alloc_attrs() 等接口。 关于 dma_alloc_coherent 接口详细的代码讲解、调用流程,可以参考这篇文章,我觉得写的非常…...
土建施工员考试:建筑施工技术重点知识有哪些?
《管理实务》是土建施工员考试中侧重实操应用与管理能力的科目,核心考查施工组织、质量安全、进度成本等现场管理要点。以下是结合考试大纲与高频考点整理的重点内容,附学习方向和应试技巧: 一、施工组织与进度管理 核心目标: 规…...