Main Content

rotaryEncoder

Connection to quadrature encoder on Arduino or ESP32 hardware

Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.

Description

A rotaryEncoder object represents a connection to a quadrature encoder on Arduino® or ESP32 hardware. Attach a quadrature encoder to the appropriate pins on the Arduino or ESP32 hardware. Create a rotaryEncoder object using the rotaryEncoder function. You can read from the quadrature encoder in MATLAB® using object functions.

Creation

Description

encoder = rotaryEncoder(a,chA,chB) creates a connection to the rotary encoder using the arduino object a. chA and chB are the Arduino or ESP32 interrupt pins connected to the output of channel A and channel B of the encoder.

example

encoder = rotaryEncoder(a,chA,chB,ppr) also specifies the pulses per revolution.

example

Input Arguments

expand all

Arduino or ESP32 hardware connection created using arduino, specified as an arduino object.

Example: encoder = rotaryEncoder(a,'D2','D3') creates a connection to the rotary encoder on the specified arduino object a.

Arduino or ESP32 interrupt pin connected to channel A output of encoder, specified as a character vector of the form 'Dx' or 'Ax' where x is the Arduino pin number. For example, 'D2'.

Example: encoder = rotaryEncoder(a,'D2','D3') creates a connection to a rotary encoder with channel A output connected to digital pin 2 on the Arduino.

Data Types: char

Arduino or ESP32 interrupt pin connected to channel B output of encoder, specified as a character vector of the form 'Dx' or 'Ax' where x is the Arduino pin number. For example, 'D3'.

Example: encoder = rotaryEncoder(a,'D2','D3') creates a connection to a rotary encoder with channel B output connected to digital pin 3 on the Arduino.

Data Types: char

Pulses per revolution of the encoder, specified as a number.

Example: encoder = rotaryEncoder(a,'D2','D3',180) creates a connection to a rotary encoder and sets the pulses per revolution to 180.

Properties

expand all

This property is read-only.

Arduino or ESP32 interrupt pin connected to channel A output of encoder, specified as a character vector of the form 'Dx' or 'Ax' where x is the Arduino pin number. For example, 'D2'.

Example:

>> encoder.ChannelA

ans =

    'D2'

Data Types: char

This property is read-only.

Arduino interrupt pin connected to channel A output of encoder, specified as a character vector of the form 'Dx' or 'Ax' where x is the Arduino pin number. For example, 'D3'.

Example:

>> encoder.ChannelB

ans =

    'D3'

Data Types: char

This property is read-only.

Pulses per revolution of the encoder, specified as a number.

Example:

>> encoder.PulsesPerRevolution

ans =

     180

Data Types: double

Object Functions

Use object functions to read from your quadrature encoder.

resetCountSet count value to zero or user-specified value
readCountRead current count value from encoder with X4 decoding
readSpeedRead current rotational speed

Examples

collapse all

Connect to a quadrature encoder using your Arduino board.

Create an Arduino object with the 'rotaryEncoder' library. In this example, an Arduino Uno is connected to port 4. The port and board names depend on your configuration.

a = arduino('COM4','Uno','Libraries','rotaryEncoder');

Connect to the encoder that is connected to your Arduino board.

encoder = rotaryEncoder(a,'D2','D3')
encoder = 
  rotaryEncoder with properties:

           ChannelA: 'D2'
           ChannelB: 'D3'
PulsesPerRevolution:  []

Connect to a quadrature encoder, and specify the pulses per revolution.

Create an Arduino object with the 'rotaryEncoder' library. In this example, an Arduino Uno is connected to port 4. The port and board names depend on your configuration.

a = arduino('COM4','Uno','Libraries','rotaryEncoder');

Connect to the encoder that is connected to your Arduino board.

encoder = rotaryEncoder(a,'D2','D3',180)
encoder = 
  rotaryEncoder with properties:

           ChannelA: 'D2'
           ChannelB: 'D3'
PulsesPerRevolution: 180

Version History

Introduced in R2017a