1.. _qdec_sensor: 2 3Quadrature Decoder Sensor 4######################### 5 6Overview 7******** 8 9This sample reads the value of the counter which has been configured in 10quadrature decoder mode. 11 12It requires: 13* an external mechanical encoder 14* pin to be properly configured in the device tree 15 16Building and Running 17******************** 18 19In order to run this sample you need to: 20 21* enable the quadrature decoder device in your board's DT file or board overlay 22* add a new alias property named ``qdec0`` and make it point to the decoder 23 device you just enabled 24 25For example, here's how the overlay file of an STM32F401 board looks like when 26using decoder from TIM3 through pins PA6 and PA7: 27 28.. code-block:: dts 29 30 / { 31 aliases { 32 qdec0 = &qdec; 33 }; 34 }; 35 36 &timers3 { 37 status = "okay"; 38 39 qdec: qdec { 40 status = "okay"; 41 pinctrl-0 = <&tim3_ch1_pa6 &tim3_ch2_pa7>; 42 pinctrl-names = "default"; 43 st,input-polarity-inverted; 44 st,input-filter-level = <FDIV32_N8>; 45 st,counts-per-revolution = <16>; 46 }; 47 }; 48 49Sample Output 50============= 51 52Once the MCU is started it prints the counter value every second on the 53console 54 55.. code-block:: console 56 57 Quadrature decoder sensor test 58 Position = 0 degrees 59 Position = 15 degrees 60 Position = 30 degrees 61 ... 62 63 64Of course the read value changes once the user manually rotates the mechanical 65encoder. 66 67.. note:: 68 69 The reported increment/decrement can be larger/smaller than the one shown 70 in the above example. This depends on the mechanical encoder being used and 71 ``st,counts-per-revolution`` value. 72