You are on page 1of 19

EE-361 Feedback Control Systems

Introduction to Simulink and Data Acquisition


Experiment # 2

Date: / / 2022 Section:

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

Figure 2: Library Browser of Simulink

To open Simulink, there are two options:

1. Click on the simulink button in the menu bar.

2. Type ”simulink” in the command window.

The appearing window is shown in Figure-1.

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.

Simulink → Continuous → Transfer Fcn

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.

Question: Why does this error occur?

1.1.1 Building a Block Diagram in Simulink


It is very easy to build a block diagram in the Simulink. First open a new
model file. Place the required blocks in the blank model file and connect them
as required. Build the block diagram as shown in Figure-4 and run it.

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.

1.2 Data Acquisition in MATLAB


There are multiple options to acquire data in MATLAB from external sensors.
One of them is to use the Data Acquisition Toolbox of MATLAB. Recently, low
cost microcontroller boards like Arduino have been equipped with toolboxes to
communicate with Simulink directly. One such package is Simulink Support
P ackage f or Arduino Hardware.

1.2.1 Getting Started with Arduino Mega 2560 Hardware


This example shows how to use Simulink Support Package for Arduino Hard-
ware to run a Simulink model on Arduino Mega 2560 board. Simulink Support
Package for Arduino Hardware enables you to create and run Simulink models
on Arduino Mega 2560 board. The target includes a library of Simulink blocks
for configuring and accessing Arduino sensors, actuators and communication

4
Figure 4: Basic Block Diagram

Figure 5: Step Function Window

5
Figure 6: Scope Function Window

Figure 7: Getting started with Arduino Hardware using Simulink

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.

1.2.2 Required Hardware


To run this example you will need the following hardware: Arduino Mega 2560
board, USB cable, LED, 220 Ohm resistor and Breadboard.

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.

Task 2: Review Arduino Block Library

Simulink Support Package for Arduino Hardware provides an easy way to


create 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 and open a new model.


2. In the Simulink Library Browser, navigate to Simulink Support Package
for Arduino Hardware → Common.
3. Connect Arduino Mega 2560 with your PC through USB cable.
4. Drag a Digital Output block on the new model and save the file.
5. 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.

Task 3: Create a Model for Arduino Mega 2560 Hardware

In this task, you will create a simple Simulink model that changes the state
of the Arduino digital output pin.

1. In MATLAB, select Home → New → Simulink Model.


2. Drag the Pulse Generator block from the Simulink Sources library to your
model.
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.

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.

Exercise-3: Show the resulting waveform in oscilloscope to instructor.


Take a snapshot as well for your report.

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.

1.2.3 Communicating with Arduino Mega 2560 Hardware


This example shows how to tune the parameters and monitor the signals of an
algorithm running on Arduino Mega 2560 board.

Simulink Support Package for Arduino Hardware enables you to moni-


tor 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

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.

Simulink’s External mode feature enables you to accelerate the process


of parameter 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 communicated to the target hardware immediately. The effects of the pa-
rameters 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:

- Set up communication between Simulink and Arduino Mega 2560 board.


- Use a Simulink model to tune the parameters of an algorithm that is
running on Arduino Mega 2560 board.
- Use Simulink scopes to monitor the state of an algorithm running on
Arduino Mega 2560 board.

1.2.4 Required Hardware


To run this example you will need the following hardware: Arduino Mega 2560
board, USB cable, LED220 Ohm resistor, Breadboard.

Task 1: Connect an LED to the Arduino Output Pin

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.

Task 2: Simulate the Model

11
Figure 10: Connect an LED to the Arduino Output Pin

Figure 11: Motion Energy block properties

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.

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.

1.2.5 Analog Input


To check the analog input of the Arduino Mega 2560, set up a new simulink
model as shown in Figure-16. Configure the function generator with following
settings:

Sine Wave
5 Hz
5 V (peak to peak)

14
Figure 13: Model for Analog Input

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-5: Give the plot of the Simulink scope.

Exercise-6: Repeat the same procedure with the following sampling rates
(Change the sampling rate by a double click on the Analog Input block).

Sampling Rate = 500


Sampling Rate = 100
Sampling Rate = 50

1.2.6 Digital Input


To use the digital input block, open a new model file. Place the digital input
block from the Simulink Support P ackage f or Arduino Hardware and con-
nect the model as given in Figure-18. Set following properties:

Simulation Time (in Simulink) = 10 sec


Sample time = 0.01 sec

15
Figure 14: Model for Digital Input

Now connect the function generator to pin 8 with the following settings:

Output Type = TTL


Frequency = 10 Hz

Note: Take care of Nyquist-Shannon sampling theorem while deciding for


sampling 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
effect on the Simulink scope. Also, save all these response for your report.

16
Figure 15: Model for Analog Output

1.2.7 Analog Output


There is no block in Simulink Support P ackage f or Arduino Hardware for
Analog Output. But there are other options which can be used for this pur-
pose. One of them is to use PWM Block. The model for this task is shown in
Figure-19. The parameter values for the blocks are as:

Input: Sine Wave


Frequency: 100 rad/sec
Amplitude: 127
Bias: 127
Phase: 0
Sample Time = 0.001 sec
Simulation Time (in Simulink) = 10 sec

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.

Exercise-8: Show the resulting waveform in oscilloscope to instructor.


Take a snapshot as well for your report.

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

Simulink Design Optimization


MATLAB command line → srolib

19

You might also like