Arduino Programming Using MATLAB by Agus Kurniawan
Arduino Programming Using MATLAB by Agus Kurniawan
www.electronicbo.com
Table of Contents
Copyright
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
1.4 Testing
2. Setting Arduino Development for MATLAB
2.1 Getting Started
2.2 Setting up Arduino Development for MATLAB
2.3 Connecting Arduino Board to Computer
2.4 Hello Arduino: Blinking LED
3. Working with Digital I/O
3.1 Getting Started
3.2 Demo : LED and Pushbutton
3.2.1 Wiring
3.2.2 Writing a Program
3.2.3 Testing
4. Working with PWM and Analog Input
4.1 Getting Started
4.2 Demo Analog Output (PWM) : RGB LED
www.electronicbo.com
4.2.1 Wiring
4.2.2 Writing Program
4.2.3 Testing
4.3 Demo Analog Output Voltage: LED Brightness
4.3.1 Wiring
4.3.2 Writing a Program
4.3.3 Testing
4.4 Demo Analog Input: Working with Potentiometer
4.4.1 Wiring
4.4.2 Writing Program
4.4.3 Testing
5. Working with I2C
5.1 Getting Started
5.2 Writing Program
5.3 Demo 1: Scanning I2C
5.4 Demo 2: Reading Data from Sensor Based I2C
6. Working with SPI
6.1 Getting Started
6.2 Demo : SPI Loopback
7. Working with Servo Motor
7.1 Getting Started
7.2 Wiring
7.3 Writing a Matlab Program
7.4 Testing
8. Measuring and Plotting Sensor Data in Real-Time
8.1 Getting Started
8.2 Wiring
8.3 Writing a Program
8.4 Testing
Source Code
Contact
Preface
This book was written to help anyone want to develop Arduino board using MATLAB with Arduino supported. It
describes the basic elements of Arduino development using MATLAB.
Agus Kurniawan
Depok, September 2015
www.electronicbo.com
1. Preparing Development Environment
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.
www.electronicbo.com
Find it on http://arduino.cc/en/Main/Buy. The following is the list of Arduino store you
can buy
Because Arduino is an open-source hardware, people can build it. It’s called Arduino
compatible. Generally it’s sold in low prices.
www.electronicbo.com
The Arduino Due is a microcontroller board based on the Atmel SAM3X8E ARM Cortex-
M3 CPU. You can download the datasheet, http://www.atmel.com/Images/doc11057.pdf.
If you want to know about Arduino Due, I recommend to visit this website,
http://arduino.cc/en/Main/ArduinoBoardDue.
1.2 Electronic Components
We need electronic components to build our testing, for instance, Resistor, LED, sensor
devices and etc. I recommend you can buy electronic component kit.
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.
www.electronicbo.com
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
1.2.4 Arduino Sidekick Basic kit
Store website: http://www.seeedstudio.com/depot/arduino-sidekick-basic-kit-p-775.html
Alternative online store
http://www.amazon.com/Arduino-Sidekick-Basic-Kit-Version/dp/B007B14HM8/
http://www.exp-tech.de/Zubehoer/Arduino-Sidekick-Basic-Kit.html
1.3 Matlab
MATLAB Support Package for Arduino hardware enables you 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 is available for R2014a and later releases. It’s available on 32-bit
and 64-bit Microsoft® Windows®, 64-bit Mac OS, and 64-bit Linux®.
I will explain how to set up Matlab for Arduino development on chapter 2.
www.electronicbo.com
1.4 Testing
For testing, I used Arduino Uno R3 and Arduino Mega 2560 on OSX and Windows 10
platforms with Matlab 2015b.
This chapter explains how to work on setting up Arduino board on a computer and then,
access it from MATLAB.
www.electronicbo.com
2.1 Getting Started
In this chapter, we set up Arduino board development using MATLAB support package
for Arduino hardware. To set up this development, you must have MATLAB 2014a or
later and MATLAB account to verify while installing.
2.2 Setting up Arduino Development for MATLAB
In this section, we try to set up Arduino development for MATLAB. You can configure
MATLAB Support Package for Arduino hardware using MATLAB 2014a or later. We also
need internet connection to download this package.
Let’s start.
Run MATLAB application. Click Get Hardware Support Packages on Add-Ons icon
on toolbox.
www.electronicbo.com
Then, you get a dialog. Select Install from Internet.
If done, click Next> button.
Select Arduino on Support for. You should see two Arduino support packages. You can
select both.
If done, click Next> button.
You will be asked to logon with your MATLAB account. You should have MATLAB
license. Click Log In button.
www.electronicbo.com
You should the authentication dialog. Fill your account. After that, click Log In button.
If success, you should get a software license agreement. Checked I accept and then click
Next> button.
You will get confirmation. Click Next> button.
www.electronicbo.com
2.3 Connecting Arduino Board to Computer
Now you can connect Arduino board to computer. Then, verify which serial port is used
for Arduino board. On Mac platform, you type this command.
$ ls /dev/cu*
MATLAB will detect your Arduino board. You should detected Arduino board
information on Maltab Command Window.
www.electronicbo.com
2.4 Hello Arduino: Blinking LED
In this section, we build a blinking LED program using MATLAB. Arduino
Uno/Mega/Leonardo boards provides onboard LED which is connected on pin 13.
Let’s start to write our Blink program.
Firstly, you set working folder on MATLAB. You can change it on MATLAB IDE, see a
red arrow.
After that, you can get a script editor, shown in Figure below.
www.electronicbo.com
board = arduino();
led = 'D13';
for k=1:10
disp('turn on LED');
writeDigitalPin(board,led,1);
pause(1);
disp('turn off LED');
writeDigitalPin(board,led,0);
pause(1);
end
Save those scripts into a file, called blinking.m. Now you can run it.
>> blinking
In this chapter I’m going to explain how to work with digital I/O on Arduino board and
write a program for demo.
www.electronicbo.com
3.1 Getting Started
MATLAB support for Arduino board provides three functions which we can use on digital
I/O processing. The following is the functions:
To illustrate how to work with digital I/O, we build a simple program by utilizing LED
and pushbutton.
3.2 Demo : LED and Pushbutton
we build a program using LED and pushbutton. When we press a pushbutton, LED will
lighting. It’s a simple;).
3.2.1 Wiring
The following is hardware wiring:
www.electronicbo.com
A sample of hardware implementation is shown in Figure below.
configurePin(board,pushbutton,'DigitalInput');
disp('press Ctr-C to exit');
while 1
state = readDigitalPin(board,pushbutton);
writeDigitalPin(board,led,state);
disp(state);
pause(0.5);
end
end
function exitprogram(b)
clear b;
disp('program has exit');
end
3.2.3 Testing
Run this program by typing this command on Command Window on Matlab.
>> led_pushbutton
Press pushbutton. Then, you should see lighting LED. Press CTRL+C to exit program.
Program output:
www.electronicbo.com
LED is lighting while a pushbutton is pressed.
4. Working with PWM and Analog Input
This chapter explains how to work with Arduino Analog I/O using MATLAB.
www.electronicbo.com
4.1 Getting Started
MATLAB support for Arduino board provides five functions which we can use on analog
I/O processing. The following is the functions:
In this chapter, we try to access Arduino Analog I/O using MATLAB. There are three
scenarios for our cases:
Let’s start.
4.2 Demo Analog Output (PWM) : RGB LED
In this scenario we build a program to control RGB LED color using Arduino Analog
output (PWM).
Please be careful if you want to work with Arduino PWM. If you have Arduino Mega, you
will see PWM label so you obtain PWM pins easily but if you have Arduino Uno, it writes
DIGITAL (PWM ~). It means your PWM pins can be found on DIGITAL pins which pin
with ~, for instance, ~3,~5,~6,~9, ~10, ~11.
For Arduino Mega 2560, you can see PWM pins on picture below (see red arrow).
www.electronicbo.com
For Arduino Uno R3, you can see PWM pins as below.
RGB LED has 4 pins that you can see it on Figure below.
Note:
Pin 1: Red
Pin 2: Common pin
Pin 3: Green
Pin 4: Blue
4.2.1 Wiring
Firstly we implement RGB LED hardware. The following is a hardware schema.
www.electronicbo.com
For our testing, we configure the following PWM pins.
Arduino Mega 2560:
www.electronicbo.com
end
function exitprogram(b)
clear b;
disp('program has exit');
end
4.2.3 Testing
Upload and run the program. You should see several color on RGB LED.
>> led_rgb
4.3.1 Wiring
We connect a LED on PWM pin on digital pin 3. The following is my hardware wiring.
function exitprogram(b)
clear b;
disp('program has exit');
end
www.electronicbo.com
Save these scripts into a file, called led_brightness.m.
This program will set voltage value on PWM pin on digital pin 3 from 0 to 5 and then set
a value from 5 to 0 too.
4.3.3 Testing
Make sure Arduino board already connected to your computer. You can run the program
by typing this command.
>> led_brightness
4.4.1 Wiring
To understand Potentiometer, you see its scheme in Figure below.
www.electronicbo.com
You can connect VCC to Arduino board on VCC +5V pin. Vout to Arduino board Analog
input A0. In addition, GND to Arduino board GND. The following is hardware
implementation. I use slide potentiometer.
4.4.2 Writing Program
Firstly, create a program via MATLAB. To read analog input, we can use readVoltage()
function. Ok, Let’s write these scripts.
function [] = potentiometer()
board = arduino();
finishup = onCleanup(@() exitprogram(board));
disp('press Ctr-C to exit');
while 1
analog = readVoltage(board,'A0');
disp(['analog= ',num2str(analog)]);
pause(1);
end
end
function exitprogram(b)
clear b;
disp('program has exit');
end
www.electronicbo.com
5. Working with I2C
In this chapter we learn how to work with I2C on Arduino board using MATLAB.
5.1 Getting Started
The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early ’80s to allow
easy communication between components which reside on the same circuit board. TWI
stands for Two Wire Interface and for most marts this bus is identical to I²C. The name
TWI was introduced by Atmel and other companies to avoid conflicts with trademark
issues related to I²C.
I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock Line). You
can see I2C pins on Arduino board as follows:
MATLAB for Arduino support provides several functions to access I2C protocol. You can
www.electronicbo.com
read it on http://www.mathworks.com/help/supportpkg/arduinoio/i2c-sensors.html .
For testing, I used PCF8591 AD/DA Converter module with sensor and actuator devices.
You can find it on the following online store:
Amazon, http://www.amazon.com/PCF8591-Converter-Module-Digital-
Conversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-to-
analog-converter-module-w-dupont-cable-deep-blue-336384
Aliexpress, http://www.aliexpress.com/
In addition, you can find this device on your local electronics store/online store.
This module has mini form model too, for instance, you can find it on Amazon,
http://www.amazon.com/WaveShare-PCF8591T-Converter-Evaluation-
Development/dp/B00KM6X2OI/ .
This module use PCF8591 IC and you can read the datasheet on the following URLs.
http://www.electrodragon.com/w/images/e/ed/PCF8591.pdf
http://www.nxp.com/documents/data_sheet/PCF8591.pdf
In this chapter, we build a program to access sensor via I2C using Arduino software
on Arduino board.
www.electronicbo.com
5.2 Writing Program
We use PCF8591 AD/DA Converter as I2C source. You can connect PCF8591 AD/DA
Converter to Arduino board directly. In this demo, I use Arduino Uno.
The following is our wiring lab
clear board;
www.electronicbo.com
>> i2c_scan
On Command Window, you should see I2C address of sensor device. For instance, my
sensor was detected on 0x48.
function exitprogram(b)
clear b;
disp('program has exit');
end
www.electronicbo.com
6. Working with SPI
In this chapter I’m going to explain how to work with SPI on Arduino board using
MATLAB.
6.1 Getting Started
The Serial Peripheral Interface (SPI) is a communication bus that is used to interface one
or more slave peripheral integrated circuits (ICs) to a single master SPI device; usually a
microcontroller or microprocessor of some sort.
SPI in Arduino Uno board can be defined on the following pins:
www.electronicbo.com
MISO on Digital pin 50
SCK on Digital pin 52
SS on Digital pin 53
Save these scripts into a file, callsed spi_loopback.m. Then, run this program on
Command Windows of MATLAB.
>> spi_loopback
www.electronicbo.com
7. Working with Servo Motor
In this chapter I’m going to explain how to work with servo motor on Arduino board using
MATLAB.
7.1 Getting Started
Servo motor provides a shaft movement 360 degree. We can control this movement based
on its degree. In this scenario, you can use any DC motor (servo) that will be connected to
Arduino. I used a mini servo from Arduino Sidekick Basic kit.
The following is a picture of my mini servo motor.
www.electronicbo.com
To understand servo’s cables, you can identify as follows:
The next step we are going to build a MATLAB program with Arduino and servo motor.
7.2 Wiring
To build hardware implementation, you can connect servo motor to Arduino by following
configuration:
www.electronicbo.com
end
for pos = 1:-0.25:0
disp(['position: ',num2str(pos)]);
writePosition(motor,pos);
pause(1);
end
end
end
function exitprogram(b)
clear b;
disp('program has exit');
end
You should see servor motor is running from degree 0 to 180 and then back again from
degree 180 to 0.
A sample output program can be seen in Figure below.
8. Measuring and Plotting Sensor Data in Real-Time
In this chapter I’m going to explain how to read data from sensor devices and plot it on
graph in real-time.
www.electronicbo.com
8.1 Getting Started
This section has an objective to show how to work with a real-time on measurement. We
read data from sensor devices and display it on graph.
Let’s start!.
8.2 Wiring
We use the same wiring from section 5.2
www.electronicbo.com
8.3 Writing a Program
Now you run MATLAB and write these scripts.
function [] = sensing()
board = arduino();
disp('press Ctr-C to exit');
h = figure(1);
finishup = onCleanup(@() exitprogram(board,h));
PCF8591 = '0x48';
PCF8591_ADC_CH0 = '40'; % thermistor
PCF8591_ADC_CH1 = '41'; % photo-voltaic
PCF8591_ADC_CH3 = '43'; % potentiometer
i2c = i2cdev(board,PCF8591);
end
function exitprogram(b,h)
clear b;
close(h);
disp('program has exit');
end
www.electronicbo.com
8.4 Testing
You can run the program by typing this command.
>> sensing
www.electronicbo.com
Contact