Shop OBEX P1 Docs P2 Docs Learn Events
Trouble reading HMC5883L Compass module — Parallax Forums

Trouble reading HMC5883L Compass module

ResRes Posts: 4
edited 2012-08-11 13:46 in Accessories
I recently acquired a HMC5883L compass module and connected it to an Arduino to run the test code provided at the Parallax web site. After fixing an error that popped up during compilation (had to change "send" and "receive" to "write" and "read" to conform to the latest Arduino syntax), the code ran, but all I get out of the module are unchanging values on all three axes regardless of orientation. I have experience with I2C software and with I2C software for the Arduino in particular (using the wire library), and the code looks fine. Anyone seen this kind of behavior?

Comments

  • FranklinFranklin Posts: 4,747
    edited 2012-08-08 21:08
    Show us your code and how you have things connected. You probably are not getting any signal either to or from the device and may be sending the wrong requests.
  • ResRes Posts: 4
    edited 2012-08-09 15:12
    Here is the code I'm using for this test. It came from the Parallax product web page for the HMC5883, and modified by me to replace "send" and "receive" by "write" and "read" respectively to account for the latest Arduino syntax, and to declare the registers used in the magnetometer for the I2C communications. The magnetometer is connected to the Arduino ports A4 and A5 for SDA and SCL respectively. The output the 5883 provides is: x = 1434, y = 134 and z = 1280. Output is the same regardless of orientation. It could be not enough time for the device to update before requesting a reading, or some fault in the Wire library of the Arduino, or a faulty device or something else. I'm kind of at a loss at the moment, other than doing a rewrite of the code to eliminate the Wire library. Any ideas would be appreciated.
    Thanks!
    Res

    /*
    An Arduino code example for interfacing with the HMC5883


    Modified by Res to update "send" to "write" and "receive" to "read" and
    to define mode, reg and xreg as registers of interest for experiment 8/9/12.


    by: Jordan McConnell
    SparkFun Electronics
    created on: 6/30/11
    license: OSHW 1.0, http://freedomdefined.org/OSHW


    Analog input 4 I2C SDA
    Analog input 5 I2C SCL
    */


    #include <Wire.h> //I2C Arduino Library


    #define address 0x1E //0011110b, I2C 7bit address of HMC5883


    byte mode = 0x02; //mode select register
    byte reg = 0x00; //continuous read mode
    byte xreg = 0x03; //first data register (1 of 6, MSB and LSB for x, y and z


    void setup(){
    //Initialize Serial and I2C communications
    Serial.begin(9600);
    Wire.begin();

    //Put the HMC5883 IC into the correct operating mode
    Wire.beginTransmission(address); //open communication with HMC5883
    Wire.write(mode); //select mode register
    Wire.write(reg); //continuous measurement mode
    Wire.endTransmission();
    }


    void loop(){

    int x,y,z; //triple axis data


    //Tell the HMC5883 where to begin reading data
    Wire.beginTransmission(address);
    Wire.write(xreg); //select register 3, X MSB register
    Wire.endTransmission();


    //Read data from each axis, 2 registers per axis
    Wire.requestFrom(address, 6);
    if(6<=Wire.available()){
    x = Wire.read()<<8; //X msb
    x |= Wire.read(); //X lsb
    z = Wire.read()<<8; //Z msb
    z |= Wire.read(); //Z lsb
    y = Wire.read()<<8; //Y msb
    y |= Wire.read(); //Y lsb
    }

    //Print out values of each axis
    Serial.print("x: ");
    Serial.print(x);
    Serial.print(" y: ");
    Serial.print(y);
    Serial.print(" z: ");
    Serial.println(z);

    delay(250);
    }
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2012-08-09 17:02
    The code here is a little simpler, already updated for Arduino 1.0, and verified with Arduino Uno:

    http://learn.parallax.com/KickStart/29133

    If it doesn't work, things to check:

    1. Be sure power and ground are connected as shown in the diagram on the above page. Don't use the 3.3V source on the Arduino.
    2. Double-check the I2C wiring. It's pretty easy to switch data and clock.
    3. Don't operate on a metal table, or near heavy metal objects.

    Though you did say you've used I2C before, just as a final sanity check, verify agains the specific Arduino you're using. Be sure to connect I2C according to the needs of your board. On an Uno, that's analog pins 4 and 5. On a Mega, it's pins 20/21. It's also different on the Leonardo.

    -- Gordon
  • ResRes Posts: 4
    edited 2012-08-09 17:27
    Thank you Gordon,
    Unfortunately, no joy with the code you recommended. I connected 5 volts from the Arduino Uno, made sure Arduino pin A4 is connected to SDA on the 5883 and A5 is connected to SCL. I am presuming the Parallax 5883 module has pull ups on its board for the I2C bus. I made sure the board is clear of large metal things and uploaded the code. This time the code returns x = -1, y = -1 and z = -1, regardless of how I move the module or what orientation it is in. I know the Arduino is working with I2C correctly using the Wire library because I have been experimenting with a Parallax 27911 gyroscope, and it performs just fine. I had been experimenting with the gyro and an accelerometer and was adding the 5883 to complete an inertial measurement unit. By-the-way, I tried the 5883 both by itself with the Arduino Uno and on the same bus as the gyroscope to make sure there wasn't something going on between the two, but got the same results. i'm beginning to think I have a bad unit.
    Thanks again!
    Res
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2012-08-10 14:20
    The way these things operate is that they don't give results if you're looking directly at them. They only work if you look away. Very shy critters, these compasses.

    Beyond that, you might want to contact Parallax customer support at this point. If you got the gyro to work, the compass should, too. So it sounds like it could be a wacky unit.

    -- Gordon
  • ResRes Posts: 4
    edited 2012-08-11 13:46
    I finally know why this compass module has not been working for me. A close inspection of the module reveals that there is a component missing from the SCL line. In the photos of the module on the product web page, you can see what may be a SMT capacitor connected to the SCL line, which in turn is connected to a pull-up resistor and to the compass chip. On my module, there are a pair of empty solder pads where the capacitor (or whatever it is) is supposed to be connected, so the SCL is not connected to anything and therefore I2C can't work!

    I have sent a note and a pair of photos of my module to Parallax Support requesting verification of my finding and and asking what is supposed to occupy those empty solder pads (I can't find a schematic of this module anywhere on the web).

    Thanks to those, especially Gordon, who have replied to this thread. I bought this part from Jameco, and will deal with them once I get verification from Parallax.

    Res
  • Hi. Can i know what should i do because i get the same value, which is -1 no matter what orientation the sensor is. Please tell me what might be wrong Res and Gordon. Thank you so much!

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2017-08-20 00:54
    Res,

    Did you get your compass module from Parallax? I made the mistake awhile back obtaining a bunch of HMC5883L modules from China for a class I was teaching. Half of them were duds. You can read my mea culpa (i.e. crow-eating) here:

    http://forums.parallax.com/discussion/158176/cheap-compass-sensors-a-cautionary-tale

    -Phil
Sign In or Register to comment.