Main Content

Rotate a Servo Motor

This example shows how to rotate a servo motor by its maximum angle.

Create an Arduino® object and include the servo library.

a = arduino('COM22', 'Uno', 'Libraries', 'Servo');

Configure a servo object using the PWM pin 9 and set the minimum pulse duration to 1e-3 seconds and the maximum pulse durations to 2e-3 seconds.

s = servo(a, 'D9', 'MinPulseDuration', 1e-3, 'MaxPulseDuration', 2e-3);

Write 1, to turn the motor by the maximum angle.

writePosition(s, 1);

The angle depends on device pulse duration. Refer to your device data sheet for valid pulse duration values.