Servo Control
Servo Control
This example shows how to use Simulink Support Package for Arduino Hardware to control a standard servo
motor.
Supported Hardware:
Arduino Esplora
Arduino Fio
Arduino Leonardo
Arduino Micro
Arduino Mini
Arduino Pro
Arduino Uno
Available versions of this example:
Arduino Mega 2560 board: arduinomega2560_servocontrol_sweep
The provided model is pre-configured for Arduino Mega 2560 and can be run on any of the borad listed in the
"Suppoerted Hardware" section, by changing the "Target Hardware" parameter in the configuration parameters
of the model as described in Task 1 of this example.
On this page
Introduction
Prerequisites
Required Hardware
Task 1 - Configure the Model for Supported Arduino Hardware
Task 2 - Connect the Servo Motor to the Arduino Board
Task 3 - Set the Servo Motor Position via an Internal Source
Task 4 - Control the Servo Motor Position via a Potentiometer
Task 5 - Control the Servo Motor Position via a Photocell
Other Things to Try
Summary
Introduction
Simulink Support Package for Arduino Hardware enables you to create and run Simulink models on Arduino
board. The target includes a library of Simulink blocks for configuring and accessing Arduino sensors,
actuators and communication interfaces.
In this example you will learn how to create Simulink model that controls a standard servo motor. In a standard
servo motor, the shaft position can be precisely set, usually between 0 and 180 degrees. Servo motors are
used in many industrial, military and consumer applications and products.
Prerequisites
We recommend completing Getting Started with Arduino Hardware example.
Required Hardware
To run this example you will need the following hardware:
USB cable
Potentiometer
CdS photocell
10 kOhm resistor
Breadboard wires
6. When the model starts running on the Arduino board, observe the motor shaft position sweeping between 0
and 180 degrees.
Task 4 - Control the Servo Motor Position via a Potentiometer
In this task you will set the position of the servo motor shaft manually, using a potentiometer. Connect the outer
potentiometer terminals to 5V and GND pins on your Arduino board. Connect the middle terminal to analog
input pin 0. As your rotate the potentiometer, its resistance changes. As a result, the voltage at the analog
input pin changes as well. Your task is to set the servo motor angle proportional to this voltage.
3. In the Simulink Library Browser, navigate to Simulink Support Package for Arduino Hardware >
Common.
4. Drag the Analog Input block into the model. Double-click the block and set the Pin number to 0, and
the Sample time to 0.01 second.
5. Drag the Standard Servo Write block into the model. Double-click the block and set the Pin number to 4.
6. Connect the Analog Input and the Standard Servo Write blocks.
7. From Simulink Math Operations library, drag the Gain block into the model and drop it on the line connecting
theAnalog Input and the Standard Servo Write block. Double-click the Gain block and set its value to 0.1760
(maximum servo motor displacement in degrees divided by analog input digital resolution i.e. 180/1023).
8. In your Simulink model, click Tools > Run on Target Hardware > Prepare To Run... .
9. When the Configuration Parameters page opens up, set the Target hardware parameter to supported
Arduino hardware. Do not change any other settings.
10. Click OK.
11. In your Simulink model, click the Deploy To Hardware button on the toolbar. The model will now be
deployed to the Arduino hardware.
12. When the model starts running on Arduino board, observe the motor shaft sweeping between 0 and 180
degrees.
If you have trouble creating the model as described above, you can use the Servo Control model.
Task 5 - Control the Servo Motor Position via a Photocell
In this task you will create a model that sets the position of the servo motor according to light intensity detected
by a photocell. In a dark room the motor shaft should be positioned at 0 degrees, while in a bright room it
should be positioned at 180 degrees.
Use a photocell and a resistor instead of the potentiometer you used in Task 4. Connect one end of the
photocell to the 5V pin on the Arduino board. Next, connect one end of the resistor to the GND pin on the
board. Connect the other ends of the photocell and the resistor together and then to the analog input pin 0 on
the board.
Use Digital Input block to adjust the motor shaft position depending on the external control signal or signals.
Use Standard Servo Read block to read the motor shaft position and compare it with the desired position.
Summary
This example showed you how to use Simulink Support Package for Arduino Hardware to control a standard
servo motor. In this example you learned that:
Standard Servo Write block allows you to set the servo motor shaft position, usually in the range from 0 to 180
degrees.
Analog Input block measures the voltage applied to an Arduino analog input pin. Variety of sensors can be
attached to analog pins.
Supported Hardware:
Arduino Esplora
Arduino Fio
Arduino Leonardo
Arduino Micro
Arduino Mini
Arduino Pro
Arduino Uno
Available versions of this example:
Arduino Mega 2560 board: arduinomega2560_gettingstarted
The provided model is pre-configured for Arduino Mega 2560 and can be run on any of the borad listed in the
"Suppoerted Hardware" section, by changing the "Target Hardware" parameter in the configuration parameters
of the model as described in Task 4 of this example.
On this page
Introduction
Prerequisites
Required Hardware
Model
Task 1 - Connect an LED to an Arduino Output Pin
Task 2 - Review Arduino Block Library
Task 3 - Create a Model for Arduino Hardware
Task 4 - Configure and Run the Model on Supported Arduino Hardware
Other Things to Try
Summary
Introduction
Simulink Support Package for Arduino Hardware enables you to create and run Simulink models on Arduino
board. The target includes a library of Simulink blocks for configuring and accessing Arduino sensors,
actuators and communication interfaces. Additionally, the target enables you to monitor and tune algorithms
running on Arduino board from the same Simulink models from which you developed the algorithms.
In this example you will learn how to create and run a simple Simulink model on Arduino board. See other
examples for Arduino board to learn how to use External mode and to learn how to implement more complex
algorithms.
Prerequisites
If you are new to Simulink, we recommend completing Interactive Simulink Tutorial, reading the Getting Started
section of the Simulink documentation and running Simulink Getting Started example.
Required Hardware
To run this example you will need the following hardware:
USB cable
LED
Breadboard wires
Create and run a model that turns the LED on if a signal is applied to a digital input pin.
Create and run a model that repeatedly brightens and dims an LED. Hint: use the PWM block.
Summary
This example introduced the workflow for creating an algorithm from a Simulink model and then running it on
the supported Arduino board. In this example you learned that:
Simulink Support Package for Arduino Hardware provides blocks for configuring, reading from and writing to
Arduino sensors and actuators.
You can use the Deploy To Hardware button to configure and run the model on supported Arduino board.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
arduinomega2560_gettingstarted
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB
commands.