Main Content

servo

Connection to servo 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 servo object represents a connection to a servo motor on an Adafruit® Motor Shield V2 connected to Arduino® hardware. Attach a motor shield to the Arduino hardware, and connect a servo motor to the appropriate pins on the motor shield. Create a motor shield object using the addon function, and create a servo object using the servo function. You can control your servo motor in MATLAB® using the object functions.

Note

This object is different from servo, which is for a servo motor directly connected to pins on the Arduino hardware.

Creation

Description

s = servo(shield,motornum) creates a servo motor connection with the specified motor number on an Adafruit Motor Shield.

example

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

Input Arguments

expand all

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

Example: s = servo(shield,1) creates a connection to a servo motor on the specified adafruit motor shield object shield.

Servo motor number where the motor is connected on the Adafruit Motor Shield, specified as a number. Valid values are 1 and 2. Motor number 1 uses digital pin 10 and motor number 2 uses digital pin 9.

Example: s = servo(shield,1) creates a connection to a servo motor on digital pin 10 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: s = servo(shield,1,'MaxPulseDuration',2e-3,'MinPulseDuration',1e-3);

Minimum pulse duration, specified as the comma-separated pair consisting of 'MinPulseDuration' and a number that represents the duration in seconds. Specified value must be equal to the width of the pulse the motor needs to be at the minimum angle. Refer to your device data sheet for valid values.

Example: s = servo(shield,1,'MaxPulseDuration',2e-3,'MinPulseDuration',1e-3); sets the minimum pulse duration to 1 millisecond.

Maximum pulse duration, specified as the comma-separated pair consisting of 'MaxPulseDuration' and a number that represents the duration in seconds. Specified value must be equal to width of the pulse that the motor needs to be at the maximum angle. Refer to your device data sheet for valid values.

Example: s = servo(shield,1,'MaxPulseDuration',2.4e-3) sets the maximum pulse duration to 2.4 milliseconds.

Properties

expand all

This property is read-only.

Servo motor number where the motor is connected on the Adafruit Motor Shield, specified as a number. Valid values are 1 and 2. Motor number 1 uses digital pin 10 and motor number 2 uses digital pin 9.

Example:

>> s.MotorNumber

ans =

     1

Data Types: double

This property is read-only.

Digital pin number on the Arduino board that the servo motor is connected to, specified as a character vector of the form 'Dx' where x is the Arduino pin number.

Example:

>> s.Pin

ans =

    'D10'

Data Types: char

This property is read-only.

Minimum pulse duration, specified as a number that represents the duration in seconds. Specified value is equal to the width of the pulse that the motor needs to be at the minimum angle.

Example:

>> s.MinPulseDuration

ans =

   5.4400e-04

Data Types: double

Maximum pulse duration, specified as a number that represents the duration in seconds. Specified value is equal to the width of the pulse that the motor needs to be at the maximum angle.

Example:

>> s.MaxPulseDuration

ans =

    0.0024

Data Types: double

Object Functions

Use these object functions to control your servo motor.

readPositionRead position from servo motor connected to Adafruit Motor Shield
writePositionWrite position to servo motor connected to Adafruit Motor Shield

Examples

collapse all

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

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

Create a servo motor connection to motor number 1 on the shield.

s = servo(shield,1);

Version History

Introduced in R2014b