Home » » Data Acquisition and Processing

Data Acquisition and Processing

Written By Unknown on Thursday 9 May 2013 | 05:15

Data Acquisition and Processing through MATLAB AND ARDUINO

In previous POST, you used MATLAB to analyze data and the Arduino with various sensors to gather information from the environment. In this POST, you will learn how to interface the Arduino with MATLAB ONCE AGAIN.

One of the most useful things about the Arduino is how widely it is used. The Arduino platform is so popular that even companies such as MathWorks, which produces MATLAB, have developed software to support it. In the first part of the lab, you will use the MATLAB Arduino support package to connect the Arduino to MATLAB, allowing you to obtain and plot accelerometer data directly from within the MATLAB command window.
In the second part of the lab, you will use MATLAB to do some simple, real-time processing on the accelerometer data that you gather. This will have important applications for the final projects.

Procedure:


1. Interfacing Arduino with MATLAB and plotting accelerometer data
- Go to http://www.mathworks.com/matlabcentral/fileexchange/32374 to download the MATLAB support package for Arduino. Click the blue “Download Submission” button on the right.
- Extract the “ArduinoIO” folder and save to C:/user/MATLAB.
- Open MATLAB: Start  All Programs  Mathematics  MATLAB R2012a
- On the left in the Current Folder tab, right click “ArduinoIO” and select Add to Path  Selected Folders and Subfolders (Figure 1). This makes the files accessible to MATLAB.

Image shows Adding folder to path in MATLAB (note that your folder will be called ArduinoIO, not MATLAB_Arduino)


- Connect the Arduino to the computer and open the Arduino IDE. Check which COM port the Arduino is using and select that option (Tools  Serial Port). Also select the correct board (Tools  Board).
- Go to Files  Open  C:/user/MATLAB/ArduinoIO/pde/adiosrv/adiosrv.pde to open the .pde file. This program tells the Arduino to send data to and receive data from the computer such that it can be controlled by MATLAB. Upload this code to the Arduino.
- In the MATLAB command window, create a new Arduino object a by entering

a = arduino(‘COMX’);
where X should be replaced by the number of the COM port that the Arduino is using. Once the Arduino has successfully connected, you should see the following text:


Attempting connection .............
Basic I/O Script detected !
Arduino successfully connected !


This object can now utilize all of the functions specified by the arduino.m library so that you can perform digital and analog I/O functions.
Now you will write a script to read and plot analog values from an accelerometer. A MATLAB script file (also known as an m-file because MATLAB script files always have the file extension .m) is a method for executing a series of MATLAB instructions without typing all of the commands into the Command Window.
- To create a new script, go to File  New  Script, or press Ctrl + N.

Type the following into your script:


a.pinMode(16,’input’);
a.pinMode(17,’input’);
- To configure pins 18 and 19 as outputs, type the following lines in your script:
a.pinMode(18,’output’);
a.pinMode(19,’output’);
- Since pin 18 corresponds to the ground pin on the accelerometer, we must have it output 0V. Likewise, we must output 5V on pin 19, which corresponds to Vcc.
a.digitalWrite(18,0);
a.digitalWrite(19,1);
You will now record data from the x-axis and y-axis readings of the accelerometer into dynamic vectors and plot this data in real time. Dynamic vectors are vectors that do not have a fixed length; they grow as more data is added to them.
First create two dynamic vectors to store the data from the x- and y-axes:
xval = zeros();
yval = zeros();
Now you will create a for-loop that will read values from the analog pins and plot this data in real-time. Begin a for-loop that increments an integer i from 1 to 500 by typing:
for i = 1:500
At each instance of i, you must read the accelerometer values and store them into the vectors.
xval(i) = a.analogRead(3);
yval(i) = a.analogRead(2);
Now you can create two separate plots to show this data using the subplot function in MATLAB. Copy the following code into your script (within the for-loop):
subplot(2,1,1)
plot(xval, 'b');
title('x');
subplot(2,1,2)
plot(yval, 'r');
title('y');
In order to plot this data in real-time, you need to add a pause (similar to the Arduino delay function) between success iterations of the for-loop. Add the following line to pause for five milliseconds between iterations:
pause(.005);
Finally, end the for-loop by typing:
End

- Now, click the “Run” button (green arrow on toolbar) or go to Debug  Save File and Run. Name the file “aplot.m” and save it under the MATLAB folder (C:/user/MATLAB/). A plot should now pop up and you should see the x- and y-values.





2 comments:

  1. Its not my first time to pay a visit this website, i am visiting this site dailly and obtain nice information from
    here everyday.

    my web-site; Las Vegas GE Appliance Parts

    ReplyDelete
  2. nice tutorial, but it could be wrote more neatly

    ReplyDelete

HOST PARTNER