The 9DoF Razor IMU from Sparkfun

In one of the projects my team was working on at IIT Kanpur, we were supposed to conduct some ‘on-the-road’ tests on a parafoil. To measure the inertial parameters, we had to find out some cheap alternative to the costly Inertial Measurement Units (IMU) we had in our lab as rugged road test could have rendered them inaccurate. As Sparkfun Electronics happens to be our vendor of choice for this kind of stuff, it didn’t take us long to narrow down on the 9DoF Razor IMU. The Razor IMU was cheap, very light, small in size and most important of all, it was Open Source. The on-board sensors were of pretty high quality too. Perfect Choice!

Once we received the IMUs, testing them for functionality was pretty easy with the on-board firmware. Simply power them up, connect them to the PC through a USB to Serial converter and that’s pretty much it. I faced some problems with the magnetometer output and the on-board power supply, which I sorted out eventually, the technical support staff from Sparkfun told me that there was some inherent problem with the power supply design due to which the magnetometer malfunctioned sometimes. I have discussed the problem at length and the workaround in the following two videos on YouTube (read the description) :-

9DoF Razor IMU from SparkFun Malfunctioning

9DoF Razor IMU from SparkFun Functioning Properly

Currently, the Razor IMU’s product page on Sparkfun’s website states that they are coming up with a new version, I hope this issue won’t be there this time.

The IMU’s firmware (not talking about the factory burnt firmware for testing here) has been written in the Arduino environment. Being an Arduino illiterate, it took me quite some time to get it up and running. To help others like me, I wrote a step-by-step tutorial. I have also discussed about some modifications I did in the original source code. You might want to have a look……

SparkFun’s 9DoF Razor IMU Made Easy

If you are reading this then you probably don’t know anything about Arduino and Python(like me!) and have already spent a few frustrating hours trying to get this IMU up and running. I can understand that the python graphic interface video on the product page looks elusive now and is making you pull your hair off! No more headbanging! Simply follow the steps given below while enjoying a cheese pizza with coke and you’ll be just fine.

Uploading the AHRS code on the Razor IMU

  1. Download the latest AHRS code from here.
  2. Download and install the latest Arduino IDE(arduino 0018) from here.
  3. Now you need to find a file called ‘boards.txt‘ in the installed Arduino directory. The path to this file is, Root Directory → arduino-0018 → hardware → arduino → boards.txt.
  4. Assuming that you have the latest Razor IMU boards from SparkFun which come fitted with an ATMega328p, open the boards.txt file and add the following lines at the bottom and save it.

############################### 9DOF Razor IMU with ATmega328p












You need to perform this underground file manipulation because the Razor IMU has the ATMega328p deriving its clock from an 8MHz crystal oscillator while the preset board definitions assume the processor to be running at 16MHz. Note the second last line above,


If you don’t add the above stuff to the boards.txt file, you’ll still be able to upload the AHRS code on the IMU board but you’ll receive nothing but some weird ASCII characters on the terminal (like I did for two days!).

  1. Now connect your Razor IMU to the computer through the SparkFun’s FTDI Breakout board or some other USB-to-serial converter stuff (you are lucky if you have that thing called serial port still there).
  2. Uploading the AHRS code on the Razor IMU through Arduino IDE is pretty simple. Open all the files present in the (I am using version 1.0 for the time being) in the Arduino environment.
  3. Click on Tools → Boards → Sparkfun 9DOF Razor IMU with Atmega328p
  4. Click on Tools → Serial Port → Select the serial port on which Razor IMU has been connected.
  5. To upload the code on the board, click on File → Upload to I/O Board.
  6. You should then see the IDE compiling code and then burning it on the IMU board. Once ‘Done Uploading’, you can see the Razor IMU in action by opening a terminal software like Hyperterminal or RealTerm. Set the baud rate at 57600.

Python Graphic Interface for the Razor 9DoF IMU

I know you were waiting waiting for this! Its pretty simple…..

  1. Download and Install Python 2.6.5 and VPython from here.
  2. Download and Install PyWin from here.
  3. Download and Install PySerial from here.
  4. Download and Unzip (WHEW!!!) the graphic interface code from here.
  5. Open the IMU_Razor9DOF file in Notepad and set your COM port and the baud rate(57600 already) and save changes.
  6. Double Click IMU_Razor9DOF to run it and brace yourself for some cool graphical display of the IMU data. :-)

Some playing around with the AHRS code

I have always been a WINAVR chap, but Arduino is fun and easy once you get familiar with it. I am not! But still, I thought why not play around with the AHRS code. I wanted to see the accelerometer readings apart from the Euler Angles which are shown on the terminal. Code is very much self-explanatory and anyone can easily modify it.

