Configure an Incremental Encoder

Use the incremental encoder model to configure a quadrature encoder.

Configuring an incremental encoder requires specifying the pin numbers of the 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.

To be able to test the encoder as you configure it, connect the encoder to your machine’s computer and power both on. Then, configure the encoder:

Navigate to the CONFIGURE tab of your machine’s page in the Viam app. Click the + icon next to your machine part in the left-hand menu and select Component. Select the encoder type, then select the incremental model. Enter a name or use the suggested name for your encoder and click Create.

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

Fill in and edit the attributes as applicable.

{
  "name": "<your-encoder-name>",
  "model": "incremental",
  "type": "encoder",
  "namespace": "rdk",
  "attributes": {
    "board": "<your-board-name>",
    "pins": {
      "a": "<your-first-pin-number>",
      "b": "<your-second-pin-number>"
    }
  }
}
{
  "components": [
    {
      "name": "local",
      "model": "pi",
      "type": "board",
      "namespace": "rdk",
      "attributes": {}
    },
    {
      "name": "myEncoder",
      "model": "incremental",
      "type": "encoder",
      "namespace": "rdk",
      "attributes": {
        "board": "local",
        "pins": {
          "a": "13",
          "b": "11"
        }
      }
    }
  ]
}

The following attributes are available for incremental encoders:

NameTypeRequired?Description
boardstringRequiredThe name of the board to which the encoder is wired.
pinsobjectRequiredA 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.
  • b: Required for two phase encoder. Pin number for the second board pin to which the encoder is wired.
  • If the encoded motor does not operate as expected, the encoder pins might be configured in reverse, switch the a and b pin definitions in your incremental encoder attributes to reconfigure your encoded motor.

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

Test the encoder

Once your encoder is configured and connected, open the encoders’s TEST panel on the CONFIGURE or CONTROL tabs. The ticks count is displayed. Try moving the encoder (for example, by turning a motor it is attached to) and check whether the count increases as expected.

Encoder test panel.

If the encoder does not appear on the TEST panel, or if you notice unexpected behavior, check your machine’s LOGS tab for errors, and review the configuration.

Troubleshooting

If your encoder is not working as expected, follow these steps:

  1. Check your machine logs on the LOGS tab to check for errors.
  2. Review this encoder model’s documentation to ensure you have configured all required attributes.
  3. Check that all wires are securely attached to the correct pins.
  4. Click on the TEST panel on the CONFIGURE or CONTROL tab and test if you can use the encoder there.

If none of these steps work, reach out to us on the Community Discord and we will be happy to help.

Next steps

For more configuration and usage info, see:

Have questions, or want to meet other people working on robots? Join our Community Discord.

If you notice any issues with the documentation, feel free to file an issue or edit this file.