Skip navigation

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.

I have purchased an Arduino Mega 2560 R3 and want to connect a 3.5" TFT LCD to it. I am aware that I have to enter the source code but am stuck. There was no code sent with the screen.

I bought it from:  Arduino Mega 2560 R3 R61581 3 5 inch TFT LCD Touhscreen Shield | eBay.

I was directed to this site:   UTouch - Electronics - Henning Karlsen

for the UTFT and UTOUCH codes. I have added them to my library but the UTFT names do not match my screen when I try to run a configuration

What am I doing wrong. I am new to this so you will have to explain it bit by bit please.


I was browsing through YouTube today and found an interesting project video from Mohammad Alshawabkeh & Ahmad Alameer of the University of Jordan Department of Mechatronics Engineering.


They have created a robotic hand powered by an Arduino Mega and the Simulink Support Package for Arduino Hardware. The robotic contraption uses 5 servo motors and 5 flex sensors to identify the position of the fingers. Seems like a really great project. Check out the video below to see the robotic hand in action. If you have any cool projects as well share them with us. More ideas for makers to try at home.


MathWorks organized a robotics competition as a part of the recently held Paris Maker Faire scheduled on June 21st and 22nd. The competition allowed participants to get their hands on a custom built mini Mars rover and play with Simulink and MATLAB to show off their robotics skills and received a raving response from a very large number of teams.


                                                                     The Mars rover

The goal of the competition was simple: Program an autonomous Mars rover to visit a number of sites in the fastest time possible. The sites would be designated using markers on an arena. The robots would be built and provided by MathWorks so the teams could focus on the algorithms - the brainpower to drive the robots, although the robot design was released as well if participants were interested in 3D printing their own prototypes.


                              CAD Model


A month prior to the competition, the participants were provided a functioning simulation model of the robot on github that they could use to prepare for the competition (check out the models to play with them yourself!). The robot designs and list of necessary hardware are also located on github, so anyone can build the rover and program it!. The competition received about 90 registrations, with teams of varied age groups from 15 - 58. The teams had up till June 1st to tune and modify the models to come out with the quickest simulations.


                                                            Simulation model in Simulink

Approximately 45 teams submitted their simulation models, and the best 12 amongst them were invited to the Paris Maker Faire for the final showdown. Not to disappoint the other teams, each team got free stuff including 2 e-tickets for the Maker Faire and an invitation for a Saturday night private party (Losing does not sound so bad after all). Each of the 12 finalists was provided with a webcam equipped Mars rover running on an Arduino DUE and access to the arena. The robots used a Raspberry Pi to process webcam images. The located targets acquired in the images are sent to the Arduino DUE (new support in MATLAB R2014a!) via i2c.



                                                                 The Arena

On competition day, It was a fight up to the last minute as teams battled their opponents, switching between algorithms and fine tuning those parameters. Throughout the day, many teams had the chance to be at the top of the scoring list with the best time. But none of them would stay very long as their opponents would quickly redouble their efforts and find ways to pull ahead. This happened all the way to the end, when within the final minutes of the final slugout, a team named LCMG was able to do a 37.6 s run, besting team 'Rayn on Mars’ 37.9. The roar and applause of the audience was intense. The 12 finalists took home tonnes of goodies and the winners got one of the Mars rover robots!


                                                              The Venue



                                              The tension (or lack of  ) in the air


This however, is not where the story ends. For those interested in running their own competition event, MathWorks would love to help you! All design files and programs are available on github (Last minute changes and bug fixes pending). So reach out let MathWorks help you make it happen! Looking forward to the next competition.


                                                       Mars rovers: The next generation



                                                            3D Printed Trophy




It's the Independence day weekend in the United States and a time for patriotism and fireworks . My colleague, Ye Cheng wanted to show her patriotism in her own "techie" way. Her idea: Light Paintings of the US flag.

Light paintings use point sources of light to sketch shapes which are captured by a camera at a low shutter speed. Ye used 13 RGB LEDs for her paintings.

The color of the LEDs are controlled by an Arduino Mega 2560, programmed using the Simulink Arduino Support Package and a Simulink model shown below.


The model sequentially switches the colors of a first 8 LEDs at appropriate times to give the desired effect. The hardware set up required is minimal and requires setting up the LEDs in a straight line on a breadboard.  Ye plans to attach the circuit to a motor and have a persistence of vision exhibit: A USA flag painted created using 13 LEDs constantly rotating. I am definitely looking forward to getting one of those for myself!

Filter Blog

By date: By tag: