Configure an incremental encoder

Use the incremental encoder model to configure a quadrature encoder.

Configuring an incremental encoder requires configuring two pins on the board to which the encoder is wired. These two pins provide the phase outputs used to measure the speed and direction of rotation in relation to a given reference point.

On the COMPONENTS subtab, navigate to the Create Component menu. Enter a name for your encoder, select the type encoder, and select the incremental model.

Creation of an incremental encoder in the Viam app config builder.

Fill in the attributes for your encoder:

Configuration of an incremental encoder in the Viam app config builder.
{
    "name": "<encoder_name>",
    "type": "encoder",
    "model" : "incremental",
    "attributes": {
      "board": "<board_name>",
      "pins": {
        "a": <string>,
        "b": <string>
      }
    }
}

The following attributes are available for incremental encoders:

NameInclusionDescription
boardRequiredThe name of the board to which the encoder is wired.
pinsRequiredA struct holding the names of the pins wired to the encoder:
  • a: Pin number of one of the pins to which the encoder is wired. Use pin number, not GPIO number.
  • b: Required for two phase encoder. Pin number for the second board pin to which the encoder is wired.

Viam also supports a model of encoder called "single" which requires only one pin (i).

Example Config

The following example shows the configuration of a board and an encoder:

{
  "components": [
    {
      "name": "local",
      "type": "board",
      "model": "pi",
      "attributes": {}
    },
    {
      "name": "myEncoder",
      "type": "encoder",
      "model": "incremental",
      "attributes": {
        "board": "local",
        "pins": {
          "a": "13",
          "b": "11"
        }
      }
    }
  ]
}