Anda di halaman 1dari 13

Lab

 1:  Matlab-­‐‑Arduino  Communication  


The  goal  of  this  lab  is  to  set-­‐up  the  interface  that  allows  Matlab  to  command  and  control  the  Arduino.  
This  is  useful  because  Matlab  is  a  much  more  user  friendly  interface  for  analyzing  data.  In  the  following  
labs  we  will  be  using  Matlab  to  plot  data  generated  by  the  Arduino.    

 This  lab  will  teach  you  how  to  set-­‐up  serial  communication  between  the  Arduino  and  Matlab.    In  order  
to  do  this  you  first  need  code  running  on  the  Arduino  that  will  communicate  with  Matlab.    

Arduino  Set-­‐‑up:  
 

In  this  section  we  will  install  a  program  onto  the  Arduino  that  will  send  information  to  Matlab.  The  
Arduino  and  Matlab  communicate  using  the  I2C  (Inter-­‐Integrated  Circuit)Bus.  In  order  to  do  this  you  
need  to  import  the  "Wire"  library,  found  here:  
https://github.com/arduino/Arduino/blob/master/libraries/Wire  .  This  file  allows  you  to  use  I2C  to  
communicate  between  the  Arduino  board  and  Matlab.    

You  will  need  both  the  Wire.cpp  and  the  Wire.h  files.  If  you  cannot  directly  download  the  files,  try  
copying  the  text  and  putting  it  into  a  text  document.  Then  save  the  documents  as  .h  and  and  .cpp  files.  
Once  you  have  downloaded  (or  created)  the  files,  you  will  need  to  put  them  into  the  Arduino  library.  To  
find  out  where  the  libraries  are  located,  right  click  on  the  Arduino  program  and  look  at  its  properties  
(see  the  screenshot  below).  The  highlighted  address  is  where  the  libraries  are  located.    
 
Figure  1-­‐  Screenshot  of  Arduino  properties.  Highlighted  address  shows  location  of  libraries.    

Save  the  Wire.cpp  and  Wire.h  files  in  the  library  inside  a  folder  called  Wire.  Once  the  libraries  are  in  
place,  you  can  reference  them  from  any  function  or  class  by  using  the  #include  argument.  See  syntax  
below.    

#include  <Wire.h>    

Only  the  .h  file  needs  to  be  included  because  it  references  the  .cpp  file.    

Once  you  have  included  the  required  libraries,  you  are  ready  to  write  the  code  for  the  Arduino.  A  very  
basic  example  of  code  that  will  send  the  numbers  0,1,2,3  and  4  continuously  to  Matlab  is  shown  in  
Appendix  A.  If  you  want  to  check  the  output  of  this  function  before  sending  the  data  to  Matlab,  
comment  out  the  establishContact()  function  and  open  the  Serial  port  (the  magnifying  glass  on  the  top  
RH  corner  of  the  file).  You  should  see  the  following  output.    
 
Figure  2-­‐  Serial  port  output  when  Arduino  code  in  Appendix  A  is  uploaded  

Remember  the  Arduino  needs  to  be  connected  to  the  computer  in  order  to  upload  the  sketch.  If  you  
look  at  the  top  of  the  serial  port  screenshot  above,  you  will  notice  it  says  COM8.  That  is  because  this  
Arduino  is  connected  to  communication  port  8.  This  port  will  be  different  for  each  Arduino.  To  check  
which  COM  port  your  Arduino  is  connected  to,  go  to  Control  Panel  -­‐  System  -­‐  Device  Manager  -­‐  
Ports(COM  &  LPT)-­‐  Arduino  Uno.  Note  the  example  below  is  connected  to  COM8  which  is  why  COM8  is  
used  as  the  connection  port  in  the  code.    
 

 
Figure  3-­‐  Arduino  Uno  COM  connection  port  

Now  you  should  have  your  Arduino  properly  connected  and  your  serial  communication  program  
running.  Next  it's  time  to  set-­‐up  the  Matlab  code.    

Matlab  Set-­‐‑up:    
 

If  you  simply  want  to  read  raw  data  from  the  Arduino  and  manipulate  it  in  Matlab,  the  code  in  Matlab  is  
very  simple.    However,  if  you  want  to  actually  control  the  Arduino  from  Matlab,  the  code  is  more  
complicated.  We  will  go  over  this  in  the  next  section.    For  now,  let's  just  look  at  how  to  read  the  five  
values  sent  from  the  Arduino  code  (in  Appendix  A)  above.    The  code  required  to  do  this  is  found  in  
Appendix  B.  Notice  that  it  creates  a  new  serial  port  connection  which  is  connected  to  COM8.  It  also  
defines  the  terminator  as  a  new  line.  This  means  that  Matlab  will  read  data  until  it  sees  a  newline  
(println  function  in  the  Arduino)  so  the  data  sets  will  be  separated  by  new  lines.  Since  the  Arduino  runs  
in  an  infinite  loop,  the  data  sets  are  also  infinite,  that  is  why  in  this  code  data  is  only  collected  for  five  
seconds  (numSec  =  5).    