1. Open the file Output.pde in the Arduino IDE.

2. Replace the following piece of code

#if PRINT_EULER == 1










With this code

#if PRINT_EULER == 1








Serial.print( Accel_Scale(accel_x) );

Serial.print (“,”);

Serial.print( Accel_Scale(accel_y) );

Serial.print (“,”);

Serial.print( Accel_Scale(accel_z) );




3. In the file SF9DOF_AHRS_1_0.pde the Accel_Scale macro has been erroneously defined as

#define Accel_Scale(x) x*(GRAVITY/9.81)

Change it to

#define Accel_Scale(x) x*(9.81/GRAVITY)

4. Compile and upload the program on the Razor IMU. Now you should see the accelerometer readings in meters per second per second on the terminal. Making these modifications does not affect the functionality of the Python Graphic Interface.

Thats all folks…..I hope you enjoyed your cheese pizza and coke! :-)

About these ads

About Pranjal Chaubey

An engineer, who believes in 'Being Wise, b.i.t.Wise!™'
This entry was posted in AVR, Tutorials, Utility and tagged , , , , , . Bookmark the permalink.

66 Responses to The 9DoF Razor IMU from Sparkfun

  1. Sebastian says:

    Hey thank you very much for this Tutorial.
    My prof wanted me to find that .
    I recognized some drifts but thats not your problem ^^

    • Thanks!
      Drifts will always be there as the Razor IMU code (and on-board processor) are very simplistic and don’t employ any sophisticated filtering algorithm (Kalman for example).

  2. Frederic C says:

    In your opinion, is it possible to post-process the accelerometer values to obtain a reliable displacement value in the x-y plane?
    And thanks for the post. Good catch on the gravity constant.

    • Thanks!
      You can always post-process to get the position but it can NEVER be reliable for a considerable amount of time, even with the best MEMS accelerometers in the world! It depends on how long you want to do it to get the position and how much positional error you can tolerate. The positional error will increase (without any bounds) with every passing second…….google about something called as Kalman Filter, its the only way out if you don’t want the error to keep on growing with time.

  3. hyosunkwon says:

    Thanks Pranjal,
    it was so much useful tutorial indeed.
    I have a question for further development though, do you have any clues to connect that arduino with processing?? I want to make a graphical contents according to the values that the sensor gives.

    Thanks again!

    • Thanks! :-)
      You want to connect this with your computer/laptop?
      You can use the FTDI F232RL chip module from Sparkfun to connect it to the PC….but you must be doing that already for programming this IMU. Can you please elaborate ?

  4. sidhant says:

    Hey..Can you tell me about the Ultimate IMU from Sparkfun, Which one would be a better option, Ultimate with LPC or Razor with 328? Even im going with the UAV which needs good data rate, what would you suggest?

  5. Fan says:

    Sorry,Does anyone know how to modify AHRS code to calculate position and velocity in 3D translation or full 9DOF displacement? Thanks!

    • parimbaut says:

      this IMU can’t be used as such for velocity and position estimation because of the accelerometer drift (it needs an additional GPS to cope with that)

  6. testms says:

    You know how much it draws on the 3.3v line ? <50mA or more ?

  7. BGOOD says:

    Hello Pranjal!

    I have seen your videos on the sparkfun 9DOF yaw issues and I have tried to hook up 3.3v to the 9DOF directly. However, it still does not work. How exactly did you directly hook up 3.3v to the 9DOF board? The issue I am having looks just like the video you have of the sensor malfunctioning. Any ideas?

  8. Zeng says:

    When I upload the program, I keep getting the error:
    avrdude: stk500_getsync(): not in sync: resp=0x0d
    avrdude: stk500_disable(): protocol error, expect=0×14, resp=0x0d

    I disconnected everything and connected them, for thousands of times, but I just can’t fix it. Did you ever have the same problem?

  9. Invicta says:

    Thank you for the great article on the Razor 9DOF. Do you remember which version of the board you had? Did you ever come up with a fix for the board or did you continue to use the external regulated 3.3V from the FTDI breakout.

  10. sid says:

    Hey again. I’ve got a new Razor IMU from Sparkfun. And it comes preloaded with a firmware which transmits serial data of the sensors. Now is it possible to change in the code of python interface rather than loading the new firmware to the IMU?

    And in case I load the AHRS code in IMU, what type of data would be coming in as output(the format exactly), so that i can interface it with my Arduino board?

    • The first half of your comment in not exactly straightforward. What you are suggesting effectively means that you’ll have to port the AHRS code in python. But it not impossible though, you’ll be getting raw values from the sensors and will be processing the euler angles on the PC. Interesting!

      For this, I suggest you have a look at the AHRS code files. They are in arduino as well, should be no problem for you.

  11. Pingback: RAZOR IMU: Как прошить | MyLinks

  12. praveen says:

    do you hav eany idea to change sensor data rate from 50 hz to some other rate

    • You need to change the timing of the interrupt that does all the processing. I have a feeling that you can trim it down, i.e. sample at less than 50Hz, but you cannot go above 50Hz. The processing power in ATMega is very limited.

  13. Alvaro says:

    i read a char array of serial port and i get Euler angles correctly buy now i dont know how to split them to have pitch roll and yaw in c++ , i have tried with tokens (strtok)ut fails to do it correctly
    Any sugestion ?

    • I would suggest that you slightly modify the firmware of the IMU so that it only transmits the data when there’s a request (like transmit data only when IMU receives the character ‘T’ on the UART). In that way, you’ll be able to capture the complete string coming from the IMU, parsing it should be pretty easy.

  14. Muhammad Usman says:

    I followed the above tutorial in order to burn the code in IMU but when i burn this modified base code into the IMU’s Atmega328p ic using usb to serial Converter…i see the following error….

    “done uploading…
    avrdude: stk500_getsync(): not in sync: resp=0×00″

    now i tried to solve this error by the help of internet but i could not succeed…from the internet i found the information about this error as given below…

    “Check the following:
    If you have a NG Arduino, did you press reset just before selecting Upload menu item?
    Is the correct Serial Port selected?
    Is the correct driver installed?
    Is the chip inserted into the Arduino properly? (If you built your own arduino or have burned the bootloader on yourself)
    Does the chip have the correct bootloader on it? (If you built your own arduino or have burned the bootloader on yourself)
    Note that it is nearly impossible for anyone to debug this, as there are so many possible issues. Try everything.”

    Please tell me the procedure to get this error corrected….
    Waiting for reply and thanking in advance…

    • Marcos says:

      If using one cable USB -> TTL make sure to connect the DTR pin, arduino won’t be able lo load your programs if this pin is not connect

  15. eletrox says:

    hiiii …. i am using 16f877a for interfacing IMU…. any idea abt the code ???

    • You need to know how to use the UART peripheral on the 16F877A, as the IMU interfaces via the UART. Once you understand that, you can very easily write your own code. There are many examples out there on how to use the UART present on 16F877A, google them.

  16. Emory Moody says:

    Pranjal, I would like to thank you very sincerely on behalf of everyone who owns one of these versions of this board. I am also highly grateful that you continue to monitor this article. Thanks for helping to keep a useful tool useful.

  17. Emory Moody says:

    There is good discussion here, and you are providing a shred of hope for those who spent the money on these discontinued devices. Let’s face it, it makes for an expensive keychain and it is much better to find a practical use for the device. I am referencing the conversation below as this sort of application was exactly why I originally bought the device and my own research may be useful to anyone pursuing such an application.

    My team ran straight into math problems, specifically, not enough bit depth to retain accuracy.

    Hence, it does not seem possible to fully implement a Kalman Filter on an 8 bit processor but you may well be able to do so using a FPU like this one


    Frederic C says:
    July 5, 2011 at 9:58 PM
    In your opinion, is it possible to post-process the accelerometer values to obtain a reliable displacement value in the x-y plane?
    And thanks for the post. Good catch on the gravity constant.
    Pranjal Chaubey says:
    July 6, 2011 at 10:37 AM
    You can always post-process to get the position but it can NEVER be reliable for a considerable amount of time, even with the best MEMS accelerometers in the world! It depends on how long you want to do it to get the position and how much positional error you can tolerate. The positional error will increase (without any bounds) with every passing second…….google about something called as Kalman Filter, its the only way out if you don’t want the error to keep on growing with time.

    • Emory Moody says:

      I forgot to add that as Sparkfun was wise enough to put vias on the board for the I2C bus, it is relatively simple to add a wired breakout board with the FPU on it.

  18. HI! So I have the 9DOF Razor program successfully installed and the board is being read by the computer. The program is communicating with the board, but displaying no data. When I run the python interface the roll, pitch, and yaw are all unresponsive (the proper COM port has been programmed). When I run the python shell I get a menu with options 1-5 including accerelrometer,magnetometer, gyroscope, raw output, change baud rate, and then toggle autorun and help. None of the options are clickable/inputable and they scroll up infinitely unless I hit the restart button on the IMU, which moves them back to the beginning and it starts to scroll up again.
    Do you know if the problem lies within the software? Or the board? They seem to be communicating but I’m getting no data and the menu is not working.
    Please let me know if you can/not help! Thank you!

    • Catch is, the razor IMU program does not show up any menu when you program it with the 9DoF code. It simply initializes and then starts sending out data at 57600 baud. Sparkfun actually sells the device with their own firmware, which is supposed to test the unit, which rolls out those 4-5 options that you have stated. On the face of it, it seems that you have tried to burn the 9DoF code in your IMU but for some reason that hasn’t happened. And going by your description, your hardware looks perfectly alright to me.

      Check out this link, and download the “” file (I use this one, other one also works fine).

      Burn the above code in your IMU, inside the zip file, using the Arduino IDE. Your Razor should work fine after that.

  19. Jason says:

    I have a problem :) I copy the configuration in the boards.txt file but Arduino respond:

    AVRDUD :can’t find programmer id

    Valid programmers are:….

  20. Asupathy says:

    how long it ll take to upload the file in the IMU board for me it is taking more than 15 minutes is there any problem

  21. Asupathy says:

    actually i m connecting through XBee the signal is transmitting from sensor to XBee but there the DOUT led is not blinking is there any mistake

  22. Asupathy says:

    i downloaded code from ur website . it is having more than one file which file i shd upload

  23. Asupathy says:

    what does it mean protocol error ,expect= 0*14,resp = 0*18

  24. Asupathy says:

    thanks ya as u said it is finely uploading in USB.actually i have some doubt in ur code

  25. Chan says:

    Hey Pranjal,

    I am trying to connect the Razor via a FTDI breakout board (3.3 V, 8 MHz), but it doesn’t recognize any serial ports to upload my sketch. Is there a fix to this or am I missing something?


    • Have you installed the FTDI driver in your system? Search for FTDI driver on the internet and install their driver so that when you connect the USB, it shows up as a serial port. Then when you’ll open the Arduino IDE, you’ll see a serial port.

      • Chan says:

        I had actually done that, but turns out that my FTDI board had issues. Replacement board worked fine. Thanks though :)

  26. Amol Mahurkar says:

    Do you know any online shopping site from India where I get a IMU? (Looking for cheapest one.. accelerometer n gyro) I just found it on robokits.. (Don’t know whether to trust them) it’s costly.. It’s similar to one at sparkfun.. robokits copied all details except its datasheet .. robokits has new version..
    robokits: (Is it trustworthy enough)
    Which should one I go for? (importing will cost veryhigh). or do u know any other place?

  27. ArduTest says:

    I installed all the python stuff and did all modifications to the razor lke described before.
    In the debug window of python i can see the output of the razor running.
    The two scenes of the python script keeps frozen, the scene with the arrow keeps grey.
    Is there anything i need to configure in the python runtime or is there anything else wrong?

    • Prima facie, this looks to be some VPython issue. Which OS you are running? Check if VPython is compatible with it..

      • ArduTest says:

        I´m running WinXP professional. I found somwhere in the internet a compiled C# executable which is using Python libraries. This works fine, but i cannot modify it.
        I also think this should be a Python issue, so i asked if there is any ajustment in the Python runtime. The serial comunication works well, i can see all the output values form the Razor in the debug window, only the graphic output seems to hang. If i modify the script, setting x/y/z manually to fixed values and deleting all the serial comunication, i get the graphical output working. Could that be any problem with refresh rates of the output window or do i have to force (somehow?) a screenupdate of the output window after recieving new values form the serial port? I never used Python before, so i try to get some help…
        Thanks for any idea..

        • Even I am not a python user, so can’t say much. Is your graphics card compatible with the Python Library?
          You might want to put this question on some Python forum with seasoned python coders around.

  28. Johnny PaxIovanni says:

    Hi guy,
    thanks for the article, i have a problem with my Razor 9DOF, i have the Drift problem to SE but i have the FTDI Basic 3.3v by Sparkfun, so i think it’s not an alimentation problem, any suggest or i forgot something? :D

    Thank you Gio.

  29. Michael says:

    Hi Pranjal,

    I am connecting the Razor board(new version, to a Xbee Module. But the xbee on my computer is not receiving anything. The connection to the Xbee is correct(i checked it using a bounce back code written on an arduino).
    Sparkfun says that the razor comes with a inbuilt code. So just by connecting to a serial port of 3.3 v logic level, we will be able to see the values.
    A 5V supply is given to the board and the 3.3 v from the board is connected to the Xbee module.

    Sometimes I get some unknown characters in the serial terminal, otherwise nothing,
    The xbee is working at 57600bps.

    What can be the error? Can you please help me?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s