You are on page 1of 17

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/339003854

Introduction to Interfacing Arduino Hardware and MATLAB ® Simulink ®

Preprint · February 2020

CITATIONS READS

0 4,244

2 authors:

Francisco Gonzalez-Longatt Martha Nohemi Acosta Montalvo


University of South-Eastern Norway University of South-Eastern Norway
391 PUBLICATIONS   2,771 CITATIONS    25 PUBLICATIONS   42 CITATIONS   

SEE PROFILE SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Smart Multi-Terminal DC micro-grids for autonomous Zero-Net Energy Buildings View project

My documents of DIgSILENT PowerFactory View project

All content following this page was uploaded by Francisco Gonzalez-Longatt on 03 February 2020.

The user has requested enhancement of the downloaded file.


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

Introduction to Interfacing Arduino Hardware And MATLAB®-


Simulink®
Created by: Martha Nohemi Acosta Montalvo. February 2020.
Revised by: Professor Francisco Gonzalez-Longatt

Introduction
Arduino is an open-source hardware and software company that designs and manufactures
single-board microcontrollers and microcontroller kits for building digital devices. The Arduino
company products (Arduino boards and software) can be produced and distributed by anyone
since these products are licensed under the GNU Lesser General Public License (LGPL) or the
GNU General Public License (GPL).
A program for Arduino hardware may be written in any programming language with compilers
that produce binary machine code for the target processor.
The Arduino integrated development environment (IDE) is a cross-platform application (for
Windows, macOS, Linux) that is written in the programming language Java.
On October 18th, 2019, Arduino Pro IDE (alpha preview) was released. The system still uses
Arduino CLI (Command Line Interface), but improvements include a more professional
development environment, autocompletion support, and Git integration.
However, Arduino programming is supposed to be fun but can become frustrating and time-
consuming for tasks such as plotting sensor data or incorporating advanced math, signal
processing, or controls routines into your projects.
MATLAB® and Simulink® address several challenges with traditional Arduino programming.
MATLAB support package for Arduino allows writing MATLAB programs that read and write
data to Arduino and connected devices, such as Adafruit motor shield, I2C, and SPI devices. The
main benefits of using MATLAB for Arduino programming are:
• Read and write sensor data interactively without waiting for code to compile.
• Analyse the sensor data using thousands of pre-built functions for signal processing, machine
learning, mathematical modelling, and more.
• Quickly visualise the data using the vast array of plot types in MATLAB.

This short note presents some details of a simple example of

MATLAB Support Package for Arduino Hardware


MATLAB Support Package for Arduino Hardware enables the user to use MATLAB® to
communicate with the Arduino® board over a USB cable. This package is based on a server
program running on the board, which listens to commands arriving via serial port, executes the
commands, and, if needed, returns a result. This support package supports Arduino Uno,
Arduino Mega 2560 and Arduino Due hardware. Moreover, this approach helps the user:
• Start programming right away without any additional toolboxes.
• Work in the MATLAB for interactive development and debugging.
• Interactively develop programs to acquire analog and digital data, and to control DC,
servo, and stepper motors.
• Access peripheral devices and sensors connected over I2C or SPI.
• Run control loops at up to 25 Hz (not real-time).
• Introduce mechatronics, signal processing, and electronics concepts in classroom labs.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 1 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

Installing the MATLAB Support Package for Arduino Hardware


1. Create an account in MATLAB works https://www.mathworks.com/
2. Install the Support Packages for Arduino Hardware:
a) In MATLAB, select HOME > Add-Ons > Get Hardware Support Packages [Fig.
1].

FIG. 1. OPEN THE A DD-O N E XPLORER WINDOW . “GET HARDWARE …”


b) The Add-On Explorer window will appear, click on Simulink Support Packages for
Arduino Hardware [Fig. 2].

FIG. 2. A DD-O N EXPLORER WINDOW


c) Click on the install button and select the install option [Fig. 3].

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 2 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

FIG. 3. I NSTALL S IMULINK SUPPORT PACKAGE FOR ARDUINO H ARDWARE


d) Log in with the previously created account [Error! Reference source not found.].

FIG. 4. LOG IN INTO THE MATLAB ACCOUNT


e) Accept the license agreement, then click Next button on the Third-Party Software
window. Wait for all packages to be downloaded and installed. Disable the Open
Examples box and click Finish [Error! Reference source not found.].

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 3 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

FIG. 5. I NSTALLATION PROCEDURE.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 4 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

Reader must find useful looking into the official MathWorks videos related to MATLAB-
Simulink interaction with Arduino.
https://uk.mathworks.com/matlabcentral/fileexchange/47522-matlab-support-package-for-
arduino-hardware

