Main Content

Encoder

Measure incremental position and direction of rotating motor

  • Library:
  • Simulink Support Package for Arduino Hardware / Sensors

  • Arduino Encoder Sensor Library icon

Description

The Encoder block outputs the tick count from a quadrature encoder on a rotating motor connected to an Arduino® board. Every increment in the tick count of the encoder indicates that the motor is rotating clockwise. Every decrement in the tick count of the encoder indicates that the motor is rotating counterclockwise. The total tick count represents the incremental position of the rotating motor.

You can reset the encoder readings from the block using the Reset mode parameter in the block parameters dialog box.

When simulating without hardware, this block outputs zeros. For more information, see Block Produces Zeros or Does Nothing in Simulation.

Note

The Encoder block does not support the connected IO workflow on the Arduino Nano 33 BLE Sense board currently.

Ports

Input

expand all

Use this input port to reset the Encoder block tick counter.

When the value of the signal at the input port is 0, the block outputs the total tick count it reads from the encoder. In this case, the block output is the same as in the No reset mode. When the value of the signal at the input port is a nonzero value, the block outputs the number of ticks it reads from the encoder at the at the sample time you set in the Sample time parameter. In this case, the block output is the same as in the Reset at each Sample time mode.

Dependencies

To enable this port, set Reset mode to Reset by external signal.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean

Output

expand all

The block outputs the relative change in the position of the rotating motor in ticks. You can determine the direction of rotation based on the previous and current tick counts and the mode you select in the Reset mode parameter. For example, in the No reset mode, with a previous tick count of 30 and current tick count of 20, the motor is moving in the counterclockwise direction.

Data Types: int16

Parameters

expand all

Select pin on the Arduino board to which you have connected output A of the encoder.

Select pin on the Arduino board to which you have connected output B of the encoder.

The block supports three reset modes.

  • No reset — The block outputs the total tick count it reads from the encoder.

  • Reset at each Sample time — The block outputs the number of ticks it reads from the encoder at the sample time you set in the Sample time parameter.

  • Reset by external signal — When you set the parameter to this mode, an input port becomes available. In this mode, when the value of the signal at the input port is 0, the block output is the same as in the No reset mode. When the value of the signal at the input port is a nonzero value, the block output is the same as in the Reset at each sample time mode.

Assume that the encoder wheel that you have connected to your Arduino hardware has 90 slots, which means that the encoder tick count is 1 for every 4 degrees of rotation. Assume that the block sample time is 1 second and the tick count at time instant T = 0 is 0. This table shows the tick counts that the block outputs in the three modes.

Arduino reset mode image

  • Between T = 0 and T = 1, assume that the encoder rotates 20 degrees clockwise.

    • No reset — The output is 20/4 = 5

    • Reset at each sample time — Assume that the encoder has rotated 20 degrees at the given sample time, so the output is 20/4 = 5

    • Reset by external signal — Assume that the value of the external signal is 0, so the count is calculated as in the No reset mode, and the output is 20/4 = 5

  • Between T = 1 and T = 2, assume that the encoder rotates 40 degrees clockwise.

    • No reset — In this mode, the block output at any time instant is the sum of the previous count and the current count, so the output is 5+40/4 = 15

    • Reset at each sample time — Assume that the encoder has rotated 40 degrees at the given sample time, so the output is 40/4 = 10

    • Reset by external signal — Assume that the value of the external signal is 0, so the block calculates the count as in the No reset mode, and the output is 5+40/4 = 15

  • Between T = 3 and T = 4, assume that the encoder rotates 20 degrees counterclockwise.

    • No reset — In this mode, the block output at any time instant is the sum of the previous count and the current count, so the output is 30–20/4 = 25

    • Reset at each sample time — Assume that the encoder has rotated 20 degree counterclockwise at the given sample time, so the output is – 20/4 = – 5

    • Reset by external signal — Assume that the value of the external signal is a nonzero value, so the block calculates the count as in the Reset at each sample time mode, and the output is – 20/4 = – 5

Enter the time interval in seconds at which the block reads values from the encoder.

Introduced in R2021b