108个传感器之-按钮模块(7)

108个传感器之-按钮模块(7)

介绍

按钮模块配备了一个按钮,该按钮在按下按钮时将两个信号输出在一起。它的工作电压范围为3.3 V至5 V,并且可以在-25°C至105°C的温度范围内进行操作。按钮模块的使用寿命为100,000个致动周期,可提供高可靠性和耐用性。按钮模块是需要可靠且耐用的按钮开关的应用。

初学者通常会遇到两个常见的问题:

1. 浮动输入问题

症状:输入引脚的读取值与按钮的按压状态不匹配。

原因:输入引脚不使用上拉或下拉电阻。

解决方案:使用上拉或下拉电阻。它将在本教程中描述

2. 聊天现象

仅在某些需要检测到按压数量的应用中考虑它。

症状:按下按钮,但Arduino代码多次检测到。

原因:由于机械和物理问题,按钮(或开关)的状态很快在低和高之间切换几次

技术参数
电压 3,3 V - 5 V
尺寸 18 x 15 x 6 mm
使用寿命 100,000
适用温度 -25 °C - 105°C

工作原理

一般这种按钮都有四个引脚,但是大多数情况下只需要连接其中两个即可。

![按钮引脚展示](https://oss.jzxer.cn/blog/截屏2025-01-15 10.41.40.png)

那么问题来了,既然只用到了两个引脚,为什么它却有四个引脚呢?

答案是这样的设计能让它在 pcb 板上更好的抵抗外界的压力。

引脚连接

引脚状态pin 引脚连接开发板的 gpio 接口即可:

DEV BOARD Sensor
PB6 signal
5 V +V
GND GND

代码示例

1. 监听按键点击事件

该实例使用的是 STM32C103开发板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const int BUTTON_PIN = PB6; // the number of the pushbutton pin

int lastState = HIGH; // the previous state from the input pin
int currentState; // the current reading from the input pin

void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
currentState = digitalRead(BUTTON_PIN);

if(lastState == LOW && currentState == HIGH)
Serial.println("The state changed from LOW to HIGH");

lastState = currentState;
}

2. 按键长按事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int BUTTON_PIN = PB6; // the number of the pushbutton pin

int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin

void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
currentState = digitalRead(BUTTON_PIN);

if(lastState == HIGH && currentState == LOW)
Serial.println("The button is pressed");
else if(lastState == LOW && currentState == HIGH)
Serial.println("The button is released");

// save the the last state
lastState = currentState;
}

3. 防抖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const int BUTTON_PIN = PB6;        // the number of the pushbutton pin
const int DEBOUNCE_DELAY = 50; // the debounce time; increase if the output flickers

int lastSteadyState = LOW; // the previous steady state from the input pin
int lastFlickerableState = LOW; // the previous flickerable state from the input pin
int currentState; // the current reading from the input pin

unsigned long lastDebounceTime = 0; // the last time the output pin was toggled

void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);

if (currentState != lastFlickerableState) {
// reset the debouncing timer
lastDebounceTime = millis();
// save the the last flickerable state
lastFlickerableState = currentState;
}

if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
if (lastSteadyState == HIGH && currentState == LOW)
Serial.println("The button is pressed");
else if (lastSteadyState == LOW && currentState == HIGH)
Serial.println("The button is released");

lastSteadyState = currentState;
}
}

小结

待完善…

参考

https://arduinogetstarted.com/tutorials/arduino-button

ezbutton

108个传感器之-按钮模块(7)

http://blog.jzxer.cn/20241218/20241219-button-ky004/

作者

dev

发布于

2024-12-18

更新于

2025-05-16

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×