Example: Implementation of a Sine-wave generator


This example is dedicated to implementing a sinewave generator using an Arduino Due. The
Arduino Due will work as an AC signal generator, creating a perfect sine wave defined inside
the Simulink model, for this purpose the analog output of the Arduino Due is used.
The implementation of this example is very simple to follow. First, a sine wave block of Simulink
creates a digital signal, then this signal is adjusted and converted into an analogic signal.
Finally, the signal is sent to the Arduino Due though an Analog Output block of Simulink.
The procedure to read a signal from the Arduino Due is the signal is read through an Analog
Input block of Simulink. This analogy signal is adjusted and converted into a digital signal.

Create a Model for Arduino Hardware


1. In MATLAB, select HOME > New > Simulink Model [Fig. 6].

FIG. 6. OPEN SIMULINK AND CREATE A NEW EMPTY SIMULINK MODEL


2. Simulink Start Page will appear. Click in Blank Model option [Fig. 7] to create a new
model.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 5 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

FIG. 7. CREATING A NEW SIMULINK MODEL


3. Open the library browser by clicking SIMULATION > Library Browser [Fig. 8].

FIG. 8. OPEN LIBRARY BROWSER


4. Insert the necessary blocks to create the model (In this example, the following blocks will
be used: Sine Wave, Gain, Constant, Data Type Conversion, Arduino (Analog Output),
Arduino (Analog Input), Sum, Scope):
a) Go to the Library Browser and type the name of the block and click the button
(See Fig. 10).

FIG. 9. I NSERT A BLOCK FROM THE LIBRARY BROWSER ONTO THE WORKSPACE .
b) Select the block and drag it from the Library Browser to the model workspace
[Fig. 9].

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 6 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

FIG. 10. SEARCH A BLOCK


c) Repeat the steps a) and b) until all elements require to construct the model be in
the workspace.
5. Connect the blocks, as shown in Fig. 11.

FIG. 11. MODEL FOR IMPLEMENTING A SINE-WAVE GENERATOR IN A RDUINO D UE.


6. Adjust the parameters of each block as indicate Table 1.
Analog Output block: generates voltage on the specified DAC pin of the Arduino Due,
which has two 12-bit DAC pines: DAC0 and DCA1. The Analog Output block accepts
uint16 values as an input. However, it considers only the 12 most significant bits for the
conversion. The voltage produced by this block on the DCA pin is:
 input  (1)
Vout = Vref  N
− 1  + Voffset
 2 

where Vref = 2.2, input = 0 < input < 4095, N = 12 (number of bits on DAC channel), Voffset
= 0.56. Vref is the reference voltage for the DAC pin. Therefore, to obtain the output signal
in the correct values, it is necessary to compute input from (1) as follows:

 N 
(
input =  2 − 1  Vout − Voffset ) (2)
 Vref 
Substituting the known values:

input =  2 − 1  (Vout − 0.56 )


12
(3)
 2.2 

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 7 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

Therefore, several adjustments must be made before sending the signal to Arduino
Due:
• An offset must be added to the signal; the value of the offset must have the same
value of the amplitude of the signal to have a completely positive signal.
• A gain of (212-1)/2.2 must be added to deliver the signal whit the magnitudes
corrections.
• The signal must be converted to uint16 because it is the type of accepted values
in the Analog Output block.
Analog Input block: Measure the voltage of an analog pin relative to the analog input
reference voltage on the Arduino hardware. If the measured voltage is equal to the analog
reference voltage, the block output emits 1023. The default value of the analog input reference
voltage for Arduino Due boards is 3.3V. Therefore, for coupling the measured analog signal, it
is necessary to add a gain of 3.3/1023 that modifies this signal to the required ranges. Moreover,
it is required to subtract the offset that was previously attached to have an entirely positive
signal.
Table 1. Parameters of the Model
Block Equation Parameters

Sine type=Time based


Amplitude (A)=1
Bias=0
Sine Wave f (t ) = A sin(t +  ) Frequency (ωt)=10 rad/s
Phase (θ) = 0 rad
Sample time =0
2N − 1
G1 =
Vref N =12
Vref = 2.2V
N: Number of bits on DAC channel
Gain
Vref: Reference voltage for the DCA pin

Vrefinput Vrefinput = 3.3 V


G2 =
Output Output=1023

Vrefinput : Analog input reference voltage

Data Type
Conversion Output data type: uint16
--
uint16: converts inputs to 16-
bit signed integer.

