An Experiment in Accelerometers, the Raspberry Pi, and Web Technology

Once a quarter my place of work grants our teams a ‘day of autonomy’ to work on any projects we see fit to grow our knowledge and development skill set. For this day of autonomy I decided to take an idea that was outside the box (or, outside the barn) and see how far I could run with a hardware-based project using the Raspberry Pi, an accelerometer and the power of the web.

What’s an Accelerometer?

First thing’s first, what’s an accelerometer and what does it do? In short an accelerometer is a chipset capible of detecting orientation on an x, y, and z axis by measuring the acceleration forces applied to it. Your smart phone uses an accelerometer to determine its orientation to trigger screen rotation. The chipset itself will only run you a few dollars and looks something like this:

“The MPU6050 chipset was used for this project.”

The setup for this project involved the use of a breadboard and jump wires to connect the chipset to the GPIO pins of the Raspberry Pi.

“The pin diagram for the MPU6050 and Raspberry Pi.”

Cool, But Why?

An accelerometer has many applications. For this particular application we’re going to discuss bicycles. The initial pitch was simply, “Wouldn’t it be interesting if bikes included an accelerometer?” Bicycles are dynamic machines, frequently pivoting in coordinate space. What kind of information could we derive from a rider if we had this data? A few thoughts included the potenial for unique insights into coaching and progress building through analysis of rider and bike dynamics, product development and testing through use of 3D modeling software, or the potential for integrated safety features such as crash detection (the measure of an accelerated force in a given direction).

Hardware Interfacing

Now that I’ve covered the ‘why’, let’s cover the technical implementation. With the hardwire configuration wired up some code needs to be written to read the analog data produced from the accelerometer. The Raspberry Pi has libraries to help interface with the GPIO pins, but rather than spending time reinventing the wheel I discovered a Python module that effectively gets right to the point of abstracting the low level communication between the MPU6050 chipset and the Raspberry Pi. All that was left to do was to import this module within my own Python script and make use of the data.

from mpu6050 import mpu6050
sensor = mpu6050(0x68)
accelerometer_data = sensor.get_accel_data() # Get the current X, Y and Z coordinates from the accelerometer

The Python script imports the necessary module, instantiates the sensor based on its I2C address, and pulls the coordinate data from the accelerometer. Tilts in the X-axis represent leaning the bike left or right, while tilts in the Y-axis represent an incline or decline (or a wheelie/stoppie when measured in short durations). Once the script is running, the breadboard holding the accelerometer can be picked up and rotated to produce feedback.

“The running Python application with X/Y-axis feedback for turns, wheelies, and stoppies”

Passing the Data via Websockets

For this proof of concept I wanted to distribute the data as close to real time as possible to an external system. The thought was to explore the possibility of distributing the data across the web to an external service that could parse the coordinate details and analyze the data for patterns that might offer helpful insight. Websockets offer a convent way to ingest the data in real time across the network, and have the added benefit of allowing others to view this data in real time as well. For this I used Spring Boot to develop a websocket server that would accept an asyncronous connection from the Python script managing the accelerometer and pass the data as it’s being generated. The Spring Boot application also includes static HTML and JavaScript resources to allow clients to connect to the embeded websocket server and view the data stream in real time.

“Connection to the websocket server via JavaScript shows the real time data stream from Python”

Further Implementation

Now that the data is being accepted by the websocket server the sky is the limit. Instead of simply appending the input the server can further parse the coordinate data being passed in and be analyzed to form patterns for the use cases above (and more!). This kind of shared coordinate data might also be used in game development to transform game objects in real time (let’s get some wheelies up in Zwift).

Final Thoughts

This project was interesting to work through on a number of levels. Dusting off the Raspberry Pi to work on another hardware-based project reminded me why I’ve made a career of development - it’s exciting to brainstorm concepts and see how far they can be taken. While it’s easy to get caught up in the day-to-day tasks of development, taking the time to conceptualize the ideas rolling around in mind keeps the craft interesting.

I’ve uploaded the source code base for both the websocket server and Python script for anyone interested in taking this day one proof of concept to the next level.

2021

Back to top ↑

2018

A Journey in NES Emulation - Part II

4 minute read

With the last post covering a brief overview of the project as a whole, I want to start discussing some of the more detailed subject matter surrounding the N...

A Journey in NES Emulation - Part I

3 minute read

Writing an NES emulator has been a long time interest of mine, primarily because the subject of software emulation is fascinating. I’ve spent my fair share o...

Game Off 2018 Update - Part II

2 minute read

It’s only been a few days since the last update, but I’ve decided to go ahead and comment on the overall status of this project and where I see it heading.

Game Off 2018 Update - Part I

2 minute read

Wow, I can’t believe it’s already been over two weeks since the Game Off kicked off. This year’s theme is ‘Hybrid’.

Game Off 2018! (Using PICO-8)

less than 1 minute read

Next month marks GitHub’s fifth annual Game Off, where participants spend one month creating games based on a theme that GitHub provides once the contest beg...

A Journey in NES Emulation

less than 1 minute read

NES emulation has been done again and again for decades, yet seemly every year there is a new take on this monumental task using new languages, frameworks, a...

Back to top ↑