2.25 Rotary Encoder Module¶
In this lesson, you will learn about Rotary Encoder. A rotary encoder is an electronic switch with a set of regular pulses in strictly timing sequence. When used with IC, it can achieve increment, decrement, page turning and other operations such as mouse scrolling, menu selection, and so on.
In this example, we can connect the Rotary Encoder pin directly to the Mega 2560 Board pin, connect the GND of the Rotary Encoder to GND, 「+」 to 5V, SW to digital pin 4, DT to digital pin 3, and CLK to digital pin 2.
You will see the angular displacement of the rotary encoder printed on Serial Monitor. When you turn the rotary encoder clockwise, the angular displacement is increased; when turn it counterclockwise, the displacement is decreased. If you press the switch on the rotary encoder, the readings will return to zero.
When Rotary Encoder is used, the following situations of pin level will occur.
When rotating the shaft, dtPin will go from high level to low level.
2. clkPin will remain high level when the shaft rotates clockwise, otherwise it goes low level.
When the shaft is pressed, swPin will have low level.
From this, the program flow is shown on the right.
For detailed analysis of potential state change judgment, please refer to 1.10 State Change Detection.