Download  this  code  onto  Matlab  and  hit  run.  You  should  see  the  following  in  the  Command  Window.  

 
Figure  4-­‐  Command  window  output  when  running  Arduino  communication  function  

This  Matlab  program  is  slightly  different  than  a  regular  program  because  it  stores  all  the  variables  
declared  inside  the  function  on  the  command  window  workspace.  If  you  type  'v'  into  the  command  
window  you  should  see  the  same  output  as  from  the  Arduino  serial  port.  You  do  not  need  to  use  this  
type  of  function  to  read  data  from  the  Arduino,  but  it  makes  it  easy  to  see  all  the  data  without  having  to  
have  your  function  output  everything.    

Now  you  have  set  up  serial  I2C  communication  between  the  Arduino  and  Matlab.  However,  if  you  want  
to  make  the  Arduino,  say  turn  on  an  LED  you  would  have  to  alter  the  Arduino  sketch.  The  section  below  
shows  you  how  to  directly  control  the  Arduino  from  Matlab.  

Arduino  Control  via  Matlab  


 

In  order  to  set-­‐up  the  Arduino  communication,  we  will  be  using  object  oriented  Matlab  programming.  
The  first  step  is  to  download  the  ArduinoIO  folder.  This  contains  the  'arduino'  class  (in  Matlab)  which  
defines  the  arduino  object  and  creates  all  the  functions  that  control  the  Arduino  from  Matlab.  Once  this  
is  installed  you  will  be  able  to  control  the  Arduino  directly  from  Matlab  instead  of  having  to  upload  a  
different  Arduino  sketch  each  time  you  want  to  modify  the  code.      

The  ArduinoIO  folder  can  be  downloaded  at:  


http://www.mathworks.com/matlabcentral/fileexchange/32374-­‐matlab-­‐support-­‐package-­‐for-­‐arduino-­‐
aka-­‐arduinoio-­‐package  

Right  click  on  the  Download  Submission  button  on  the  top  RHS  of  the  screen.  Select  save  file.    

Once  it  is  finished  downloading,  open  the  folder.  It  should  contain  2  files:  license  and  ArduinoIO.  Drag  
the  ArduinoIO  folder  into  your  Matlab  folder.  It  should  now  appear  on  your  Matlab  file  list  in  grey.  Right  
click  on  the  folder  and  select  Add  to  Path-­‐Selected  Folders  and  Subfolders  (see  below).    

 
Figure  5-­‐  Adding  Arduino  folder  to  Matlab  path  

This  folder  contains  all  the  necessary  code  to  control  the  Arduino  from  Matlab,  however  it  does  not  
include  I2C  communication.  If  you  want  to  send  data  from  the  Arduino  to  Matlab,  you  will  need  to  
download  the  I2C  ArduinoIO  package  found  here:  

http://www.mathworks.com/matlabcentral/fileexchange/41602-­‐i2c-­‐block-­‐for-­‐arduinoio-­‐simulink-­‐
package  
Once  both  of  those  are  downloaded  we  are  ready  to  begin.    

To  control  the  Arduino  from  Matlab,  the  Arduino  needs  to  be  running  the  'adiosrv'  sketch.    This  sketch  
can  be  found  in  I2C_read_block_for_ArduinoIO_package  folder  than  you  downloaded  above.  Open  that  
sketch  and  upload  it  to  the  Arduino.  Once  you  have  uploaded  this  sketch,  the  Arduino  will  continue  to  
run  it  until  a  new  sketch  is  uploaded.    It  enables  Matlab  to  control  the  Arduino  directly  so  you  should  no  
longer  need  to  use  the  Arduino  program.  

Next,  open  the  install_arduino.m  file  (found  in  the  ArduinoIO  folder)  and  run  it  on  Matlab.  This  installs  
the  Matlab  support  package  for  the  Arduino  and  only  needs  to  be  done  once.    

The  arduino.m  file  is  the  Arduino  class.  It  initializes  the  arduino  object  that  connects  to  a  specified  port  
(COM8  in  this  lab)  and  defines  all  the  functions  required  to  control  the  Arduino.  These  are  the  same  
functions  that  are  defined  in  the  Arduino  system  (digitalWrite,  digitalRead,  pinMode  etc),  but  now  they  
will  work  from  Matlab.    

Since  the  original  arduino.m  file  does  not  have  I2C  communication,  you  will  need  to  replace  your  original  
arduino.m  file  with  the  new  arduino.m  file  found  in  the  I2C_read_block_for_ArduinoIO_package  folder.    

Now,  let's  check  to  see  you  did  that  correctly  by  creating  an  Arduino  object.    

Open  the  Matlab  command  line  and  type  in  the  following  code:  

Example  =  arduino('COM8')    %  This  creates  an  instance  of  the  arduino  object  called  example  which  is    
                     %    connect  to  COM  port  8.    

You  should  get  the  following  output  when  you  enter  this  line  into  Matlab  command  window:  
 
Figure  6-­‐  Output  when  creating  an  instance  of  arduino  

