Saturday, 16 November 2013

3D OpenGL visualisation of the data from an MPU-6050 connected to a Raspberry Pi

In this post I'll show how to serve the data over http and display a 3D representation in OpenGL extending on a previous blog post detailing how to read data from the MPU-6050 sensor and convert it into a something useful.

Using a simple web server to serve up the data

Let's start by setting up a simple server based on, which is installed via apt-get
sudo apt-get install python-webpy
Now create a directory to put the code in and create a simple test program
mkdir webpy
cd webpy
Use the following as a test
Save the and then set it as executable with
chmod +x 
and then run the code
 you will see something like this showing the server is waiting for a request (pressing Ctrl+C will stop the server)
Now point your browser at http://ip-address-of-your-pi:8080 and it will show a web page with the content of Hello, world!.  We can make use of this to read data from a remote machine, in my case my Linux desktop.

Adding the sensor code to the server

Replace the contents of with The server has to be run as sudo so you have permissions to read from the I2C
sudo ./
Connecting via your browser will now return the X & Y rotation values e.g.
-28.7291281627 -39.4833542336

3D visualisation

I'm using a Linux desktop and that is all I have tested this simple code on, I've no idea if it works on Windows or Macs and it certainly won't run on the Pi itself.  I'm no OpenGL guru so this code is just hacked together to get something visible. 

Setting up OpenGL and pygame
sudo apt-get install python-opengl
sudo apt-get install python-pygame
Now save the following to a file (in my case and run it
Remember to change the URL line 038 to your specific value (the address you used earlier to test the server). When you run it a window will open showing the orientation of the sensor, rotating the sensor will update the display.

You'll notice that when the sensor isn't being physically moved the noisy data is causing it to wobble.  The next blog post shows how to reduce this.


  1. Once again, Thank you Andy! Counting the hours until the day job finishes so i can go play Pi! :)

  2. Excellent Work! Up and running on OSX with only few changes! Thankyou!

  3. Glad it worked ok. I'd be interested to see any changed / improvements you make to the code.

  4. I have corrected a bug in the render code and the server code so the orientations are calculated correctly.

  5. hello i tried the 3D visualisation thing in my raspi3 but i am getting the import errors of pygame and OpenGL but i have already did install them into my raspi and when i write "import pygame" into the python shell in raspi i get no error.Could you help with this problem i cant simulate my gyro in my raspberry pi3

    1. Unfortunately it won't run on the PI itself (as stated in the blog post), I ran the 3D code on my Linux machine.

    2. Try this.

    3. thanks for helping andrew birkett and nao2g, i think i got too excited to recognize it wont run on raspberry pi :D your blog is really helpful by the way have a nice day

  6. dayday, you can make it run on your RasPi alone.

    $ git clone
    $ cd MPU6050_teapot_demo
    $ python

    then in another terminal window:
    $ python

    1. K, i will try this as soon as possible, thank you very much!

    2. hi! worked with no problem but when i try work the i get an error which is:
      pygame error: couldnt fing matching GLX visual

      i wanna discuss about it if it is ok to you, do you have an mail adress, thanks again

    3. I've updated the

      Please check if you have pyopengl installed.

      $ pip list |grep GL
      PyOpenGL (3.1.0)

      If not, install it;
      $ pip install pyopengl

    4. sorry, I found sudo apt-get install python-opengl is better to install.

      Please check if it's available.
      apt list python-opengl

    5. O did it and it is working now with no problem, really thank you very much nao2g! :)

  7. Hey ! The pygame window freezes and no display comes on the terminal, how should i fix this ?

    P.S. Using it on Win 10