Streaming MPU-9250 data and charting in real time with Raspberry Pi 4


This is my blog channel, other videos up to now have been posted under my other account but unlisted

This is nothing new, I’m just making it “from scratch” (stitching libraries together). I have a Raspberry Pi 4 that’s in headless mode so no GUI. I could reinstall it with a GUI and just use some python plotting library but… you know me dunce boy. So I’ll stream the data by websocket and feed it to a web app that will plot the three sets of data (accel, gyro, mag) in real time. I bought a couple sets of MPU9250 15mo apart and finally got around to using them. Sadly seems to be a 50% fail rate whether it’s not showing up in i2c or values being stuck eg. gravity at -8 or whatever.

The SLAM-crappy project is using a Pi Zero as the brain and the IMU will be connected to the Pi’s I2C as a secondary slave by a different address like the Arduino Nano. So this is “practice” for that. Truthfully this is the first time I’ve interfaced with an IMU before and I’m just using this GitHub library to run it.

Remember to calibrate. That seems important/does have an affect eg. getting gravity to 1 if the sensor actually works.

I’m using ChartJS, I’ve used it before pretty cool/straight forward. I did have to look up how to do multiple-line plot and found this SO example.


So I have the above now which I just prototyped out. I still have to actually make it “real time” by setting up the Pi to host a websocket server to dump the live MPU sensor data. And then the webpage will have a websocket client that connects to that and updates the chart in real time.

Oh yeah I just realized you would have to toggle the data sets vs. showing them all at once as each sensor has 3 sets of data so a total of 9 lines would show.

Neat this works… this isn’t plotting the right info as I’m just using progression of time with over Y which is the first accel value. So when it jumps below I’m shaking it left/right which is the X axis on the accelerometer.

Left side is websocket server script sending sensor data to websocket browser client that is plotting it using ChartJS every 100ms, data set is 100 samples

Well that’s it… I have to go work on the math aspect(rotation of points in a 3D space) and then this data/work will be integrated there. I’ll probably use both this sensor and the simpler one 6050 that only has 6 axis. As I don’t know if I’ll gain much benefit from magnetometer indoors (2 BR apt).

Generally though for this sensor buy more than one set in case they’re broken.

Software developer and general technology tinkerer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store