Most devices communicate with users in some way. Sometimes it is a blinking LED. Sometimes displaying a message on the display. Sometimes it is a requirement to press a button. And another time you turn the knob of the potentiometer. Another way to communicate with the user is the encoder.
What is an encoder?
The external appearance is similar to the potentiometer. And it is very easy to confuse him. However, his principle of operation is completely different. So how does an encoder work? In a very simplified way, this picture shows:
In the figure, we see only a piece of the encoder. We see two circles (A and B) consisting of holes and gaps between them. We also see a window. Most often through the window there is a detector consisting of a LED diode and a photoelement (photodiode, photoresistor or phototransistor) detecting holes and gaps between them.
Now let’s look at how the waveforms look on the detector:
We rotate the encoder to the right. First, the signal appears on the detector A(1), and then on the detector B(2).
We rotate the encoder to the left. First, the signal appears on the detector B(4), and then on the detector A(3).
Thanks to these two detectors A and B, and the displacement of openings on the disk in relation to each other, we are able to check in which direction the user turns the encoder.
And how does it look in practice?
I ordered the encoder module on one of the Chinese auction portals. It cost me $ 0.49 then.
Unfortunately, the seller did not provide the documentation along with the module. That’s why I drew the connection diagram myself:
Resistors R1 has a value of 10 kΩ , R2 = 10 kΩ , R3 = 10 kΩ. The CLK and DT signals are signals informing about the encoder rotation. The SW signal indicates when the encoder is pressed.
Oscilloscope
I powered the encoder with + 3.3 V DC. Then I connected 1 channel of the oscilloscope to the CLK connector and connected 2 channel of the oscilloscope to the DT connector.
Here are the waveforms I observed:
When turning the encoder left, we first see the signal on channel 1 (CLK) and then after some time on channel 2 (DT). When the encoder rotates clockwise, the signal first appears on channel 2 (DT) and then on channel 1 (CLK). The difference in time between the CLK signal and DT is dependent on the rotation speed of the encoder axis.
How to connect an encoder with a microcontroller?
In my case, I connected the encoder module to the ST NUCLEO-L053R8 PCB.
Encoder module | ST NUCLEO-L053R8 |
+ | 3V3 |
– | GND |
SW | PC12 |
CLK | PC10 |
DT | PC11 |
The program for handling the detection of the encoder’s rotation is best based on interrupt handling from digital inputs. In my case the software written in Atollic looked as follows:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_PIN)
{
if(GPIO_PIN==GPIO_PIN_10)
{
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_11)==0)
{
*ptr_Rotate=Right;
}
}
if(GPIO_PIN==GPIO_PIN_11)
{
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_10)==0)
{
*ptr_Rotate=Left;
}
}
if(GPIO_PIN==GPIO_PIN_12)
{
SW_Line=1;
}
}
The software skeleton was generated in CubeMX. In my case, interrupts from PC10, PC11 and PC12 are handled by the same interrupt.
Let’s assume we’re turning the encoder to the right. An interrupt from the PC11 line will be generated first. The condition in if in line 11 will be executed. Next, the microcontroller will go to the line 13 of the code, but this if will not be met. The microcontroller will go to line 18 and then exit the interrupt service. Nothing in this case happened in the code.
Then an interrupt will appear on the PC10 line. The interrupt service will again be reported. Now the microcontroller will go to line 03. The if condition will be execute. The microcontroller will go to line 05. The if condition will be met because the PC11 line is still in the low state. Then the microcontroller will go to line 07, where the indicator pointing to the variable will change its value to “Right”. And on this interrupt service will be terminated.
The analogous situation will be when the encoder is turned to the left.
Summary
As you can see, the principle of the encoder operation is quite simple. His program support is also not complicated. Therefore, the encoder can be used in many projects.