Main Content

dcmotor

Connection to DC motor on Adafruit Motor Shield V2 for Arduino

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

Description

A dcmotor object represents a connection to a DC motor on an Adafruit® Motor Shield V2 connected to Arduino® hardware. Attach a motor shield to the Arduino hardware, and connect a DC motor to the appropriate pins on the motor shield. Create a motor shield object using the addon function, and create a dcmotor object using the dcmotor function. You can control your DC motor in MATLAB® using the object functions.

Creation

Description

dcm = dcmotor(shield,motornum) creates a DC motor connection with the specified motor number on an Adafruit Motor Shield connected to an Arduino board.

example

dcm = dcmotor(shield,motornum,Name,Value) adds additional options specified by one or more Name,Value pair arguments.

example

Input Arguments

expand all

Adafruit Motor Shield connection on Arduino hardware, specified as an adafruit motor shield object created using the addon function.

Example: dcm = dcmotor(shield,1) creates a connection to a DC motor on the specified adafruit motor shield object shield.

Port number on the shield that the motor is connected to, specified as a number. Valid port values are 1, 2, 3, and 4.

Example: dcm = dcmotor(shield,1) creates a connection to a DC motor at port 1 on the motor shield.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: dcm = dcmotor(shield,1,'Speed',0.2)

DC motor speed, specified as the comma-separated pair consisting of 'speed' and a number between —1 and 1.

Example: dcm = dcmotor(shield,1,'Speed',0.2); creates a DC motor connection with the DC motor speed set to 0.2.

Properties

expand all

DC motor speed, specified as a number between —1 and 1. You can specify motor speed when you create the dcmotor object or after you create the dcmotor object.

Example:

>> dcm.Speed = 0.2

dcm = 

  dcmotorv2 with properties:

    MotorNumber: 1 (M1)
          Speed: 0.20           
      IsRunning: 0              

Data Types: double

This property is read-only.

DC motor port number on the shield that the DC motor is connected to, specified as a number during the dcmotor object creation. Valid port values are 1, 2, 3, and 4.

Example:

>> dcm.MotorNumber

ans =

     1

Data Types: double

This property is read-only.

DC motor state, indicated as a boolean value 0 or 1. IsRunning is 0 if the motor has not started and 1 otherwise.

Example:

>> dcm.IsRunning

ans =

  logical

   0

Data Types: logical

Object Functions

Use these object functions to control your DC motor.

startStart DC motor connected to Adafruit Motor Shield
stopStop DC motor connected to Adafruit Motor Shield

Examples

collapse all

Create a DC motor connection to an Adafruit Motor Shield attached to Arduino hardware.

a = arduino('COM4','Uno','Libraries','Adafruit/MotorShieldV2');
dev = addon(a,'Adafruit/MotorShieldV2');

Create a DC motor connection at port 1.

dcm = dcmotor(dev,1)
dcm = 
  dcmotorv2 with properties:

    MotorNumber: 1 (M1)
          Speed: 0.00           
      IsRunning: 0              

Create a DC motor connection to an Adafruit Motor Shield attached to Arduino hardware.

a = arduino('COM4','Uno','Libraries','Adafruit/MotorShieldV2');
dev = addon(a,'Adafruit/MotorShieldV2');

Create a DC motor connection at port 1 on the shield, and set the speed to 0.2.

dcm = dcmotor(dev,1,'Speed',0.2)
dcm = 
  dcmotorv2 with properties:

    MotorNumber: 1 (M1)
          Speed: 0.20           
      IsRunning: 0              

Version History

Introduced in R2014b