Getting Started With Matlab Simulink and Arduino PDF
Getting Started With Matlab Simulink and Arduino PDF
Getting Started With Matlab Simulink and Arduino PDF
Preface
1. Preparing Development Environment
1.1 Arduino
1.1.1 Arduino Uno
1.1.2 Arduino Leonardo
1.1.3 Arduino Mega 2560
1.1.4 Arduino Due
1.2 Electronic Components
1.2.1 Arduino Starter Kit
1.2.2 Fritzing
1.2.3 Cooking-Hacks: Arduino Starter Kit
1.2.4 Arduino Sidekick Basic kit
1.3 Matlab Simulink
1.4 Arduino Software
1.5 Testing
2. Matlab Simulink and Arduino
2.1 Matlab
2.2 Installing Arduino for Simulink Target
3. Hello World - Matlab Simulink and Arduino
3.1 Hello World
3.2 Creating Arduino Simulink
3.2.1 Configuring Arduino Digital Output
3.2.2 Configuring Pulse Generator
3.2.3 Configuring Scope
3.3 Configuring Arduino
3.3 Running Simulink
4. Simulink with Arduino Digital I/O
4.1 Working with Arduino Digital I/O
Preface
This book was written to help anyone wants to get started in Arduino and Matlab Simulink. It
describes all the basic elements of the Arduino and Matlab Simulink with step-by-step approach.
Agus Kurniawan
Berlin, September 2013
To develop a program with Matlab Simulink and Arduino, you need the following required
environment:
Arduino
Matlab
Arduino software
Electronic components for testing
In this chapter, I'm going to explain in brief required items for Matlab simulink and Arduino
development.
1.1 Arduino
Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware
and software. This board uses Atmel microcontroller series. There are many Arduino hardware
models that you can use. Further information about Arduino products, you can visit on
website http://arduino.cc/en/ .
You must one Arduino hardware to follow practices in this book. I recommend to obtain one of the
following Arduino hardware:
Arduino Uno
Arduino Leonardo
Arduino Mega 2560
Arduino Due
You can buy this product on your local electronic store. You also can order it by online. Find it
on http://arduino.cc/en/Main/Buy. The following is the list of Arduino store you can buy
Arduino store, http://store.arduino.cc/
Amazon, http://www.amazon.com
Cooking-hacks, http://www.cooking-hacks.com/index.php/shop/arduino.html
RS Components, http://www.rs-components.com
Element 14, http://www.element14.com
EXP-Tech, http://www.exp-tech.de
Because Arduino is an open-source hardware, people can build it. It's called Arduino compatible.
Generally it's sold in low prices.
website, http://arduino.cc/en/Main/ArduinoBoardDue.
1.2.2 Fritzing
Store website: http://shop.fritzing.org/ .
You can buy Fritzing Starter Kit with Arduino UNO or Fritzing Starter Kit with Arduino Mega.
http://www.amazon.com/Arduino-Sidekick-Basic-Kit-Version/dp/B007B14HM8/
http://www.exp-tech.de/Zubehoer/Arduino-Sidekick-Basic-Kit.html
To develop application based on Arduino board, we need Arduino software. You can obtain it
on http://arduino.cc/en/Main/Software . Please install based on your platform.
If your platform is Ubuntu, you can install by writing this script
sudo apt-get install arduino
For Windows platform, you can download setup file and install it.
The following is a screenshot of Arduino software on Ubuntu platform.
If you run Arduino software on Windows platform, you should configure arduino.exe running as
Administrator. You can change it by editing file property. Click Compatibility and then checked Run
this program as an administrator.
1.5 Testing
For testing, I used Arduino Uno and Arduino Mega 2560 on Ubuntu and Windows 8 platforms.
This chapter explains how to work with Matlab Simulink and Arduino Software.
2.1 Matlab
You can work with Matlab Simulink with targeting on Arduino using Matlab 2013a or the latest
version. You also can work with Matlab 2013a Student Version.
Then you will the list of Simulink library include Arduino library.
Click Simulink Support Package for Arduino Hardware, then you will see all Arduino installed
library.
You entry which digital pin on your Arduino board that LED is connected. If finished, click OK
button.
Select Arduino on Target hardware. In this scenario, I used Arduino Mega 2560.
Click OK button.
After that, you will see Arduino configuration.
Choose Set host COM port for your Arduino device. Make sure your Arduino already connected.
For COM setting, I chosen Automatically.
Checked Enable External mode to obtain feedback results from Arduino.
If done, click OK button.
To deploy and run on Arduino, click menu Tools -> Run on Target Hardware -> Run .
Matlab will be compile the model and send it to Arduino. You can see the compiling process shown
in Figure below.
This chapter explains how to program simulink using Arduino Digital I/O.
pinMode(signal, OUTPUT);
}
void loop() {
digitalWrite(signal,
delay(2000);
digitalWrite(signal,
delay(1000);
digitalWrite(signal,
delay(1000);
digitalWrite(signal,
delay(500);
}
HIGH);
LOW);
HIGH);
LOW);
In this section, we're going to a simulink model with Arduino and LED.
The following is a simulink model.
Notes:
Digital Input and Digital Output on Simulink Support Package for Arduino Hardware
Logical Operator on Commonly Used Blocks
Scope on Commonly Used Blocks
Pulse Generator on Sources
Fill pin for Arduino digital output. I used pin 10 for my scenario.
If finished, click OK button.
4.4 Testing
After we build a simulink model and Arduino hardware, we can build and deploy to Arduino. Please
read chapter 3 section 3.3 if you don't know how to build and run a Arduino simulink.
If your model run successfully, you see that LED turned on and off.
You also can open Scope for digital input. Then you see the digital signal (pulse signal) graphic on
Scope library. The following is a sample graphic.
Open Scope library for pulse generator too. You can see a sample graphic as below.
Scope for AND operation shows the result of AND operation between digital input and pulse
generator. The following is a sample graphic.
This chapter explains how to work with Arduino Analog I/O and Matlab Simulink. We build two
simulink models to illustrate how to use Arduino analog I/O. The following are our scenarios:
Reading temperature sensor using Arduino analog input
Controlling RGB LED color using Arduino analog output (PWM)
Now we can start to build simulink model to read temperature analog sensor.
We use a divider approach. One of sensor pin is connected to 5V Arduino pin. Thermistor 503 is
attached to the Analog In of Arduino, A4. The following is hardware implementation.
Normally Arduino ADC generates value 0 - 1023. To read sensor data, I used an approach based on
this site, http://playground.arduino.cc/ComponentLib/Thermistor2 . In this scenario I used Celsius
unit. How to read and to convert to Celsius unit you can use the following function.
RawADC is a sensor value from Arduino analog input. This function will be implemented in simulink
model.
For analog input, I used pin 4. You may change it based on your hardware implementation.
The second module is subsystem. This module is Thermistor function implementation. The following
is function implementation.
Notes:
Devide, Add, Subtract, Product, Math Function on Math Operations
Constant on Commonly Used Blocks
Make sure all modules must be configured Output data type as double. The following is a sample of
Output data type.
You also can use Matlab function that you can create to change a Subsystem.
You can see Arduino read sensor values around 524-525. After that, these values will be converted to
Celcius using Thermistor function.
The following is a scope output from Thermistor function.
You can see the temperature data, for instance, on time 2 temperature has 26.16 Celcius.
Note:
Pin 1: Red
Pin 2: Common pin
Pin 3: Green
Pin 4: Blue
Note:
RGB LED pin 1 is connected to Arduino PWM pin 6
RGB LED pin 2 is connected to Arduino VCC 5V
RGB LED pin 3 is connected to Arduino PWM pin 5
RGB LED pin 4 is connected to Arduino PWM pin 4
Note:
Repeating Sequence library can be found on Sources.
On PWM library you can set Arduino PWM pin. In this case, I used Arduino PWN pins: 6, 5, and 4.
Finally you can configure Repeating Sequence for PWM pin 4 as follows.
You also can see the output of red values shown on Figure below.
The last you can see the output of blue values as below.
For Arduino Mega, you can not use serial port 0 (Rx 0 and Tx 0).
In this scenario, we use 2 Arduino devices, Arduino Mega 2560 and Arduino Mega 1280. The second
Arduino is be used as "bridge". It receives serial data from the first Arduino and then it will be sent
back to the first Arduino.
As I said, the second Arduino is be used as "bridge". We must write code to receive serial data and
send back to serial port.
Now open Arduino application. Then write this code.
int data = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
data = Serial.read();
Serial.write(data);
}
}
Save all codes for the second Arduino. Before you compile and upload codes to the second Arduino,
you must unplug cables from Rx 0 Tx 0. If not, you fail to upload codes to Arduino because it seems
serial port 0 that is be used.
Notes:
Serial Transmit and Serial Receive on Simulink Support Package for Arduino Hardware
Scope on Commonly Used Blocks
Data Type Conversion on Commonly Used Blocks
6.4 Testing
After we build a simulink model and Arduino hardware, we can build and deploy to Arduino. Please
read chapter 3 section 3.3 if you don't know how to build and run a Arduino simulink.
If success, open scope library. You can see values that was be sent to to serial transmit on the first
Arduino.
Now open the second scope. As you know, the second Arduino will send back the received data from
serial input (Rx 0). After that, the first Arduino will receive this data. You can see the result the
following Figure as below.
You can see on time 0 to 1, the first Arduino received noise signal data. After time 1, the first Arduino
received the normal data.
This chapter explains how to program simulink using Arduino and Servo motor.
The next step we are going to build a simulink with Arduino and servo motor.
I used two Arduino devices, Arduino Mega 2560 and Arduino Mega 1280. The second Arduino is be
used to receive serial data so we check on a serial monitor application to see serial data.
The following is my hardware implementation.
Ok, let's build our simulink hardware. Firstly, we build a program for the second Arduino.
Open your Arduino application and write this script
int data = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
data = Serial.read();
Serial.print("received: ");
Serial.println(data);
}
}
Save the code. Then compile and upload code to the second Arduino. Don't forget to change board
and port based on your Arduino model. When you upload a program to Arduino, please unplug any
cable from serial pins (Rx Tx pins).
Notes:
Standard Servo Write, Standard Servo Read and Serial Transmit on Simulink Support Package
for Arduino Hardware
Scope on Commonly Used Blocks
Counter Limited on Sources
Arduino servo write library is be used to control a servo. In this scenario, I used digital input pin 10.
The following is a screenshot of digital input dialog.
Fill pin same as pin value on standard servo write library. Fill 0.5 on Sample time.
If finished, click OK button.
We will obtain a value from this library between 0 and 180.
7.4 Testing
After we build a simulink model and Arduino hardware, we can build and deploy to Arduino. Please
read chapter 3 section 3.3 if you don't know how to build and run a Arduino simulink.
Before you run the simulink model, set simulation stop time about 90. You can change it that depends
on your period time. Now you run this the simulink model.
Now you can open a scope library to see data from the standard servo read library.
You can open a serial monitor on the second Arduino to see incoming serial data.
If you run your simulink model with disabled Enable External mode, I recommend to change period
time into 1 and simulation stop time 180.
Contact
If you have question related to this book, please contact me at aguskur@hotmail.com . My blog:
http://blog.aguskurniawan.net .
Source code can be downloaded on http://www.aguskurniawan.net/book/matlab_arduino.zip .