Arduino (Analog -- DAC pin = DAC0


Output)
Arduino (Analog -- Pin number = 0
Input) Sample time=−1
Constant -- C1= 1
-- C2=−1

7. Save your model.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 8 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

Configure and Run the Model in the Arduino


This task is dedicated to configuring and run the model on the supported Arduino board.
1. Connect the Arduino board to your computer with a USB cable.
2. In the Simulink model, click Hardware > Hardware Settings to open the Configuration
Parameters dialogue [Fig. 12].

FIG. 12. OPEN THE CONFIGURATION PARAMETERS WINDOW


3. Select the Hardware Implementation and select the required Arduino hardware from
the Hardware board parameter list (For this model, Arduino Due is used). Do not
change any other settings [Fig. 13].

FIG. 13. SELECT THE A RDUINO HARDWARE TO BE USED


4. Click OK.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 9 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

5. Click the Monitor & Tune -button. The model will now be deployed to the connected
Arduino hardware and a code generation report will appear [

6.
7. Fig. 14].

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 10 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

FIG. 14. R UN THE MODEL


8. To run the model standalone on the Arduino, click the Build, Deploy & Start button [Fig.
15].

FIG. 15. R UN THE MODEL STANDALONE ON THE A RDUINO

Hardware Setup
The sine wave generator created in the previous subsections using the Simulink® Support
Package for Arduino® Hardware. The sine wave generator was designed in Simulink, but it
must be deployed and used inside the Arduino Due, as a consequence, hardware setup is needed.
The top view of Arduino Due is presented in Fig. 16.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 11 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

FIG. 16. GENERAL VIEW OF THE A RDUINO D UE


The Arduino Due must be connected using the USB port of the host computer, to do so, attach
the USB micro side of the USB cable to the Due's Programming port (this is the port closer
to the DC power connector).

FIG. 17. GENERAL VIEW OF THE A RDUINO D UE SHOWING THE PROGRAMMING PORT

FIG. 18. MICRO USB CONNECTED TO THE ARDUINO D UE SHOWING THE


PROGRAMMING PORT .

The general scheme of connection of all hardware is depicted in Fig. 19.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 12 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

Simulink model

USB connection
GND DAC0

FIG. 19. GENERAL HARDWARE SETUP LAYOUT .


The output of the Arduino Due is connected to an oscilloscope using a pair of cables. The
cables are connected to the GND terminal in the Power Pins, and the analog output is taken
from DAC0, following the consideration taken in the Simulink model (see Fig. 20).

FIG. 20. CABLES ATTACHED TO GND AND DAC0.


An oscilloscope probe is used to make physical and electrical connection between a test points
and the oscilloscope. In this document a 50 MHz oscilloscope, Tektronix TBS 1052B-Edu is
used to collect the test results. The whole hardware set is shown in Fig. 21.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 13 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

Oscilloscope

Oscilloscope
probe
Arduino Due

USB Connection

FIG. 21. HARDWARE SETUP.

Measurements
This section is dedicated to present recording of the test process of the sine wave generator
implemented using Arduino Due. The initial setting of the Sine wave block is as shown in Fig.
22.

F IG. 22. SETTINGS OF THE SINE WAVE B LOCK. AMPLITUDE = 1, FREQUENCY ( RAD/ SEC)
= 10 RAD/ SEC ( F = 1.5915 HZ)

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 14 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

The output of the scope in the simulink model is presented in Fig. 23.

F IG. 23. SCOPE OF THE S IMULINK MODEL. AMPLITUDE = 1, FREQUENCY ( RAD/ SEC) = 10
RAD/ SEC (F = 1.5915 HZ ). O UTPUT OF THE SINE WAVE BLOW (BOTTOM ), O UTPUT OF THE
DATA TYPE CONVERSION (U NIT 16).
The 50 MHz oscilloscope, Tektronix TBS 1052B-Edu, is used to record the analog output from
the Arduino Due and the screen capture is presented in Fig. 22.

FIG. 24. CAPTURE OF THE A RDUINO D UE SHOWING THE SINE WAVE GENERATOR . (F =
1.5915 H Z)
Now, the frequency of the sine wave is increased and the setting the parameters of the sine
wave block inside the Simulink model as presented in Fig. 25. The amplitude is adjusted to 1,
and the frequency  = 314.1593 rad/sec, as a consequence, f = 50 Hz.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 15 of 16


Introduction to Interfacing Arduino Hardware and MATLAB® Simulink®

FIG. 25. SETTINGS OF THE SINE WAVE B LOCK.

MNAM and FGL Intro_Arduino_Matlab_v5.docx Page 16 of 16

View publication stats

You might also like