DIY Head Tracker Part 2 – Wire It Up (And Code)

MPU-6050 Libraries

So at this point you have an Pro Micro (or clone) plus a funky MPU-6050 breakout board. You will need some additional Libraries to get the MPU-6050 talking to your Arduino.

These can be grabbed here on github.  Click ‘Download Zip’.

Super smart dude Jeff Rowberg has done all the hard work. Looking inside the zip (ic2devlib-master/Arduino) you will find a whole bunch of libraries for interfacing to many many cool devices.  We only need MPU6050 and IC2DEV but I copied all these to the Arduino Library folder (<your arduino insall folder>/libraries) for later enjoyment.

You will need to quit and re-run the Arduino IDE to pick up the libraries.  You can tell if it’s worked by clicking  ‘File->Examples->”  You should see MPU6050 examples in there.

I Dream of Wires

I soldered the right-angle pins to mine so that once mounted on the side of some headphones the chip would stick out sideways. This worked fine, but so does mounting the sensor board vertically. This means you’re able to mount the finished device on the top of your head or on the side next to your ear. This is because;

  1. We’re outputting all 3 axis  to the PC
  2. You can select which axis to use in Elite Dangerous Head Tracking config
  3. You can invert the axis in the control config.

Here you can see the right angle pins. I’ve marked those that are needed with a yellow blob.


  • VCC is your +volts  (these modules are usually happy with between 3 to 5 v, but do check!)
  • GND is ground
  • SCL Is a Data Clock
  • SDA is a Data In/Out
  • Int is an interrupt pin that the MPU-6050 can use to tell the Pro Micro that is has finished generating data.

N.B Jumping ahead somewhat but, oddly, if you leave the interrupt pin unconnected the Head Track sketch will usually work for a bit and then stop. I think an unconnected interrupt pin (code says “listen to  interrupts on this pin and execute XXX when you get one”) can generate dodgy interrupt signals. Anyways…

Wiring up is a doddle.  The power for this rig is simply what’s supplied over USB.

The Arduino has a ground pin (gnd) and a vcc (+5v) pin. You connect these to the vcc and gnd on the 6050.  I always connect ground first. I think that’s a good thing.

Most MPU6050 boards I’ve seen are happy at 3 or 5v. If you have once that says 3v then the Arduiono Micro has a 3.3v pin that you can use instead.

Arduino      ->     MPU6050

  • VCC -> VCC
  • GND- > GND
  • Pin 7 -> Int
  • Pin 2 -> SDA
  • Pin 3->  SCL

NOTE  The button in the picture below is no longer connected as shows. It simply needs to be connected between Pin 10 and Ground.


Handily the pins on my 6050 board are labelled in the back as well as the front.

At this point double check your wires (especially Vcc and Gnd).  I’ve never broken anything by getting data or interrupt wires crossed but be careful with the power pins.

You now try our some of the MPU-6050 examples sketch, e.g. File->Examples->MPU6050->Example->MPU6050_raw


LED pin on Arduino Micro is 17. Most sketched will have the LED on 13. It’s easy to change .

Open the serial monitor window (shift-ctrl-m) to see text output from the device. If you see garbage make sure the BAUD rate in the monitor window matches the one specified in the code,e.g.  Serial.begin(38400);

The final Head Track sketch has debug output turned off but if you need to see what’s happening you can enable it simply enough (see sketch).


I also have a small momentary switch placed at the end of the board. This acts as a recalibrate button so if you find the Head Tracker has drifted away from centre you can look forward and press the button once. The sketch will then sample the orientation for a few seconds (while you keep still) and use this as the new ‘looking ahead’ offset.

The Arduino Micro has internal pull-up resistors than can be configured when setting a pin as ‘Input’.  The button then simple needs to go between Pin 10  and ground. The ‘unpressed’ state is then ‘HIGH’ with the value going ‘LOW’ when the botton is pressed.

The Sketch

The bit of  code which bring this all together can be found here :

This gdrive folder contains the sketch, MPU6050Joy.ino  , which is the code which gets the orientation data from the MPU6050 and sends it to the PC.

