Studio 8: Accelerometer and Step counts

Click here to access the repository for this studio.

Introduction

Some peripheral devices are considerably more complex than others. For example, a graphical color LCD display is much more complicated than the 5x7 LED matrix display we’ve used. However, there are libraries of code that make it easy to work with complex devices. This week, we’ll explore a fairly sophisticated input device, an accelerometer, and its API, which will allow us to build a pedometer.

Objectives

By the end of this studio, you should know:

  • how to read data from the accelerometer (using a preexisting library)
  • how to perform peak detection in software

Accelerometer setup

The accelerometer we will use is the MMA8452Q, manufactured by Freescale. Take a look at its data sheet. If you check out the block diagram (Figure 1 on page 3) on the data sheet, you will notice that the part is quite complex. It has a built-in processor of its own that performs the Embedded DSP Functions on the block diagram, in addition to three transducers (oriented along each axis) to detect motion, separate analog-to-digital conversion, and various support hardware.

Sparkfun’s Accelerometer Breakout Hookup Guide does a great job of describing how to hook up the accelerometer in general (see below). It also provide a nice set of example software and use cases.

First, you will need to install the SparkFun_MMA8452Q library. Go to Sketch > Include Library > Manage Libraries… to install the library. Either scroll down or use the search bar to find the SparkFun MMA8452Q library and click the Install button.

INSTALL YOUR ACCELEROMETER!!!

Connect your accelerometer as shown in the hookup guide (the version that uses two 330 Ω resistors). Be sure to use the 3.3V power. SparkFun has provided example code that acquires values from the sensor and prints them. Load and test it by Going to File > Examples > SFE_MMA8452Q > MMA8452Q_Basic.

Open the Serial Monitor and run the code. With the sensor “square” with respect to you, identify how moving it (the entire Arduino/breadboard) effects each value:

  • Which value changes the most when you slide the board directly forward or backward?
  • Which value changes most when you slide the board left/right?
  • Which value changes most when you move the board up/down?
  • Which sensor orientations create positive values and which create negative values? (Rotating the board to change the orientation of the sensor if also useful for identifying which directions correspond to which variables and values)

Close the Serial Monitor and open the Serial Plotter (Tools > Serial Plotter). When used with this sketch, the Serial Plotter will show a graphical depiction of how each of the sensor’s values changes over time. The order of the values will correspond to the color of the lines. x acceleration is printed first and will be shown in blue, y acceleration is second and will be yellow, z acceleration is third and will be red. Experiment with moving the sensor to see how the graph changes as the sensor moves.

Modern fitness trackers, like the FitBit™, smart watches, and even the health apps available for most smart phones, rely on accelerometers to identify a person’s motion. Imagine a person is wearing this sensor as they take a step. Try to move the sensor in a way that would simulate its motion during a few steps (up/down and forward). Review the Serial Plotter and observe the shape of the graph.

Finally, review the code in the example sketch and make sure you understand how to work with the sensor. SparkFun’s HookUp Guide gives a good explanation of how it works.

Research peak detection

To detect individual steps you must figure out when the accelerometer signal peaks and then count the peaks.

You can either: 1) adapt to the current orientation of the sensor (i.e., sense which direction is down and look for steps in that direction), or (2) require the user to orient the sensor in a particular direction (i.e., require that a specific choice of x, y, or z is pointing downward). In either case, you may assume that the orientation does not substantially change during the period of time over which you are counting steps.

To detect individual steps you must figure out when the accelerometer signal peaks and then count the peaks.

  1. Complete the sketch called steps in your CSE132-studio8/ directory.
  2. To start things off, send a stream of accelerometer measurements to the PC. To make this easier, format your data so it will be suitable for a .csv file. I.e., a file containing lines of “comma separated values” — print each group of x, y, and z data on one line, separated by commas, but no spaces.
  3. Open a clear Serial Monitor window, simulate some “steps” (maybe 7-15), then unplug your Arduino to stop the Serial Monitor. Select all the values displayed in the window, copy them, open a new plain text file, paste the value into the new file, and save the new file with a .csv extension in the CSE132-studio8/data directory. You will use this data with a Java program to develop a step-counting algorithm.
  4. Open your .csv file in a spreadsheet (like Excel) and plot your data. Count the number of peaks.

Doing peak detection

There are many approaches to peak detection, but the simplest have the basic form shown below.

	if (y(i) > y(i-1)) && (y(i) > y(i+1))
		sample i is a peak

Where y(i) indicates the i-th sample (the measurement at time i). Or, in other words, given three samples the middle sample is bigger than both its predecessor and its successor.

This approach is frequently augmented by adding a test to ensure that the values are above the mean (remember, we are in the presence of a 1G field, so the mean should not be zero).

  1. Complete Java code in cse132-studio8/src/studio8/CountSteps.java) to detect the peaks in the signal, using signals saved from the Arduino.

    You will not be working with SerialComm this studio. Instead, you will be working with another type of stream: a file. We suggest using a File in conjunction with a Scanner. (Hint: Consider getting an entire line and then using the String class’s split method)

    Have the File open the .csv file you saved earlier. Its constructor accepts a path to a file, relative to the top of your repo (because that happens to be where Eclipse executes this code). You might want to load, then, CSE132-studio8/data/YOUR_FILENAME.csv.

    Use the File when constructing your Scanner.

    Note that I haven’t given you much detail here. Feel free to look around in the Java documentation or elsewhere on the web for good ways to parse .csv files (there are many).

  2. Once you’re confidently loading the data properly and parsing it (separating it into x, y, and z values as appropriate), compare the different step-counting algorithms.

    Try simple peak detection as detailed above, or perhaps count zero-crossings (where, between two reads, the measurement switched polarity, positive to negative or vice versa). More robust than zero-crossings is mean-crossings, since gravity constantly pulls us down. Maybe even more robust is a peak paired with a zero-crossing counting as a step.

    Explore these algorithmic alternatives.

  3. After you have a step counting algorithm working with saved data, convert your Java code so that it will run on the Arduino, and put that code into your steps sketch so that you can count steps on live data coming from the sensor. The step count should (by the time you are completely finished) be displayed on your Serial Monitor. You might continue to send it to the PC for debugging purposes as you continue to tune your algorithm.

Finish up

  1. Commit your code and verify in your web browser that it is all there.
  2. Check out with a TA.

Things that should be present in your repo structure:

  • studio8/
    • steps/
      • steps.ino
    • data/
      • YOUR_FILENAME.csv
    • src
      • studio8/
        • CountSteps.java

Key Concepts

  • Accelerometer
    • Managing and Installing Libraries
    • X, Y, and Z
    • Exporting / Formatting to CSV
  • Peak Detection
    • One Axis Detection
    • Advanced Detection
  • Java
    • CSV parsing
    • File
    • Scanner
    • String split()