How to interface the rotary encoder to STM32 (CUBE-MX + ATOLLIC)

If you like it, share it

The Key Rotary Encoder are most used encoder for general purpose MCU applications.
The generic schematic of this type of encoder is show below.

The VCC depend of the producer of the encoder but normally is 5V. Control the relative data sheet.

Below is an image showing the waveforms of the A & B channels of an encoder.

Thanks to the 5V input tolerant, of the GPIOs of the STM32 family, is possible use also the 5V encoder connected directly to STM32 GPIOs.
I tested this encoder at 3,3V on NUCLEO boards and it’s work well.
Of course, it’s work well also at 5V connected directly to the STM32 GPIOs.

For test this encoder we used the NUCLEO-F401RE and we interface the encoder to the TIM1.
See the SetUp below.

Also we use the USART2 to send a messages (PRINTF) to the PC via the emulator USB port.
For more info concerning the USART2 and PRINTF see this tutorial.

The schematic is shown below.

The lines below show the way to read the encoder and how to show it’s value (virtual com via USART2) on the PC.

// Test Encoder
EncVal = TIM1->CNT;
printf("Encoder -> %d \n\r", EncVal);

For test this example do the step below.

Compile the example that is here and program your NUCLEO-F401RE.

Open a terminal emulator (on PC), we suggest to use on PC the TeraTerm configured as shown below.
Choose the right com port.

You must see something like below.

How to get the SW for this project

Please send us an email and ask us the password for: EncoderF401ato
Please specify also your country and your city, this are only for our personal statistics.

Get the SW clicking here, but remember to ask us the password for open it.

This example is for ATOLLIC, to see how to import this project in your ATOLLIC see this tutorial.



  • Use FIREFOX or CHROME for a clear view of the images present in this web site
  • For enlarge the image press: CTRL +
    For reduce the image press: CTRL –