What is the arduino encoder interface mode

08/09/2022
Encoder classification:

According to the working principle: photoelectric type, magnetoelectric type and contact brush type;

According to the engraving method of the code disc: incremental type and absolute type;


This is a brief introduction from the Internet. I have only contacted the Arduino encoder, and the others have not been used yet.

Arduino's encoders are incremental. It has a total of 5 wires.

They are "CLK", "DT", "SW", "+", "GND" respectively.

"+", "GND":

Needless to say, VCC and GND can be connected to the VCC and GND of the board.

"SW":

According to Arduino, when the knob completes one turn, the pin will emit a level jump signal, which is equivalent to the "Z" signal often referred to by the rotary encoder. In fact, this one I bought is just a switch, that is, the knob part can Press it down (similar to the volume adjustment button on a car), and the interface will generate a falling edge. Then the MCU does the relevant processing.

"CLK", "DT":

The silkscreen names displayed on this module are these two. I don't understand why this silkscreen is. It should actually correspond to the "A" and "B" signals commonly used by the encoder. The two signals are generated as follows:

What is the arduino encoder interface mode

What is the arduino encoder interface mode
positive spin

As shown in the figure above, when the knob starts to rotate forward, "A" changes from low level to high level, and "B" remains unchanged; when the knob is rotated to the predetermined position, "A" remains at high level, and "B" Then followed by a transition from low to high. That is to say, in forward rotation, "A" always starts level change with "B" first.

What is the arduino encoder interface mode
anti-rotation

Inverse rotation: Contrary to forward rotation, "B" always starts level change with "A" first.
So here, the silkscreen printed these two wires as "CLK", "DT" confuses me a bit. I haven't found any relevant information, so I'll put it aside for now, and I'll know why when it has practical application next time.

According to the above forward rotation and reverse rotation law, the rotation direction of the encoder and its rotation angle can be determined according to the information output by the encoder. The specific methods are as follows:
Connect "CLk" and "DT" to any IO port of the MCU with external interrupts. The processing method is as follows:

Configure the two IO ports as double-edge external interrupts.
When one of the IO ports detects a rising edge or a falling edge, the level state of the other IO port is detected in the interrupt function. Taking forward rotation as an example, during forward rotation, the rising edge of "A" first causes an interruption, and the obtained level state of "A" and "B" is "10", and then, the rising edge of "B" detects "A". ", "B" level state is "11".
If it keeps running forward, the level status of "A" and "B" is "10 - 11 - 01 - 00 - 10 - ...".
If it is always reversed, the level status of "A" and "B" is "01 - 11 - 10 - 00 - 01 - ..."
In this way, the rotation direction of the encoder can be determined, and after the simultaneous transition of "A" and "B" is completed, the rotation count of the encoder can be increased or decreased according to the rotation direction of the encoder.