Professional Documents
Culture Documents
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
4.2 Digital Sources
4.3 Simulink with Arduino Digital I/O
4.3.1 Digital Input
4.3.2 Digital Output
4.3.3 Logical Operator
4.3.4 Pulse Generator
4.3.5 Configuring Hardware
4.4 Testing
5. Simulink with Arduino Analog I/O
5.1 Simulink with Arduino Analog Input
5.1.1 Reading Temperature Analog Sensor
5.1.2 Building Simulink Model
5.1.3 Executing Simulink Model
5.2 Simulink with Arduino Analog Output
5.2.1 Controlling RGB LED Color
5.2.2 Building Simulink Model
5.2.3 Executing Simulink Model
6. Simulink with Arduino Serial
6.1 Arduino Serial Communication
6.2 Configuring Arduino
6.3 Building a Simulink Model
6.3.1 Serial Transmit
6.3.2 Serial Receive
6.3.3 Counter Limited
6.3.4 Data Type Conversion
6.4 Testing
7. Simulink with Arduino and Servo Motor
7.1 Servo Motor
7.2 Building A Simulink Hardware
7.3 Building A Simulink Model with Arduino and Servo Motor
7.3.1 Standard Servo Write
7.3.2 Standard Servo Read
7.3.3 Counter Limited
7.3.4 Serial Transmit
7.4 Testing
Contact
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
1. Preparing Development Environment
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
You can buy Fritzing Starter Kit with Arduino UNO or Fritzing Starter Kit with Arduino Mega.
1.2.3 Cooking-Hacks: Arduino Starter Kit
Store website: http://www.cooking-hacks.com/index.php/shop/arduino/starter-kits/arduino-starter-
kit.html
http://www.exp-tech.de/Zubehoer/Arduino-Sidekick-Basic-Kit.html
For Windows platform, you can download setup file and install it.
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.
Now run your Matlab and then click Add-Ons. You can see the menu as below.
Select Get Hardware Support Packages.
You may log in to MathWorks Account to continue installation process. Click Log In and then entry
your username and password.
After that, click Next button.
Check Arduino and then click Next button.
If you see Matlab Simulink Library on Arduino, you can see Digital Output library. This library
generates digital output on Arduino. We will use this library on our model to turn on and off our LED.
3.2 Creating Arduino Simulink
Now we are ready to build a "hello world" Matlab Simulink with Arduino. Follow all steps.
On Home, click + New icon. Then you will obtain a menu as below.
Select Simulink Model.
On the Simulik Model dialog, click menu Tools -> Library Browser.
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.
For our scenario, hello world, we create a simulink as below.
You need three libraries as follows
You test and validate your model. Make sure you select Normal. Then, click Arrow, shown in Figure
below.
If success, you don't see the error dialog.
To run this model to Arduino, you must configure. How? Click menu Tools -> Run on Target
Hardware -> Prepare to Run.
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.
void setup() {
pinMode(signal, OUTPUT);
}
void loop() {
digitalWrite(signal, HIGH);
delay(2000);
digitalWrite(signal, LOW);
delay(1000);
digitalWrite(signal, HIGH);
delay(1000);
digitalWrite(signal, LOW);
delay(500);
}
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
Click OK if finished.
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.
5. Simulink with Arduino Analog I/O
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:
In this scenario, we use cheap temperature sensor device, thermistor. Thermistor can be used to
measure temperature. I have thermistor 503 from Arduino Sidekick Basic kit. Thermistor 508 can be
shown in Figure below.
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.
You also can use Matlab function that you can create to change a Subsystem.
Note:
Pin 1: Red
Pin 2: Common pin
Pin 3: Green
Pin 4: Blue
Now we can start to build a simulink and hardware implementation.
Note:
On PWM library you can set Arduino PWM pin. In this case, I used Arduino PWN pins: 6, 5, and 4.
On Repeating Sequence for PWM pin 6, you configure as follows.
Configure Repeating Sequence for PWM pin 5 as below.
Finally you can configure Repeating Sequence for PWM pin 4 as follows.
If done, save your a simulink model.
If your model doesn't has any error while deploying to Arduino, you will see RGB LED shows some
colors based on RGB value. The following is a sample output for Arduino and RGB LED.
You also can see the output of red values shown on Figure below.
The output of green values can be seen as below.
The last you can see the output of blue values as below.
6. Simulink with Arduino Serial
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.
int data = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
data = Serial.read();
Serial.write(data);
}
}
The following is a code implementation on Arduino application.
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.
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.
7. Simulink with Arduino and Servo Motor
This chapter explains how to program simulink using 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.
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
To work with standard servo write, we can give input between 0 and 180 to this library.
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 .