EC11 Rotary Encoder
The EC11 rotary encoder is a type of sensor that converts rotational displacement into a series of digital pulse signals. These pulses are used to control angular displacement. The reading system typically uses a differential method, where two waveforms with a 180° phase difference are compared to improve the output signal's quality and stability. The readings are based on the difference between the two signals, which helps eliminate interference.
Module Source
Purchase Link: https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.10.211a6a4bFbOotE&id=522555699841
Baidu Netdisk Download Link:
https://pan.baidu.com/s/ 18pp1KaT2V_llizWvdIXtKA?pwd=8889
Password: 8889
Specifications
Operating Voltage: 5V
Operating Current: 1mA
Module Size: 18 x 25 mm
Rotational Angle: 360 degrees
Communication Protocol: Phase difference
Pin Count: 5 Pins (2.54mm pitch header)
Hardware Connection
Rotary encoders have three or more pins. For the most basic types, there are three main pins: A (CLK, clock), B (DT, data), and GND (ground). The other two pins (if any) are usually + (VCC) and SW (pushbutton switch).
- GND connects to the GND of the Arduino.
- +(if any) connects to the Arduino's 5V. 3.
- A (CLK) connects to one of the Arduino's digital pins (e.g., pin 2). 4.
- B (DT) Connects to another digital pin of the Arduino (e.g., pin 3). 5.
- SW (pushbutton switch, if used) connects to another digital pin (e.g., pin 4) of the Arduino, and may require an additional pull-up or pull-down resistor.
Usage Method
Enter the code:
The code mainly implements the detection of clockwise and counterclockwise rotation of the rotary encoder, as well as the button press event (if used).
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 8, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
// 定义用于旋转编码器的引脚。
const int pinA = 2; // CLK 引脚
const int pinB = 3; // DT 引脚
const int buttonPin = 4; // SW 引脚
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
void setup() {
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP); // 设置按钮作为输入与内部上拉
// 设置中断
attachInterrupt(digitalPinToInterrupt(pinA), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(pinB), updateEncoder, CHANGE);
Serial.begin(9600);
}
void loop() {
// 如果按钮被按下
if(digitalRead(buttonPin) == LOW) {
Serial.println("Button pressed!");
}
if(lastencoderValue != encoderValue) {
Serial.println(encoderValue);
lastencoderValue = encoderValue;
}
}
void updateEncoder() {
int MSB = digitalRead(pinA); //MSB = 高位
int LSB = digitalRead(pinB); //LSB = 低位
int encoded = (MSB << 1) |LSB; //将2个引脚值转换为单个数字
int sum = (lastEncoded << 2) | encoded; //将其添加到之前编码的值中
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
lastEncoded = encoded; //保存这个值以备下次使用
}
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
Usage Testing
Rotate counterclockwise, then clockwise, then press the rotary encoder.