Final Project – Networked Pedometer Part 1: Counting Steps

University of Pennsylvania
Department of Electrical and Systems Engineering
ESE 111 – Intro to ESE
Final Project – Networked Pedometer
Part 1: Counting Steps
Introduction:
In Lab 5, you used a MATLAB Arduino support package to observe the output of the
accelerometer in real time. In this lab, you are going to use the MATLAB code provided to you
by your TAs to determine how to use the accelerometer to track steps. You will then use this
basic algorithm to write an Arduino program which counts the number of steps that you take.
Materials:






Arduino
Breadboard shield
Accelerometer
DIP switch
1 LED
2 1-kΩ resistors
Procedure:
Observing Accelerometer Data in MATLAB
-
-
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 Sam Wolfson (EE ’13) and Nick Howarth (EE ’13)
Last updated: November 4, 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.
-
Connect the accelerometer to the Arduino with the body facing in as shown in Figure 2.
Figure 2 - Acclerometer connection to Arduino analog pins
-
Download the MATLAB code (stepCounter.m) from the ESE 111 website, save it to the
MATLAB directory, and open the file in MATLAB.
You may notice that this MATLAB code is slightly different from what you used previously.
Rather than observe the x, y, and z components of the acceleration separately, as you did before,
you will use this code to observe how the total magnitude of the acceleration vector changes with
motion.
As you may recall from math or physics class, the magnitude of a three-dimensional vector (such
as the acceleration vector) is given by:
,
where a, b, and c are the components of the vector in the x, y, and z directions.
For our acceleration vector in particular, the total magnitude is given by:
,
where , , and represent the average values of the components of the acceleration when the
accelerometer is at rest.
The MATLAB code computes the values of , , and by sampling xval, yval, and zval 50
times and then finding the average value of the resulting vectors. Check out lines 18-37 of the
MATLAB code and make sure that you understand how it works. You can think of this step as
the calibration of your pedometer.
The rest of the MATLAB code is very similar to what you have seen previously. The only
difference is that instead of plotting the individual components, the total magnitude of the
acceleration is calculated using the equation above and then plotted in real time.
Determining Step Thresholds
Before you program the Arduino to count the number of steps you take, you are going to have to
see what the accelerometer data actually looks like when you take a step. The MATLAB code is
very useful for this, because the plot should make steps easy to see.
-
-
Hold the Arduino to your hip or upper thigh and run the code (click the green arrow
button on the toolbar).
When you see the plot appear, take a few steps—try to walk as naturally as possible
(despite being constrained by the USB cable).
You should see some very clear peaks on the plot correlating to steps that you took. Be
sure to think about the difference between steps taken with your right foot and steps taken
with your left foot.
Figure out roughly which value of acceleration distinguishes a peak. Experiment until
you have a value that you think will work.
Keep in mind that everyone walks differently, so the data output will look different for
everyone who tries it. Make sure to be consistent with how you position of the Arduino.
Counting Steps with Arduino
Your task is to create a pedometer using the Arduino. You will write a program that will allow
the Arduino to calculate the magnitude of the acceleration vector and use that value to detect and
count steps. The program should be similar to the MATLAB code. The specifications are as
follows:
1) Your pedometer should use an on-off switch (use a DIP switch) to turn the step counting
function on and off. Remember that you need to connect it over the cove in the
breadboard in either active-high or active-low configuration.
2) When the switch is first turned on, the pedometer should call the posCal() function to
calibrate itself (i.e., calculate , , and ). Once calibration is complete, print , , and
to the serial monitor.
3) You should have an LED connected to pin 13 which turns on at the beginning of
calibration and turns off after the calibration is complete.
4) After calibration is complete, you continually read the accelerometer data, calculate the
total acceleration vector magnitude, call the stepDetect()function to check if a step
has occurred, and print the total number of steps to the serial monitor.
5) Every time a step occurs, flash the LED connected to pin 13.
6) The step counter function should terminate when the switch is turned off.
7) If you have time, determine the average distance (in inches) of one step. Use this value
to approximate the total distance (in feet) that you have walked; this value should be
calculated and printed to the serial monitor after you turn off the switch to stop the
counter. Remember that there are 12 inches in a foot; you can approximate the total
distance as an integer.
Download the outline sketch from the ESE 111 website. Complete the code to satisfy the above
requirements.
Notes and Hints:
 One significant difference between the MATLAB code and the Arduino code that you
will write is that the Arduino does not make use of vectors in the way that MATLAB
does. This means that when you are calculating , , and , you will have to use a
slightly different approach to find the average stationary acceleration. This will require a
loop; each time the loop runs, the current value of xval, yval, and zval should be added to
a running total. The average value is then the final total divided by the number of times
the loop runs. 100 iterations is a good number for calibration.
 Note that the type of xval, yval, zval, xbar, ybar, and zbar is float, not int. A float
is capable of representing fractional values, like 1.50 and 0.987. However, you cannot
perform operations on variables of different types. For instance, you can’t add an int to
a float. This is an issue because the analogRead() function returns integer values,
but we’d like to convert them to floating point values. If you want to do this, you will
have to typecast the value, as such:
xval = float(analogRead())
This will make xval a float rather than an int.
 To run a function inside another function (for example, to run posCal() inside
loop()), call it by typing its name followed by a semicolon.
 To detect a step, keep track of when the acceleration value crosses the threshold on the
way up and on the way down. That way, you won’t get false positives from jagged peaks.
 You can approximate the total number of steps as twice the number of steps detected on
one foot/leg.
 The delay() function in the Arduino does basically the same thing as the pause()
function in MATLAB, except its argument is milliseconds rather than seconds.
Show a TA when you are finished. This should be completed before the next lab session.
MAKE SURE YOU SAVE YOUR ARDUINO CODE! You might want to email it to
yourself, as well, just in case. You will build on it in the next two labs.