This blog post is a tutorial on how you can control servo motors using an Arduino Mega 2560 board and Simulink. If you are not familiar with programming an Arduino with Simulink, I would recommend that you check out the video linked below first. I had pointed to this video in an earlier blog post which went through the steps I had taken to learn Arduino programming with Simulink.


Now that the basics are out of the way. Let us get to the servo motor control part of this tutorial. This tutorial uses the Simulink Support Package for Arduino which allows you to create and run Simulink models on an Arduino Mega 2560. The support package contains a library of blocks which can be used for interfacing with Arduino sensors, actuators and communication devices. In this tutorial, we will focus on servo motor control. In a standard servo motor, the motor shaft can be precisely set to any angle between 0 degrees and 180 degrees using a data signal. The data signal is generally a pulse width modulated waveform, whose duty cycle or the amount of time the value of the waveform is set to a 1 in a single time period of the wave determines the angle of rotation of the motor shaft. However with Simulink, you do not have to worry about pulse width modulation, which is taken care of by the underlying implementation, and you just need to specify the angle of rotation instead.


For the purpose of the tutorial, you will need the following:

  1. Arduino Mega 2560
  2. USB Cable
  3. Standard Servo Motor
  4. Breadboard wires
  5. Breadboard


As the first step, let us try to control the servo motor using a signal generated from Simulink. To achieve this, we first need to connect the servo motor appropriately to the Arduino. The servo motor will have three wires: power, ground, and signal. Connect the wires as described below:

  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.


The circuit assembly is shown below:


Once the hardware is set up, the next step is to create a Simulink model to control the motor. The steps for that are:

  • Create a blank Simulink model by clicking on 'New' on the MATLAB toolstrip and select Simulink Model.

new model.png

  • In the Simulink window, add the ‘Standard Servo Write’ block which is a part of the Simulink Support Package for Arduino Hardware. The path to the blocks is:
    Simulink Library Browser -> Simulink Support Package for Arduino Hardware -> Common -> Standard Servo Write


  • In the Simulink model, double click on the Standard Servo Write block and change the pin number to 4.


  • Similarly, insert the Repeating Sequence Stair block to the model, and connect the output of the Repeating Sequence Stair block to the input of the Standard Servo Write block.The Repeating Sequence Stair block can be found at the following path:

     Simulink Library Browser -> Simulink -> Sources -> Repeating Sequence Stair


  • Double click on the Repeating Sequence Stair block and set the sample time to 0.01. Change the vector of output values to the following : [1:180 179:-1:1]. The vector of output values is the input to the Standard Servo Write block and provides the angles by which the shaft of the servo motor should rotate. In this case, the shaft will rotate about 180 degrees in steps of 1 degree and then return to the original position.


  • The final model will be the same as the example model named 'arduinomega2560_servocontrol_sweep.slx' which is an example model included with the Simulink Support Package for Arduino



  • Once the model is ready you can download the code onto your Arduino by clicking on the Deploy to Hardware button on the top right corner of the Simulink Window.



This concludes the tutorial on Servo Motor Control using Simulink. However this is just a starting point for more projects with servo motors. The immediate next steps in this case would be to control the shaft position using a potentiometer or even a photocell. In both the cases, the workflow would be to capture the value of the potentiometer or photocell as an input to the Arduino, and on the basis of the value change the servo motor position accordingly.