You  can  now  call  any  of  the  functions  defined  inside  the  arduino  object  because  you  have  created  an  
instance  of  that  object.    To  reference  those  functions  you  must  use  example.function().  This  is  because  
the  function()  exists  inside  example  (your  instance  of  the  arduino  class).    

Here  is  some  code  that  will  turn  on  an  LED  if  you  connect  it  to  pin  4  on  your  arduino  (assuming  you  have  
already  created  an  instance  of  the  arduino  class  called  example):  

>>  example.pinMode(4,'output')   %sets pin 4 to output

>>  example.digitalWrite(4,1)     %sets pin 4 to HIGH which turns on the LED  

>>  example.digitalWrite(4,0)     %sets pin 4 to LOW which turns off the LED  

 
Object  oriented  programming  is  complicated,  but  once  you  understand  it,  it  can  make  your  overall  code,  
much  simpler.  A  class  can  contain  multiple  objects  and  functions  which  makes  it  useful  for  organizing  
more  complicated  code.  Think  of  the  class  as  a  car.  The  objects  inside  car  would  be  door,  horn,  steering  
wheel  etc.  The  functions  of  car  could  be  accelerate,  decelerate,  stop  etc.  All  those  functions  can  be  
called  on  car.  so  to  call  those  functions  you  would  use  the  following  syntax:  

car.accelerate();   %accelerate function is inside car


 

Now  let's  assume  that  each  of  the  objects  also  contains  functions.  For  example,  the  horn  object  contains  
a  function  called  honk.  To  reference  this  function  we  must  use  the  following  syntax:  

car.horn.honk();     %honk function is inside horn which is inside car  

Now  that  you  understand  how  classes  work  (hopefully),  let's  create  our  own  class.  We  will  call  it  
ArduinoSystem.  It  will  contain  the  arduino  object  as  well  as  a  few  functions  that  we  define.  The  source  
code  to  create  the  ArduinoSystem  class  can  be  found  in  Appendix  C.    Fill  in  this  code  so  that  it  contains  3  
functions:  LED_ON,  LED_OFF  and  LED_BLINK.  That  turn  on,  turn  off  and  blink  three  LEDs.    

   

Lab  Deliverables    
 

Print  off  the  Matlab  code  of  the  completed  ArduinoSystem  class  as  well  as  a  screenshot  of  the  Matlab  
command  prompt  where  you  call  LED_ON,LED_OFF  and  LED_BLINK,  pinMode,  digitalRead  and  
digitalWrite.    If  you  are  unsure  about  the  syntax  or  inputs  of  the  functions,  look  in  the  arduino  object  
where  they  are  defined.  A  write-­‐up  is  not  required  for  this  lab.    

   
 

Appendix  A  

 
 

Appendix  B  
clear% Communications MatLab <--> Arduino
% Matlab file 1 for use with Arduino file 1
clc;
clear all;
v =[];
numSec = 5;

s1 = serial('COM8'); % define serial port


s1.BaudRate=9600; % define baud rate
set(s1, 'terminator', 'LF'); % define the terminator as println (it is a
new line)
fopen(s1);

%reads value in arduino write buffer. If it reads an A then it writes an A


%to the arduino read buffer and the establishContact routine in the arduino
%code terminates because an connection has been set up.
try % use try catch to ensure fclose
% signal the arduino to start collection
w=fscanf(s1,'%s'); % must define the input % d or %s, etc.
if (w=='A')
display(['Collecting data']);
fprintf(s1,'%s\n','A'); % establishContact just wants
% something in the buffer
end

i=0;
t0=tic;
while (toc(t0)< numSec)
i=i+1;
%t(i)=toc(t0);
%t(i)=t(i)-t(1);
v(i,:)=fscanf(s1,'%d,%d,%d,%d,%d');
% must define the input %d or %s, etc. Data is coming in as text (always)

end
fclose(s1);
% if you need precise timing
% you better get it from the
% arduino (see file 2)
catch me
fclose(s1); % always, always want to close s1
end
 

 
 

Appendix  C  
classdef ArduinoSystem < handle
%Class called ArduinoSystem that contains an Arduino object and some
%functions that can be used on the arduino object (can also contain
%other objects like motors, sensors etc)

properties
%properties of the class (global variables)
ard %an object called ard
end

methods
%% **************************** CONSTRUCTOR ******************************
%creates an instance of ArduinoSystem called obj
function obj = ArduinoSystem(comPort)
%creates an instance of arduino called ard which exists inside
%obj (an instance of ArduinoSystem)
obj.ard = arduino(comPort);

end% end of constructor now you have created an ArduinoSystem called


obj
%% ****************************** METHODS ********************************
%creates a function called LED_ON which exists inside the
%ArduinoSystem class therefore it needs to import the object
%because this function is used on that object (i.e. the
%ArduinoSystem)
function LED_ON (obj)
%Code goes here
end

function LED_OFF (obj)


%Code goes here
end

function LED_DIMMER (obj)


%Code goes here
end

end %end class


 

Anda mungkin juga menyukai