Professional Documents
Culture Documents
- [1.1] Question: Why did the error occur in the transfer function of
Example 3?
- [1.1.1] Exercise-1: Provide the result of the scope from the block dia-
gram shown in Figure-3.
- [1.2] Exercise-2: Provide the scope plot from Exercise-2, using the xla-
bel, ylabel, title, axis, and legend commands.
- [1.3.2] Exercise-3: Show the resulting waveform in oscilloscope to in-
structor. Take a snapshot as well for your report.
- [1.3.4] Exercise-4: Show the resulting LED to the instructor. Save the
plot from scope.
- [1.3.5] Exercise-5: Give the plot of the Simulink scope.
- [1.3.5] Exercise-6: Show the ouput with each sample rate mentioned in
exercise.
- [1.3.6] Exercise-7: Run the simulation using different sampling times
and see the effect on the Simulink scope. Also, save all these response for
your report.
- [1.3.7] Exercise-8: Show the resulting waveform in oscilloscope to in-
structor. Take a snapshot as well for your report.
1
Instructions
Follow the instructions stated below:
1. All the exercises in this lab handout are to be evaluated by instructors.
After getting a result, ask the instructor to evaluate them.
2. Please save all your graphs by taking screen shots in a word file with the
exercise numbers mentioned.
3. After the lab, you (all members of a group) have to upload the above
mentioned word file into the drop box on LMS with the name of that
file as LabX-Y-N.pdf (X is the experiment number, Y is the day e.g.
Tuesday(Morning), Tuesday(Evening), Thursday and N is the name of
the student).
1 Introduction
In this lab session an introduction to Simulink is given to the students, in which
they learn how to use Simulink, how to program in Simulink, and how to acquire
data using the Simulink Support Package for Arduino Hardware.
1.1 Simulink
Simulink is a software package included in MATLAB for modeling, simulating,
and analyzing dynamical systems. It supports linear and nonlinear systems,
modeled in continuous time, sampled time, or a hybrid of the two. For mod-
eling, Simulink provides a graphical user interface (GUI) for building models
as block diagrams, using click-and-drag mouse operations. Simulink includes a
comprehensive block library of sinks, sources, linear and nonlinear components,
and connectors. You can also customize and create your own blocks.
Search for the desired block in the library browser, and then drag that block
and place it in the model file. Any block can be resized easily by dragging its
corners. To place the model of any physical system, drag the Transfer Function
2
Figure 1: Starting Window of Simulink
3
Figure 2: Window to Enter Transfer Function
To enter the coefficients of the Transfer Function, double click on the Trans-
fer Function block. A window will appear where you can enter these coefficients
as shown in Figure-2.
Following are some examples to understand how to use the Transfer Func-
tion block:
Example-1
s2 + 3s + 1
G(s) = (1)
s2 + 8s + 10
Numerator Coefficient = [1 3 1]
Denominator Coefficient = [1 8 10]
4
Figure 3: Basic Block Diagram
Example-2
s + 10
G(s) = (2)
s3 + 3s + 1
Numerator Coefficient = [1 10]
Denominator Coefficient = [1 0 3 1]
Example-3
s2 + 10s + 10
G(s) = (3)
s
Numerator Coefficient = [1 10 10]
Denominator Coefficient = [1 0]
On entering this, an error box will appear.
5
Figure 4: Step Function Window
6
Figure 6: Transferring Data from Simulink to Workspace
Exercise-1: Copy the result of the block diagram shown in Figure-3 and
save it to your word file.
The start and stop buttons for Simulink are shown in Figure-3. The simu-
lation time can be changed through the text box shown in Figure-3.
The list of the Simulink blocks used most often is given at the end of this
lab manual. This list will help you in future labs as well, so make sure to go
through it.
Set the properties of the block, ”To Workspace” as shown in Figure-7. Af-
ter setting the properties, rerun the simulation. This will generate the ”Data”
and ”tout” variables in the workspace as shown in Figure-8. The ”Data” object
will contain information for the input and output signals.
To plot these variables, we have to separate the information from this object
into two different variables. Run the following commands from the command
7
Figure 7: Properties of ’To Workspace’ Block
8
Figure 9: System’s Response from Command Line
output = Data(:,2);
input = Data(:,1);
Now we can plot the input and output signals in separate windows as well as in
the same window. Run the following command to plot the input and output in
the same window with different colors and line widths:
plot(tout,input,’r’,’lineWidth’,2);
hold
plot(tout,output,’b-.’,’lineWidth’,2);
legend(’Input’,’Output’)
axis([0 10 0 5.1])
This will display the output as shown in Figure-9. Save this to your word file.
Exercise-2: Explore the following commands, use them in the last response,
and save the results.
xlabel
ylabel
title
axis
legend
9
Figure 10: Getting started with Arduino Hardware using Simulink
In this example you will learn how to create and run a simple Simulink model
on Arduino Mega 2560 board.
10
Task 1: Connect an LED to an Arduino Output Pin
In this task, you will connect an LED to an Arduino output pin so you can see
changes in the logical state of the pin (See Figure-11).
1. Attach one end of the 220 Ohm resistor to output pin 9 on the Arduino
Mega 2560 board. Use the recommended breadboard and the breadboard
wires.
2. Attach the long leg (positive) of the LED to the resistor. Attach the short
leg (negative) to the ground pin on the Arduino Mega 2560 board.
Simulink Support Package for Arduino Hardware provides an easy way to cre-
ate algorithms that use Arduino sensors and actuators by using the blocks that
can be added to your Simulink model. The blocks are used to configure the
associated sensors and actuators, as well as to read and write data to them.
1. Enter simulink at the MATLAB prompt. This opens the Simulink Library
Browser.
2. In the Simulink Library Browser, navigate to Simulink Support Package
for Arduino Hardware → Common.
3. Double-click the Digital Output block. Review the block mask, which
contains a description of the block and parameters for configuring the
associated Arduino digital output pin.
In this task, you will create a simple Simulink model that changes the state of
the Arduino digital output pin.
3. Double-click the Pulse Generator block. Set the Pulse type to parameter
to Sample based and set the sample time parameter to 0.1 second.
4. Drag the Digital Output block to the model. Use the default block set-
tings.
5. Connect the Pulse Generator block to the Digital Output block.
11
Figure 11: Hardware Connections
12
Figure 12: Arduino Common Blocks
Task 4: Configure and Run the Model on Arduino Mega 2560 Hard-
ware
In this task, you will configure and run your model on the Arduino Mega 2560
board.
1. Connect the Arduino Mega 2560 board to your computer with a USB
cable.
2. In your Simulink model, click Tools → Run on Target Hardware → Prepare
To Run... .
3. When the Configuration Parameters page opens up, set the Target hard-
ware parameter to Arduino Mega 2560. Do not change any other settings.
4. Click OK.
5. In your Simulink model, click the Deploy To Hardware button on the
toolbar. The model will now be deployed to the Arduino Mega 2560
hardware.
6. Look at the LED attached to pin 9. The LED should blink one time every
second.
7. Save your model.
13
Bonus Task
1. Create and run a model that turns the LED on if a signal is applied to a
digital input pin.
2. Create and run a model that repeatedly brightens and dims an LED.
Hint: use the PWM block.
Simulink Support Package for Arduino Hardware enables you to monitor and
tune algorithms running on Arduino Mega 2560 board from the same Simulink
models from which you developed the algorithms.
In this example you will learn how to tune and monitor the algorithm in real
time as it is executing. When you are developing algorithms, it is often nec-
essary to determine appropriate values of critical algorithm parameters in an
iterative fashion. For example, a surveillance algorithm that measures motion
energy in a room may use a threshold to determine an intruder in the presence
of ambient noise. If the threshold value is set too low, the algorithm may er-
roneously interpret any movement as an intruder. If the threshold value is set
too high, the algorithm may not be able to detect any movement at all. In such
cases, the right threshold value may be obtained by trying different values until
the desired algorithm performance is reached. This iterative process is called
parameter tuning.
Simulink’s External mode feature enables you to accelerate the process of pa-
rameter tuning by letting you change certain parameter values while the model
is running on target hardware, without stopping the model. When you change
parameter values from within Simulink, the modified parameter values are com-
municated to the target hardware immediately. The effects of the parameters
tuning activity may be monitored by viewing algorithm signals on scopes or
displays in Simulink.
This example introduces the Simulink External mode feature by showing you
how to:
14
- Use Simulink scopes to monitor the state of an algorithm running on
Arduino Mega 2560 board.
In this task, you will connect an LED to an Arduino digital output pin so you
can see changes in the logical state of the pin.
1. Attach one end of the 220 Ohm resistor to digital output pin 9 on the
Arduino Mega 2560 board. Use the recommended breadboard and the
breadboard wires.
2. Attach the long leg (positive) of the LED to the resistor. Attach the short
leg (negative) to the ground pin on the Arduino Mega 2560 board.
1. To simulate the model, create a model as shown in Figure- 14. For Motion
Energy block, go to:
Simulink → Sources → Repeating Sequence Square
and set the properties as Figure-15:
15
Figure 13: Connect an LED to the Arduino Output Pin
16
Figure 15: Motion Energy block properties
17
For Vector of output values, download dataset data lab2 from LMS
and load it in workspace. This dataset will represent the output of motion
sensor for our case now.
2. Observe that the model plays the motion energy recorded in a room and
compares it with a threshold to detect intrusion.
3. In the model, change the Simulation mode on the toolbar to Normal. This
tells Simulink to run the model on the host computer. See Task 3 below
to run the model on the Arduino target hardware.
4. In the model, click the Run button in the Simulink toolbar.
5. Click the Scope block. Observe that the algorithm detects multiple intru-
sions.
6. Click Stop button in the Simulink model.
Task 3: Run the Model in External Mode
1. Connect a USB cable from your computer to the Arduino Mega 2560
board.
2. In the model, change the Simulation mode on the toolbar to External.
3. In the model, click the Run button on the toolbar.
Task 4: Communicate with the Model
At this point, your model is running on Arduino Mega 2560 board. As the
model runs on hardware, it communicates with Simulink model in External
mode using serial connection over USB.
1. Notice that the LED attached to pin 9 is glowing almost constantly. This
means that the selected threshold is too low and that the algorithm mis-
interprets even a minor motion energy change as an intrusion. You need
to find a more optimal value of the threshold.
2. Double-click the Threshold block in the model, increase its value, and click
OK or Apply. This changes the threshold value in the model running on
the board.
3. Check whether the glowing pattern of the LED has changed. The LED
should light up every 10 and 11.5 seconds in a correctly tuned algorithm.
4. If there is no change in LED light pattern, repeat the Steps 2 and 3 until
you find the right value of the threshold.
5. Click Stop button in the Simulink model.
Exercise-4: Show the resulting LED to the instructor. Save the plot from
scope.
18
Figure 16: Model for Analog Input
Sine Wave
5 Hz
5 V (peak to peak)
Sample Time = 0.001 sec
Simulation Time (in Simulink) = 5 sec
Note: Analog Output block gives the measurement as a 10-bit value that ranges
from 0 to 1023. Use proper gain for the model considering the maximum value
of the analog signal you are measuring. In our case, be sure not to use more
than 5V.
Note: Make sure that you are running the model in External Mode now as
shown in Figure- 17
Next, connect the output of the function generator to Analog Input Pin 4 of
Arduino. The channel numbers are properly defined on each Arduino, so please
connect the function generator with the it properly. Connect the ground of the
function generator to GND of the Arduino. Then connect the oscilloscope to
Analog Input Pin 4 of Arduino. Now run the simulation in External Mode.
Exercise-6: Repeat the same procedure with the following sampling rates
(Change the sampling rate by a double click on the Analog Input block).
19
Figure 17: External Mode Simulation
Now connect the function generator to pin 8 with the following settings:
Note: Take care of Nyquist-Shannon sampling theorem while deciding for sam-
pling time.
Now run the simulation and check the response on the Simulink scope.
Exercise-7: Run the simulation using different sampling times and see the ef-
fect on the Simulink scope. Also, save all these response for your report.
20
Figure 18: Model for Digital Input
As this signal is being applied to PWM block, you need an RC low pass filter
at the output to convert it into smooth wave. Choose suitable values of R and
C and see the effect for different combinations.
21
Figure 19: Model for Analog Output
2 Important Blocks
Constant
simulink → Sources (or Commonly Used Blocks) → Constant
Integrator
Simulink → Continuous (or Commonly used Blocks) → Integrator
Transfer Function
Simulink → Continuous → Transfer Fcn
State Space
Simulink → Continuous → State-Space
Saturation
Simulink → Discontinuities → Saturation
Zero-Order Hold
Simulink → Discrete → Zero-Order Hold
Subtract
Simulink → Math operations → Subtract
Sum
Simulink → Math operations → Sum
Subsystem
Simulink → Ports and Subsystems → Subsystem
Mux
Simulink → Signal Routing → Mux
Scope
22
Simulink → Sinks → Scope
To File
Simulink → Sinks → To File
To Workspace
Simulink → Sinks → To Workspace
Pulse Generator
Simulink → Sources → Pulse Generator
Ramp
Simulink → Sources → Ramp
Signal Generator
Simulink → Sources → Signal Generator
Step
Simulink → Sources → Step
MATLAB Function
Simulink → User-defined Functions → MATLAB Functions
PID
Simulink →Continuous→ PID Controller
23