Arduino Programming Using MATLAB - Agus Kurniawan
Arduino Programming Using MATLAB - Agus Kurniawan
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
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
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. Its called Arduino
compatible. Generally its sold in low prices.
http://arduino.cc/en/Main/ArduinoBoardMega2560 .
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.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.
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.
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.
After done, you will be asked to configure Arduino board. Select Arduino and then, click
Next> button.
The program will check if your platform needs Arduino driver or not. If youre working
on Linux and Mac, you dont need a driver. You need to install Arduino driver if youre
working on Windows platform. Click Next> button if done.
MATLAB will detect your Arduino board. You should detected Arduino board
information on Maltab Command Window.
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
disp('close Arduino board');
clear board;
Save those scripts into a file, called blinking.m. Now you can run it.
>> blinking
This error occurs because we create arduino object while there is existing arduino object.
We cant use multiple arduino object. Delete existing arduino on Workspace Window. See
a red arrow on above Figure.
We can clear our arduino object usage using clear syntax.
Now you can run the program again. The following is a sample output of blinking
program.
In this chapter Im going to explain how to work with digital I/O on Arduino board and
write a program for demo.
To illustrate how to work with digital I/O, we build a simple program by utilizing LED
and pushbutton.
3.2.1 Wiring
The following is hardware wiring:
LED is connected to Arduino digital pin 9
Pushbutton is connected to Arduino digital pin 8
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:
This chapter explains how to work with Arduino Analog I/O using MATLAB.
In this chapter, we try to access Arduino Analog I/O using MATLAB. There are three
scenarios for our cases:
Controlling RGB LED
Controlling LED brightness
Reading Analog input using Potentiometer
Lets start.
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.
write_rgb(board,1,0,1); % green
pause(1);
disp('blue');
write_rgb(board,1,1,0); % blue
pause(1);
disp('yellow');
write_rgb(board,0,0,1); % yellow
pause(1);
disp('purple');
write_rgb(board,0.3,1,0.3); % purple
pause(1);
disp('aqua');
write_rgb(board,1,0,0); % aqua
pause(1);
end
end
% testing for Arduino Uno
function write_rgb(board,r,g,b)
writePWMDutyCycle(board,'D3',r);
writePWMDutyCycle(board,'D5',g);
writePWMDutyCycle(board,'D6',b);
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.
while 1
for k = 0:5
writePWMVoltage(board,'D3',k);
pause(1);
end
for k = 5:-1:0
writePWMVoltage(board,'D3',k);
pause(1);
end
end
end
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.3 Testing
To run the program, you can type this command.
>> potentiometer
In this chapter we learn how to work with I2C on Arduino board using MATLAB.
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-DigitalConversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-toanalog-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-EvaluationDevelopment/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.
On Command Window, you should see I2C address of sensor device. For instance, my
sensor was detected on 0x48.
If success, you should see values for Thermistor, Photo-voltaic, and potentiometer.
In this chapter Im going to explain how to work with SPI on Arduino board using
MATLAB.
function exitprogram(b)
clear b;
disp('program has exit');
end
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.
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:
Red cable is be connected to 5V
Black or brown cable is be connected to GND
The rest (yellow or orange cable) is be connected to Arduino PWM pin. I used pin 10
for Arduino Uno R3 or Arduino Mega 2560
7.4 Testing
Now you can run the program on Command Window from MATLAB.
>> servo_motor
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.
In this chapter Im going to explain how to read data from sensor devices and plot it on
graph in real-time.
8.2 Wiring
We use the same wiring from section 5.2
write(dev,config);
read(dev, 1);
out = read(dev, 1);
adc = out;
end
function exitprogram(b,h)
clear b;
close(h);
disp('program has exit');
end
8.4 Testing
You can run the program by typing this command.
>> sensing
Source Code
Contact