108个传感器之-旋转编码器(32)

108个传感器之-旋转编码器(32)

背景

旋转编码器在旋转时通过其输出输出开关的运动方向和当前位置。

两个输出的状态随着每一步而变化,从而可以确定旋转方向。旋转方向由输出状态变化的顺序决定:根据哪个输出首先改变状态,可以检测开关是顺时针还是逆时针转动。

这种旋转开关非常适合需要精确位置和运动信息的应用,例如电子设备的用户界面、机器控制或旋转测量系统。它提供准确可靠的输入,是许多项目的通用组件。

引脚连接

SW:是按钮开关的输出(低电平有效)。当按下旋钮时,电压变低。

DT(输出 B):与CLK输出类似,但滞后于CLK 90°相移。该输出用于确定旋转方向。

CLK(输出A):是用于确定旋转量的主要输出脉冲。每次仅通过一个制动装置(咔嗒声)向任一方向转动旋钮时,“CLK”输出就会经历一个先高后低的周期。

Arduino Sensor
5 V + V
GND GND
Pin 2 CLK
Pin 3 DT
Pin 4 SW

代码示例

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <Arduino.h>

// Rotary Encoder Inputs
#define CLK 2
#define DT 3
#define SW 4

int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;

void setup() {

// Set encoder pins as inputs
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);

// Setup Serial Monitor
Serial.begin(9600);

// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
}

void loop() {

// Read the current state of CLK
currentStateCLK = digitalRead(CLK);

// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){

// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir ="CCW";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir ="CW";
}

Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}

// Remember last CLK state
lastStateCLK = currentStateCLK;

// Read the button state
int btnState = digitalRead(SW);

//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
}

// Remember last button press event
lastButtonPress = millis();
}

// Put in a slight delay to help debounce the reading
delay(1);
}

以上代码如果烧录成功的话,在串口监视器会打印如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Direction: CW | Counter: 6
Direction: CW | Counter: 7
Direction: CW | Counter: 8
Direction: CW | Counter: 9
Direction: CW | Counter: 10
Direction: CW | Counter: 11
Direction: CW | Counter: 12
Direction: CW | Counter: 13
Button pressed!
Button pressed!
Direction: CCW | Counter: 12
Direction: CCW | Counter: 11
Direction: CCW | Counter: 10
Direction: CW | Counter: 11
Direction: CCW | Counter: 10
Direction: CCW | Counter: 9
Direction: CCW | Counter: 8
Direction: CCW | Counter: 7
Direction: CCW | Counter: 6

小结

待完善…

参考

dev 的艺术空间

108个传感器之-旋转编码器(32)

http://blog.jzxer.cn/20250103/20250103-Rotary-encoder/

作者

dev

发布于

2025-01-03

更新于

2025-01-04

许可协议

评论

Your browser is out-of-date!

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

×