10 years, 2 months ago.

Compass HMC5883L gives same reading

I am using a modified library for HMC5883L (http://mbed.org/users/higedura/code/dotHR_EKF_phithe/file/0c42db451cb9/HMC5883L.cpp) which has the adition of:

float HMC5883L::getHeading()
{
    float x = getMx();
    float y = getMy();
    float z = getMz();
    
    float heading = 0.00;
    
    if(y > 0)
    {
        heading = 90 -((atan(x/y))*(180/PI));
    }
    else if(y < 0)
    {
        heading = 270-((atan(x/y))*(180/PI));
    }
    else if(y == 0 && x > 0)
    {
        heading = 0.00;
    }
    else
    {
        heading = 180.00;
    }
    
    return heading;
}

The main code:

HMC5883L compass(p28, p27);

int main() 
{   
    while(1)
    {
        pc.printf("Compass: %f \r\n", compass.getHeading());
        wait(2);
    }
}

everything is connected up correctly as I have had this working before but for some reason now everytime I get a heading its always the same.

Ive tried to look into it but I cannot find a solution, does anyone know why the values are the same in each reading taken?

If you just get the X, Y and Z values, does that work? (I assume not). At which point you need to figure out what is changed. If it worked before there is a good chance your wiring is faulty.

posted by Erik - 18 Feb 2014
Be the first to answer this question.