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