servo

Connection to servo motor on Arduino hardware

Description

A servo object represents a connection to a servo motor on Arduino® hardware. Attach a servo motor to the appropriate pins on the Arduino hardware. Create a servo object using the servo function. You can read from and write to the servo motor in MATLAB® using object functions.

Note

This object is different from servo, which is for a servo motor connected to an Adafruit® Motor Shield V2 on the Arduino hardware.

Creation

Syntax

s = servo(a,pin)
s = servo(a,pin,Name,Value)

Description

example

s = servo(a,pin) creates a connection to the servo motor on the specified pin of the Arduino hardware a.

example

s = servo(a,pin,Name,Value) creates a connection to the servo motor with additional options specified by one or more Name,Value pair arguments.

Input Arguments

expand all

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

Example: s = servo(a,'D4') creates a connection to the servo on the specified arduino object a.

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

Example: s = servo(a,'D4') creates a connection to the servo on digital pin 4 on the Arduino hardware.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: s = servo(a,'D4','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(a,'D4','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 the motor needs to be at the maximum angle. Refer to your device data sheet for valid values.

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

Properties

expand all

This property is read-only.

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

Example:

>> s.Pin

ans =

    'D4'

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 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 the motor needs to be at the maximum angle.

Example:

>> s.MaxPulseDuration

ans =

    0.0024

Data Types: double

Object Functions

Use object functions to read from and write to your servo motor.

readPositionRead servo motor position
writePositionWrite position of servo motor

Examples

collapse all

Create a servo object using pin 4.

Create an arduino object, and attach the servo object to pin 4.

a = arduino('COM4','Uno','Libraries','Servo');
s = servo(a,'D4')
s = 
  Servo with properties:

                 Pin: 'D4'
    MinPulseDuration: 5.44e-04 (seconds)
    MaxPulseDuration: 2.40e-03 (seconds)

Set the minimum duration to 7e-4 and maximum to 2.3e-3 seconds.

Create an arduino object, attach the servo object to digital pin 4, and set the pulse durations.

a = arduino('COM4','Uno','Libraries','Servo');
Updating server code on board Uno (COM4). This may take a few minutes.
s = servo(a,'D4','MinPulseDuration',7.00e-4,'MaxPulseDuration',2.3e-3)
s = 
  Servo with properties:

                 Pin: 'D4'
    MinPulseDuration: 7.00e-04 (seconds)
    MaxPulseDuration: 2.30e-03 (seconds)

Copyright 2014 The MathWorks, Inc.,

Introduced in R2014b