Main Content

rotaryEncoder

Connection to rotary encoder on Arduino MKR Motor Carrier or Nano Motor Carrier

Since R2020a

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

Description

This object represents a connection to the rotary encoder with the specified channel on the MKR Motor Carrier or Nano Motor Carrier. You can read from the rotary encoder in using Object Functions.

Creation

Description

encoder = rotaryEncoder(mcObj,channelNumber) creates a connection to the rotary encoder at the specified channel on the MKR Motor carrier or Nano Motor Carrier.

example

encoder = rotaryEncoder(mcObj,channelNumber,ppr) creates a connection to the rotary encoder at the specified channel with the specified pulses per revolution on the MKR Motor Carrier or Nano Motor Carrier.

example

Input Arguments

expand all

Connection to the MKR Motor Carrier or Nano Motor Carrier, specified as an object.

Example: encoder = rotaryEncoder(mcObj,1) creates a rotary encoder connection on channel 1.

Channel number on the MKR Motor Carrier or Nano Motor Carrier to which the encoder is connected, specified as a scalar.

Example: encoder = rotaryEncoder(mcObj,1) creates a connection to a rotary encoder at channel 1.

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

Example: encoder = rotaryEncoder(mcObj,1,4) creates a connection to a rotary encoder and sets the pulses per revolution to 4.

Properties

expand all

This property is read-only.

Channel output of encoder, specified as a scalar.

>> encoder.Channel
ans =
    1

Data Types: double

This property is read-only.

Encoding scheme of the encoder.

Example:

>> encoder.Encoding
ans =
    'X4'

Data Types: char

This property is read-only.

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

Example:

>> encoder.PulsesPerRevolution
ans =
     4

Data Types: double

Object Functions

Use object functions to with your rotary encoder.

readCountRead current count value from encoder connected to MKR Motor Carrier or Nano Motor Carrier
readSpeedRead current rotational speed of motor from rotary encoder
resetCountSet encoder count value to zero or specified value

Examples

collapse all

Create an arduino object with the 'MotorCarrier' library.

arduinoObj = arduino('COM7','Nano33IoT','Libraries','MotorCarrier')
arduinoObj = 
  arduino with properties:

                  Port: 'COM7'
                 Board: 'Nano33IoT'
         AvailablePins: {'D0-D13', 'A0-A7'}
  AvailableDigitalPins: {'D0-D13', 'A0-A7'}
      AvailablePWMPins: {'D2-D6', 'D9-D12', 'A2-A3', 'A5'}
   AvailableAnalogPins: {'A0-A3', 'A6-A7'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'MotorCarrier'}
Show all properties

Create a connection to the Nano Motor Carrier.

mcObj = motorCarrier(arduinoObj)
mcObj = 
  MotorCarrier with properties:

          SCLPin: 'A5'
          SDAPin: 'A4'
      I2CAddress: 102 ('0x66')

Create a connection to the rotary encoder on channel 1.

encoder = rotaryEncoder(mcObj,1)
encoder = 
  RotaryEncoder with properties:

                Channel: 1
    PulsesPerRevolution: 3
               Encoding: 'X4'

Create an arduino object with the 'MotorCarrier' library.

arduinoObj = arduino('COM7','Nano33IoT','Libraries','MotorCarrier')
arduinoObj = 
  arduino with properties:

                  Port: 'COM7'
                 Board: 'Nano33IoT'
         AvailablePins: {'D0-D13', 'A0-A7'}
  AvailableDigitalPins: {'D0-D13', 'A0-A7'}
      AvailablePWMPins: {'D2-D6', 'D9-D12', 'A2-A3', 'A5'}
   AvailableAnalogPins: {'A0-A3', 'A6-A7'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'MotorCarrier'}
Show all properties

Create a connection to the Nano Motor Carrier.

mcObj = motorCarrier(arduinoObj)
mcObj = 
  MotorCarrier with properties:

          SCLPin: 'A5'
          SDAPin: 'A4'
      I2CAddress: 102 ('0x66')

Create a connection to the rotary encoder on channel 2, and specify the pulses per revolution.

encoder = rotaryEncoder(mcObj,2,4)
encoder = 
  RotaryEncoder with properties:

                Channel: 2
    PulsesPerRevolution: 4
               Encoding: 'X4'

Version History

Introduced in R2020a