Professional Documents
Culture Documents
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
or Thursday, and N is the name of 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.
1
Figure 1: Starting Window of Simulink
Click on Blank Model to open it. And then click on Library Browser to see
its available building blocks.
2
Figure 3: Window to Enter Transfer Function
To create a new model, 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 Block from the following path.
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-3.
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]
3
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.
Note: To set up the step function, double click on its block and set the val-
ues according to those found in Figure-5. Also double click on the scope function
block and check the ”save data to workspace” option as illustrated in Figure-6.
Run the simulation now. Plot the response using plot(out.tout, out.ScopeData(:
, 2)) and inspect the results.
Exercise-1: Copy the result of the block diagram shown in Figure-4 and
save it to your word file.
4
Figure 4: Basic Block Diagram
5
Figure 6: Scope Function Window
interfaces. Additionally, the target 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 create and run a simple Simulink
model on Arduino Mega 2560 board.
6
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-8).
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.
In this task, you will create a simple Simulink model that changes the state
of the Arduino digital output pin.
7
Figure 8: Hardware Connections
8
Figure 9: Arduino Common Blocks
9
Task 4: Configure and Run the Model on Arduino Mega 2560
Hardware
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.
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.
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
10
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.
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.
11
Figure 10: Connect an LED to the Arduino Output Pin
12
Figure 12: Motion Energy block properties
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:
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.
13
5. Click the Scope block. Observe that the algorithm detects multiple intru-
sions.
6. Click Stop button in the Simulink model.
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.
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.
Sine Wave
5 Hz
5 V (peak to peak)
14
Figure 13: Model for Analog Input
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).
15
Figure 14: Model for Digital Input
Now connect the function generator to pin 8 with the following settings:
Now run the simulation and check the response on the Simulink scope.
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.
16
Figure 15: Model for Analog Output
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.
17
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
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
18
Signal Generator
Simulink → Sources → Signal Generator
Step
Simulink → Sources → Step
MATLAB Function
Simulink → User-defined Functions → MATLAB Functions
PID
Simulink →Continuous→ PID Controller
19