The folder also contains two arduino library files (HID.cpp and USBAPI.h) which need to replace the ones under <your arduino ide folder>\hardware\arduino\avr\cores\arduino

Make a copy of the existing files and stick the modded files in there. These files have been updated to add  USB Joystick emulation along with the existing mouse and keyboard code.

You should now be able to open the Arduino IDE, load MPU6050Joy.ino , select Arduino Micro (under Tools->Board) and hit ‘Upload’.

Open up Windows USB Game Controller config to verify that the device is working:

usvc2 usbgc

The device now reports itself as a hi-resolution joystick, each axis having the range -32768 to 32767.  This is far better than the initial version of the sketch which had a range of only -128 to 127.  The 16 bit range results in much smoother head tracking in game.

The Game

In Elite:Dangerous you should now be able to bind the head look up/down and left/right axis to the USB Head Tracker.


18 thoughts on “DIY Head Tracker Part 2 – Wire It Up (And Code)

  1. Hi, I’m trying this out! MPU6050_raw works just fine, but when I try and compile your code I get “MPU6050Joy:59: error: ‘TrackState_t’ does not name a type”. I can’t find this declared in any header files. Any ideas?

    • Hi Tim,

      few things to check;
      Did you copy the modified USBAPI.h and HID.cpp files into ahardware\arduino\avr\cores\arduino ?
      Are yo using lates Arduino IDE (1.5.6 r2) ?
      Have you got the right board seleced under Tools->Board (will be Arduino Micro or Sparkfun Pro Micro depending on which type you went for).


      Rob J

      I did get this error once and quiting the IDE and restarting it fixed it.

  2. Hi, I got the info about the USBAPI.h and HID.cpp files from Dan (long time uni buddy 😀 ), but I didn’t see any reference to them on your blog, did you post them on the ED Alpha forum? I’m not Alpha but Dan pointed me in the direction of your blog.

    I’m using the Sparkfun board unmodified, and have Arduino Micro selected (seems to work). Despite an extensive IT degree (20 years ago) Arduino is all new to me with this project!!! (Good fun though)

  3. Hi, I got it to compile and upload! Now the problem is that I see output on serial monitor but it keeps going into recalibrate. Looking at the code the only time that happens is if pin 10 is high. Do I need anything else in there to keep pin 10 low? Sorry, my electronics is virtually non-existent (not touched that since GCSE physics :D).

    The good news is that I can see joystick output on the device configuration screen as well, but with BIG pauses.

  4. Hi, How do you have the button wired ? Option 1 is wire it between pin 10 and +v and then have a resistor between pin 10 and ground. The newer option is to just have the button between pin 10 and ground. There’s a change in the code depending on which one you’ve got.

    Option 1 code :
    In setup

    pinMode(buttonPin, INPUT);

    and then later

    if (digitalRead(buttonPin) == HIGH)

    Option 2 code:

    pinMode(buttonPin, INPUT_PULLUP);

    and later

    if (digitalRead(buttonPin) == LOW)

    There is a delay on startup – the code allows 10 seconds for the MPU chip output to stabilise and then calibrates for about 5s.

  5. Pingback: DIY Head Tracker | Mis-adventure in 3d Printing

  6. OK word of warning to people. Don’t make the mistake I did and think that the micro is just a smaller version of the uno and that this will therefore work with the uno. Seems there is a significant difference between the uno and the micro in that the uno has separate communication whereas the micro does everything in one place. This adds the flexibility that it can behave like a mouse, keyboard or (in this case) custom defined joystick. Big “whoops” on my part there. There is something called unojoy however that I believe is a software+firmware (flashing required) solution that makes an uno appear like a ps controller. I’ve not looked into this though and it is of course, another kettle of fish entirely. This stuff here though, will only work on a micro (and possibly a leonardo?)

  7. Hi
    I try and compile your code in Arduino Mega2560 I get “EDTrackerII:166: error: ‘TrackState_t’ does not name a type”.
    Please help me to run your code in Arduino Mega2560.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s