Main Content

dcmotor

Connection to DC Motor 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 a DC Motor with the specified motor number on an MKR Motor Carrier or Nano Motor Carrier. You can control the DC motor using Object Functions.

Creation

Description

example

dcmObj = dcmotor(mcObj,motornumber) creates a connection to the DC motor at the specified motor number on the MKR Motor Carrier or Nano Motor Carrier.

example

dcmObj = dcmotor(mcObj,motornumber,Speed) creates a connection to the DC motor at the specified motor number and specified additional options on the MKR Motor Carrier or Nano Motor Carrier.

Input Arguments

expand all

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

Example: dcmObj = dcmotor(mcObj,M1) creates a DC motor connection at motor number 1 connected to M1.

DC motor number where the motor is connected on an MKR Motor Carrier or Nano Motor Carrier, specified as a character vector.

Normalized DC motor duty cycle specified as a number between-1 and 1. A positive number indicates a forward rotation and a negative number indicates a reverse rotation of the motor shaft.

Example: dcmObj = dcmotor(mcObj,'M1','Speed',0.2); sets the duty cycle to 20% in forward direction of rotation of the motor shaft.

Properties

expand all

This property is read-only.

Motor number on the MKR Motor Carrier or Nano Motor Carrier where the DC motor is connected.

Example:

>> dcmObj.MotorNumber
ans = 
      'M1' 

Data Types: char

DC motor duty cycle specified as a number between-1 and 1. A positive number indicates a forward rotation of the motor shaft and a negative number indicates a reverse rotation of the motor shaft.

Example:

>> dcmObj.Speed
ans = 
      0.2 

Data Types: double

This property is read-only.

DC motor state specified as 0 or 1. Running is 0 if the motor has not started and 1 otherwise. Running is a read-only property.

Example:

>> dcmObj.Running
ans = 
      0 

Data Types: double

Object Functions

Use these object functions to create a connection to the DC Motor.

startStart DC motor connected to MKR Motor Carrier or Nano Motor Carrier
stopStop DC motor connected to MKR Motor Carrier or Nano Motor Carrier

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 DC Motor M3.

dcmObj = dcmotor(mcObj,'M3')
dcmObj = 
  DCMotor with properties:

    MotorNumber: 'M3'
        Running: 0              
          Speed: 0              

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 DC Motor M3 setting the speed to 0.2

dcmObj = dcmotor(mcObj,'M3','Speed',0.2)
dcmObj = 
  DCMotor with properties:

    MotorNumber: 'M3'
        Running: 0              
          Speed: 2.000000e-01   

Version History

Introduced in R2020a