You are on page 1of 6

University of Pennsylvania Department of Electrical and Systems Engineering ESE 111 Intro to Elec/Comp/Sys Engineering

Lab 5 Introduction to Data Acquisition and Processing


Introduction: In previous labs, you used MATLAB to analyze data and the Arduino with various sensors to gather information from the environment. In this lab, you will learn how to interface the Arduino with MATLAB. 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 project for the lab, as well as other classes you will take in the future. 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.

Created by Nick Howarth (EE 13) and Sam Wolfson (EE 13) Last updated: October 8, 2012

Figure 1: 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.

First, you must specify which I/O pins are inputs and which are outputs, just as you would in a regular Arduino script. Recall that we can refer to analog pins 0-5 as pins 14-19. If your accelerometer is connected as in Figure 2, then you will need to configure pins 18 and 19 as outputs and pins 15-17 as inputs (z-axis on pin 15, y-axis on pin 16, and x-axis on pin 17).

Figure 2: Acclerometer connection to Arduino analog pins

To configure pins 16 (y-axis) and 17 (x-axis) as inputs (we will not use the z-axis), 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 vary as you tilt and shake the accelerometer.

2.

Using a moving average filter to smooth the signal

Signals from sensors are often messythey contain spurious peaks that can make it difficult to isolate the event that you are actually interested in. One way to smooth out a signal is to use what is known as a moving average filter. (You will learn more about this technique in ESE 210.) The moving average works by creating a new signal composed of the average of the points of the original signal. The simplest implementation is a two-step moving average (MA2). Imagine a signal A which is a vector of n data points, a1an:

After applying a moving average filter, the resulting signal B is a vector of n data points:

The first entry in B is the same as the first entry in A. The second entry in B becomes the average of the first and second entries in A. In general, the kth entry of B is the average of the kth and the (k-1)th entries of A; that is,

For example, suppose we have a signal A with the values:

If we apply a two-step moving average filter to this signal, the result B will look like:

Now lets adapt the MATLAB program that we used to take accelerometer data so that it also plots a smoothed version of the data by running it through a two-step moving average filter. Create a new script (call it MAtest.m) and copy the following code:

a.pinMode(15,'input'); a.pinMode(16,'input'); a.pinMode(17,'input'); a.pinMode(18,'output'); a.pinMode(19,'output'); a.digitalWrite(18,0); a.digitalWrite(19,1); xval = zeros(); xave = zeros(); xval(1) = a.analogRead(3); xave(1) = xval(1); pause(.001); for i = 2:500 xval(i) = a.analogRead(3); xave(i) = (xval(i)+xval(i-1))/2; subplot(2,1,1) plot(xval, 'b'); title('x'); subplot(2,1,2) plot(xave) title('x average') pause(.001); end

Note that this code is very similar to the one you used earlier, with the addition of a new vector xave instead of the yval vector. Every ith point in xave is generated by averaging the ith and (i-1)th point in xval. Note also that the FOR loop runs from 2 to 500 instead of 1 to 500. In MATLAB, the first entry in a vector is entry 1, not entry 0 (as in Java or C). Since each entry of xave relies on both the current entry and the previous entry of xval, if the FOR loop went from 1:500 xval would have to calculate the average of xval(1) and xval(0). xval(0) does not exist in MATLAB, so there would be an error. To avoid this, we calculate xval(1) and xave(1) outside of the FOR loop, and loop from 2 to 500. Run this program, and observe the differences between xave and xval as you move the accelerometer around. What happens if you process xave with another moving average filter? Hint: you will need plot xval on subplot(3,1,1), xave on subplot(3,1,2), and xave2 on subplot(3,1,3). What happens if you use more than 2 points in your moving average filter? (Try a 3-step moving average.) Note that for an m-step moving average,

You might also like