Control Servo Motor using MATLAB

Control Servo Motor using MATLAB

Arduino Servo Motor

The story of Servo Motor is entirely different from that of a DC Motor. A Servo Motor is a type of actuator that provides high precision control of linear or angular position. A typical servo motors consists of four things (or parts): a DC Motor (or AC Motor), a gear unit, a position and speed sensing device and a control unit.

These Motors are used in applications where very high precision motion is required like assembly robots, computer numeric controls etc.

In this project, we are going to control the position of a Servo Motor using Arduino UNO board.

Controlling Servo Motor:

All motors have three wires coming out of them. Out of which two will be used for Supply (positive and negative) and one will be used for the signal that is to be sent from the MCU.

Servo motor is controlled by PWM (Pulse with Modulation) which is provided by the control wires. There is a minimum pulse, a maximum pulse and a repetition rate. Servo motor can turn 90 degree from either direction form its neutral position. The servo motor expects to see a pulse every 20 milliseconds (ms) and the length of the pulse will determine how far the motor turns. For example, a 1.5ms pulse will make the motor turn to the 90° position, such as if pulse is shorter than 1.5ms shaft moves to 0° and if it is longer than 1.5ms than it will turn the servo to 180°.

Servo motor works on PWM (Pulse width modulation) principle, means its angle of rotation is controlled by the duration of applied pulse to its Control PIN. Basically servo motor is made up of DC motor which is controlled by a variable resistor (potentiometer) and some gears. High speed force of DC motor is converted into torque by Gears. We know that WORK= FORCE X DISTANCE, in DC motor Force is less and distance (speed) is high and in Servo, force is High and distance is less. Potentiometer is connected to the output shaft of the Servo, to calculate the angle and stop the DC motor on required angle.

Connections

Connect a servo motor to Arduino hardware,

  1. Connect the power wire (usually red) to the 5V pin.
  2. Connect the ground wire (usually black) to the ground pin.
  3. Connect the signal wire (usually orange) to digital pin 4.

Create servo object and calibrate the motor

Create an arduino object and include the Servo library.

 a = arduino();

Or, you can explicitly specify it in the Libraries Name-Value pair at creation of arduino object.

a = arduino('com22', 'uno', 'Libraries', 'Servo');





Create a Servo object.

 s = servo(a, 'D4')

s =

Servo with properties:

Pins: D4
MinPulseDuration: 5.44e-04 (s)
MaxPulseDuration: 2.40e-03 (s)

Check your servo motor’s data sheet pulse width range values to calibrate the motor to rotate in expected range. This example uses 700*10^6 and 2300*10^-6 for the motor to move from 0 to 180 degrees.

 clear s;
   s = servo(a, 'D4', 'MinPulseDuration', 700*10^-6, 'MaxPulseDuration', 2300*10^-6)

s =

Servo with properties:

Pins: D4
MinPulseDuration: 7.00e-04 (s)
MaxPulseDuration: 2.30e-03 (s)

Write and read Servo position

Change the shaft position of the servo motor from 0(minimum) to 1(maximum) with 0.2, e.g 36 degrees, increment. Display the current position each time the position changes.

   for angle = 0:0.2:1
       writePosition(s, angle);
       current_pos = readPosition(s);
       current_pos = current_pos*180;
       fprintf('Current motor position is %d degrees\n', current_pos);
       pause(2);
   end

Current motor position is 0 degrees
Current motor position is 36 degrees
Current motor position is 72 degrees
Current motor position is 108 degrees
Current motor position is 144 degrees
Current motor position is 180 degrees

Clean up

Once the connection is no longer needed, clear the associate object.

 clear s a

We hope this tutorial helped you in learning how to Control Servo Motor using MATLAB support package.

If you liked this article, then please subscribe to our YouTube Channel for MATLAB & Simulink’s Video Tutorials. You can also find us on Twitter and Facebook .


Senior undergraduate student from Shri Mata Vaishno Devi Unversity, Katra.

Leave a Reply