Main Content

servo

Connection to servo motor on Arduino or ESP32 hardware

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 Arduino® or ESP32 hardware. Attach a servo motor to the appropriate pins on the Arduino or ESP32 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

Description

s = servo(a,pin) creates a connection to the servo motor on the specified pin of the Arduino or ESP32 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.

example

Input Arguments

expand all

Arduino or ESP32 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 or ESP32 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 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(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 or ESP32 board that the servo motor is connected to, specified as a character vector of the form 'Dx' or 'Ax' where x is the Arduino or ESP32 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)

Version History

Introduced in R2014b