---
title: "Quadrature Encoder Readout"
description: "Open a quadrature encoder on ALPE1MENC0 and poll the accumulated position. Demonstrates the encoder mode of `. ALPE1MENC0..ALPE1MENC3` are reserved by the E1M spec for the four portable encoder slots."
source: https://docs.alplab.ai/sdk/examples/qenc-readout
---

# `qenc-readout`

Open a quadrature encoder on `ALP_E1M_ENC0` and poll the accumulated position. Demonstrates the encoder mode of [`<alp/counter.h>`](https://docs.alplab.ai/sdk/api/counter). `ALP_E1M_ENC0`..`ALP_E1M_ENC3` are reserved by the E1M spec for the four portable encoder slots.

Source: [`examples/peripheral-io/qenc-readout/`](https://github.com/alplabai/alp-sdk/tree/main/examples/peripheral-io/qenc-readout).

## `board.yaml`

```yaml
som:
  sku: E1M-AEN801

preset: e1m-evk
supported_boards:
  - e1m-evk
  - e1m-x-evk

pins:
  - { e1m: E1M_ENC0, macro: EVK_ENC_ROTARY, doc: "PEC11R-4215K-S0024 rotary encoder (ENC0_X = A phase, ENC0_Y = B phase, 24 PPR); this demo reads the accumulated position" }
cores:
  m55_hp:
    app: ./src
    peripherals:
      - sensor

diagnostics:
  log_level: info
```

## Source (abbreviated)

```c
#include <alp/counter.h>
#include <alp/e1m_pinout.h>

int main(void) {
    alp_qenc_t *enc = alp_qenc_open(&(alp_qenc_config_t){
        .encoder_id     = ALP_E1M_ENC0,      /* 0..ALP_E1M_ENC_COUNT-1 (= 4) */
        .pulses_per_rev = 24,                /* PEC11R-4215K-S0024 on the 2626-R2 E1M EVK */
    });
    if (enc == NULL) {
        printk("[qenc] open failed: err=%d\n", (int)alp_last_error());
        return -1;
    }

    for (int i = 0; i < 20; i++) {
        int32_t position;
        alp_qenc_get_position(enc, &position);
        printk("[qenc] position=%d\n", position);
        k_msleep(250);
    }
    alp_qenc_close(enc);
    return 0;
}
```

## AEN family (E1M EVK)

The E8 has **no hardware quadrature decoder** — measured, its UTIMER "QEC" channel is an undirected edge counter (see [E1M-AEN](https://docs.alplab.ai/e1m/e1m-aen#e1m-aen801-alif-e8-bench-status)). So as of alp-sdk v0.17.0 the example's AEN board overlays bind `ALP_E1M_ENC0` to the EVK's rotary encoder through Zephyr's **`gpio-qdec`** driver, a software Gray-code decoder, by pointing the `alp-qenc0` alias at it. Overlays ship for the AEN801 and AEN803, M55-HE and M55-HP:

```bash
cd alp-sdk
python3 scripts/gen_example_alp_conf.py examples/peripheral-io/qenc-readout
west build -b alp_e1m_aen801_m55_he/ae822fa0e5597ls0/rtss_he \
    examples/peripheral-io/qenc-readout -- -DEXTRA_CONF_FILE=generated/alp.conf \
    -DEXTRA_ZEPHYR_MODULES=$(pwd) -DCONFIG_COMPILER_OPT=\"-DALP_BOARD_E1M_EVK\"
```

Turn the knob while it runs; at rest the position stays 0.

:::caution[The rotary encoder is on the 2626-R2 E1M EVK only]
The rotary encoder (designator E1, PEC11R-4215K-S0024) is fitted on the earlier **2626-R2** E1M EVK, which the alp-sdk v0.17.0 board preset follows. The current **2641-R3** E1M EVK (UG-E1M-001) has no rotary encoder: it has only the 8-pin header `J18` with `ENC1`..`ENC3`, and no pull-ups on those lines, so an open-collector encoder needs its own pull-ups to `+VARD`. The PEC12R-4222F is the E1M-X EVK part.
:::

## V2N family

On E1M-X V2N the four quadrature encoders are owned by the on-module GD32G553 supervisor. The host driver routes `alp_qenc_*` through [`<alp/chips/gd32g553.h>`](https://docs.alplab.ai/sdk/chips); the example works unchanged because the wrapper hides the transport.

## See also

- [`<alp/counter.h>` reference](https://docs.alplab.ai/sdk/api/counter)
- [Examples overview](https://docs.alplab.ai/sdk/examples/overview)
