element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog Control Servo Motors using an Arduino and Simulink
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: anujaapte
  • Date Created: 31 Jul 2014 1:34 PM Date Created
  • Views 5021 views
  • Likes 1 like
  • Comments 1 comment
  • arduino_tutorials
Related
Recommended

Control Servo Motors using an Arduino and Simulink

anujaapte
anujaapte
31 Jul 2014

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.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

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:

arduinomega2560_servocontrol_connections1.png

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.

image

  • 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


image

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

image

  • 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

image

  • 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.

image

  • 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

 

image

  • 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.

     image

 

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.

  • Sign in to reply

Top Comments

  • Robert Peter Oakes
    Robert Peter Oakes over 11 years ago +1
    nice write up, very informative, thanks
  • Robert Peter Oakes
    Robert Peter Oakes over 11 years ago

    nice write up, very informative, thanks

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube