Arduino Programming Using MATLAB Agus Kurniawan PDF
Arduino Programming Using MATLAB Agus Kurniawan PDF
Arduino Programming Using MATLAB Agus Kurniawan PDF
Agus Kurniawan
Depok, September 2015
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.
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. Its called Arduino
compatible. Generally its sold in low prices.
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.
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. Its 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.
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.
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.
Lets start.
Run MATLAB application. Click Get Hardware Support Packages on Add-Ons icon
on toolbox.
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.
MATLAB will detect your Arduino board. You should detected Arduino board
information on Maltab Command Window.
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.
Lets 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.
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 Im going to explain how to work with digital I/O on Arduino board and
write a program for demo.
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. Its a simple;).
3.2.1 Wiring
The following is hardware wiring:
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:
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.
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:
Lets 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).
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.
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
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.
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, Lets 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
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 IC. The name
TWI was introduced by Atmel and other companies to avoid conflicts with trademark
issues related to IC.
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
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.
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;
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
In this chapter Im 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:
Save these scripts into a file, callsed spi_loopback.m. Then, run this program on
Command Windows of MATLAB.
>> spi_loopback
In this chapter Im 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.
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:
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 Im going to explain how to read data from sensor devices and plot it on
graph in real-time.
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.
Lets start!.
8.2 Wiring
We use the same wiring from section 5.2
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