Sparkfun 5DOF Erratic


This is the sensor I’m using.

Here is the code (taken from the kas’ balancing robots for dummies)

// Main module   K_bot               angles in Quids, 10 bit ADC  -------------

// 4 - Checking sensor data format    display raw sensors data            

#include <math.h>

#define   GYR_Y                 0                              // Gyro Y (IMU pin #4)

#define   ACC_Z                 1                              // Acc  Z (IMU pin #7)

#define   ACC_X                 2                              // Acc  X (IMU pin #9)

int   STD_LOOP_TIME  =          9;            

int sensorValue[3]  = { 0, 0, 0};

int sensorZero[3]   = { 0, 0, 0};

int lastLoopTime = STD_LOOP_TIME;

int lastLoopUsefulTime = STD_LOOP_TIME;

unsigned long loopStartTime = 0;

void setup() {

  analogReference(EXTERNAL);                                   // Aref 3.3V





void loop() {

// ********************* Sensor aquisition & filtering *******************


// ********************* print Debug info *************************************


// *********************** loop timing control **************************

  lastLoopUsefulTime = millis()-loopStartTime;

  if(lastLoopUsefulTime<STD_LOOP_TIME)         delay(STD_LOOP_TIME-lastLoopUsefulTime);

  lastLoopTime = millis() - loopStartTime;

  loopStartTime = millis();


void serialOut_raw() {

static int skip=0;

  if(skip++==40) {                                                        

    skip = 0;

    Serial.print("ACC_X:");           Serial.print(sensorValue[ACC_X]);          

    Serial.print("  ACC_Z:");         Serial.print(sensorValue[ACC_Z]);

    Serial.print("  GYR_Y:");         Serial.println(sensorValue[GYR_Y]);



// Sensors Module  ---------------------------------------------------------------------

void calibrateSensors() {                                       // Set zero sensor values

  long v;

  for(int n=0; n<3; n++) {

    v = 0;

    for(int i=0; i<50; i++)       v += readSensor(n);

    sensorZero[n] = v/50;


  sensorZero[ACC_Z] -= 103;                        


void updateSensors() {                                         // data acquisition

  long v;

  for(int n=0; n<3; n++) {

    v = 0;

    for(int i=0; i<5; i++)       v += readSensor(n);

    sensorValue[n] = v/5 - sensorZero[n];



int readSensor(int channel){

  return (analogRead(channel));


I got the sensor about three weeks ago, I’m planning on building a balancing robot to help me learn more. Problem is when I first got the board I was excited, quickly soldered everything together and used this code to test it out, it worked perfectly.

Right side up and level: X = 0, Z = 104, Y = 0
Upside down X = 0, Z = - 104, Y = 0
90 degrees to the right X = -104, Z = 0 Y = 4 etc.

This was pretty much what the post said to expect. I’ve had exams for the past three weeks, and finally thought tonight would be the night I could get a good start on my project and bam, guess what? No such luck. Using The EXACT same code and the EXACT same wiring I no longer get the same readings, if any. The readings now remain constant:

X= 0, Z= 104, Y = 0

Sometimes if I upload the sketch to the board again the values change, but were X should be - 104, X = -36 or similar.
Sometimes movement makes the values jump e.g Z = 247.

My initial thought a damaged wire/s ?(Someone else used the board)
I have tried different sketches. The problem sticks.

Any ideas? (The is no way I’ll be able to afford another board soon, the are really expensive in my country)

Check the connections with a multimeter, some lose connections aren't visible to the eye...

Thanks for the reply!

I am getting readings from X,Y and Z on my multimeter, about 1.5V on each?

Re-soldered a suspicious GND wire and now I always receive readings. They still are not the ones I am looking for. The board is slow to respond to changes in movement, the readings have improved but I they still read around -25 where they should read around -104 and the X and Y readings are never positive, when they should read 1 - 104, they just return 0.

i am having the same problem with two of these sensors. sometimes the values are completely off the wall, going from -900 at 0 position to +900 in a matter of a few hours. all my connections are good. if anyone has any clues i'd love to hear them.

ended up redoing all the connections and that seemed to fix most of the erratic drift.

@ John Also; thanks for the input, I actually ended up getting a new IMU all together. Everything seems to be working perfectly now.