Photo by Tim Gouw on Unsplash

Low-cost sensor data collection in the field — using your smartphone and a little python

Lukas Trojansky
6 min readMay 19, 2021

--

Have you ever encountered situations where it would have been really useful (or merely interesting) to be able to record sensor data on acceleration, speed, altitude, inclination or something else, but it was impossible because you didn’t have the right equipment with you or the effort would have been too great for a “proper” measurement? Who, for example, has the right measuring equipment with him to record the g-forces during a roller coaster ride in the theme park, unless he is an engineer for roller coaster construction?

If only there were a universal device with all kinds of sensors in pocket format that you could always carry with y…oh, wait. There is. Our math teachers were horribly wrong back then when they said we won’t always be able to keep a calculator in our pockets, and along with the calculator we got a whole bundle of sensors to go along with it. What a time to be alive.

In this article we will record the acceleration sequence during an elevator ride, pull it into python, calculate the velocity and travel curve from it and finally plot the recorded data. It’s a simple, straightforward example, but the workflow can be quite similar if you want to analyze other sensor data from your phone.

I never put disclaimers, but I’ll say it anyway: don’t use sensor data from your phone when it comes to safety-critical applications.

All code and raw data used here is available on Github. You can also view the notebook online via nbviewer.

Accessing the phone’s sensors

Common sensor types found in smartphones are gyroscopes, ambient light sensors, fingerprint sensors, proximity sensor, compasses, barometers, Hall effect sensors and perhaps most important accelerometers.

Phones use it to switch between landscape and portrait modes, to identify viewing orientation, counting steps, to recognize motion gestures and so on. Usually the sensor measures changes in the distance between capacitance plates caused by motion on three axes (X, Y, and Z), and determines the instantaneous acceleration and deceleration forces accordingly.

There are many apps that allow access to the phone’s sensor data. In this example the brilliant phyphox from RWTH Aachen University is used, which is free of charge and free of ads. Another popular one which would work as well is called Physics Toolbox.

Recording the ride

For this example, I simply recorded an elevator ride (from the first floor to the top) by using the acceleration with g recording mode in phyphox and placing the smartphone on the floor of the elevator.

Coordinate system of the phone’s accelerometer

The coordinate system is such that the z-axis is pointing out of the display, which can be verified quite easily by starting a recording and “shaking” the phone in the three main directions. After exporting the raw data to a *.csv file we are good to go.

Importing the raw data

pandas DataFrame objects are a great option when dealing with large data sets. pandas also provides a very easy way to read in *.csv files without having to explicitly specify delimiters and other properties of the file. pandas provides a built-in plot function that uses matplotlib by default. However, for this example I want to use the more advanced plotly backend.

Let’s have a peek view at the resulting DataFrame df.

As you can see, the data set contains five data columns (time, acceleration in x, y, z as well as the absolute acceleration) and an index, which is simply numbered in a continuous manner.

Using the built in plot method we see that the acceleration in x and y direction is close to zero while the z-component of the acceleration ranges somewhere around the gravitational acceleration g≈ 9.8 m/s².

Cleaning up

Towards the end of the data recording, there is an acceleration peak because I had already picked up the phone from the ground before the recording was stopped. We therefore want to truncate the data at the 25 second time mark.

Accounting for gravity

Since the elevator is not in free fall, we are only interested in the acceleration relative to the (local) gravitational acceleration. However, since the absolute acceleration was recorded during the measurement, the local gravitational acceleration must be subtracted¹. We will simply determine that value here by calculating the average g_mean of all measured values of z-acceleration.

Finding velocity and distance traveled

In order to calculate the velocity and the travelled distance from the acceleration curve, we integrate the measured values numerically. For this purpose, scipy’s integration method can be used. Specifically we use scipy.integrate.cumtrapz (using the composite trapezoidal rule) which is suitable for arbitrary spaced samples. To make use of the function, I first extracted the values of the time and acceleration column from the DataFrame df and afterwards created a new dataframe df_z which contains all the interesting data for our application. There are also ways to apply functions to columns within the existing DataFrame, but I do find them somewhat cumbersome.²

Plotting the results

Previously, we used panda’s built-in plot method, which is very easy to use. However, to create a more advanced plot with multiple subplots and a shared x-axis, it is convenient to use the functions provided by plotly directly. Plotly comes with several themes/templates included which provide a distinct look for your plots. I decided for the ggplot2 template for our final results plot.

In the plot we see that the maximum travel speed of the elevator is about 0.65 m/s and the distance traveled was about 9.4 m. Short plausibility check: The reading was taken during the trip from the first floor to the 4th floor (i.e. over a distance of 3 floors) and the usual floor height in Germany is 3.2 m. The result therefore fits quite well (3⋅ 3.2 m = 9.6 m).

Final thoughts

I think that even this simple example shows impressively what can be done with the sensors in the cell phone, even if it certainly cannot be compared with industrial measurement technology.

  • The combination of pandas and plotly seems ideal for processing measurement data, especially if you want to screen and display the data without much effort.
  • As is often the case, it is crucial to check the plausibility of the measurement data as well as possible, especially if you do not fully trust the sensors or the data processing in the cell phone.
  • In many cases it will be necessary to filter the recorded data, which was not covered in this example — search for Kalman filter, low pass filter and moving average if you are looking for information on this.

In case you have feedback or constructive criticism feel free to reach out!

Footnotes

[1] This correction is only possible because there is a linear movement along a single axis. In the general case, the Acceleration without g measurement should be selected. In this case, the acceleration data are already internally corrected for the gravitational component g with the help of the values of the gyroscopic sensor, since the orientation of the phone must be considered during the correction. I have chosen the other route at this point, because my phone sometimes delivers skewed values when measuring without g.

[2] See pandas.DataFrame.apply

--

--

Lukas Trojansky

Mechanical engineering with an ever-growing toolbox.