Quadrature Decoder Sensor
Overview
This sample reads the value of the counter which has been configured in quadrature decoder mode.
It requires:
an external mechanical encoder
pin to be properly configured in the device tree
Building and Running
In order to run this sample you need to:
enable the quadrature decoder device in your board’s DT file or board overlay
add a new alias property named
qdec0and make it point to the decoder device you just enabled
For example, here’s how the overlay file of an STM32F401 board looks like when using decoder from TIM3 through pins PA6 and PA7:
/ {
aliases {
qdec0 = &qdec;
};
};
&timers3 {
status = "okay";
qdec: qdec {
status = "okay";
pinctrl-0 = <&tim3_ch1_pa6 &tim3_ch2_pa7>;
pinctrl-names = "default";
st,input-polarity-inverted;
st,input-filter-level = <FDIV32_N8>;
st,counts-per-revolution = <16>;
};
};
Sample Output
Once the MCU is started it prints the counter value every second on the console
Quadrature decoder sensor test
Position = 0 degrees
Position = 15 degrees
Position = 30 degrees
...
If the driver supports getting speed (RPM) and revolution count channels, these data will be displayed on the console.
When CONFIG_EQDC_MCUX_TRIGGER=y (e.g. on frdm_mcxa153), the sample also
registers the SENSOR_TRIG_OVERFLOW trigger on the revolution channel and
prints the trigger count each cycle:
Quadrature decoder sensor test
Registered SENSOR_TRIG_OVERFLOW on SENSOR_CHAN_ENCODER_REVOLUTIONS
Position = 0 degrees
Revolutions = 0
Triggers = 0
Position = 180 degrees
Revolutions = 1
Triggers = 1
...
Of course the read value changes once the user manually rotates the mechanical encoder.
Note
The reported increment/decrement can be larger/smaller than the one shown
in the above example. This depends on the mechanical encoder being used and
st,counts-per-revolution value.