HMC5883L Triple Axis Magnetometer

Help
Keith
2014-01-18
2014-05-23
  • Keith
    Keith
    2014-01-18

    The title sounds ever so techno-geek but in essence it is a single board device which can detect the Earth’s Magnetic Fields and outputs data over I2C ports.

    I have found some scratch code for an Arduino project, but frankly that stuff turns me off big style.

    Has anyone had any experience with this device or more some GCB (PROPER) scratch code with which I can get cracking with? Any help would be appreciated.

     
  • Anobium
    Anobium
    2014-01-18

    I do not have the code, but, post the code you have and we can convert.

    :-)

     
  • Keith
    Keith
    2014-01-18

    Here is the "HardDoneTo" (Arduino) Code I have dug up off the web. As I said before this stuff turns me clay cold....

    #include <Wire.h> //I2C Arduino Library
    #define address 0x1E //0011110b, I2C 7bit address of HMC5883

    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.send(0x02); //select mode register
    Wire.send(0x00); //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.send(0x03); //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.receive()<<8; //X msb
    x |= Wire.receive(); //X lsb
    z = Wire.receive()<<8; //Z msb
    z |= Wire.receive(); //Z lsb
    y = Wire.receive()<<8; //Y msb
    y |= Wire.receive(); //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);
    }

     
  • Anobium
    Anobium
    2014-01-19

    Here goes. A new hardware file for you. Be patient - it may take one or two posting to sort.

    You have to new functions.

    Init_HMC5883L and Read_HMC5883L ( device_x, device_y, device_z ). Init initialises the device and the other reads the device.

    I have attached a ZIP with the files you need. You will need to edit chip, ports etc. Edit the GCB file, set up a terminal to show you the results.

    How to use these files: 1) replace your existing I2C.H with the zip file version in INCLUDE\LOWLEVEL, 2) add then put the two .h files in INCLUDE, 3) then put the GCB where ever your wish. Edit the GCB to reflect your file structure - YOU NEED to edit the INCLUDE statements.

    We are here for you.

     
    Last edit: Anobium 2014-01-19
    Attachments
  • Anobium
    Anobium
    2014-03-18

    I am not aware of ATAN. There is code on the web for estimating ATAN, TAN etc.

    Would be great if you can create some functions for us all.

     
  • alejandro1957
    alejandro1957
    2014-03-18

    BS170/BS108/BSS138(SMD)
    Resistor recommended 2K2-->10K(datasheet)

     
    Attachments
  • alejandro1957
    alejandro1957
    2014-03-18

    The ATAN function in Gambas says that is the inverse of TAN.
    TAN that uses only one value (X or Y).
    hand at work, let's see what comes out with Google and mathematical functions.

     
  • Anobium
    Anobium
    2014-04-22

    I do not know. But, willing to help.

    I just reviewed the datasheet. How are the negative value returned in the bytes returned from the device?

     
  • alejandro1957
    alejandro1957
    2014-04-25

    I forgot, if I send the measured values ​​without creating a 16-bit word, the values ​​are these:
    X_Tmp(0)=000
    X_Tmp(1)=111

    Z_Tmp(0)=255
    Z_Tmp(1)=74

    Y_Tmp(0)=000
    Y_Tmp(1)=84

    only the value of Z (0) moved to the left of 8 places is equal to 0xFF00(1111111100000000-16th bit to 1, so it is a negative value).
    then,65354-65280(0xFF00) is equal to 74.that is -74(a negative value).

     
  • Anobium
    Anobium
    2014-04-25

    There are a few errors in the code.

    EjeZ=(Z_Tmp(0)256) should be EjeZ=( Z_Tmp(0) * 256)

    Is this cause of the issue?

     
  • alejandro1957
    alejandro1957
    2014-04-25

    Anobium sorry,but maybe you did not see the attached code?
    when I saw you posted with the tag like this:
    EjeZ=(Z_Tmp(0)256)
    but it was already in the code like this:
    EjeZ=(Z_Tmp(0)*256)
    has the same value to move 8 places left.

     
    Last edit: alejandro1957 2014-04-25
  • Anobium
    Anobium
    2014-04-25

    OK. Ooops.

    Advice. Write code long hand. Do not try to have arrays handled and multiplied on the same line. We are trying to figure out a bug that may be related to this.

    I this is a pain but try this method.

    you have:
    EjeX=SHIFT(X_Tmp(0),"LEFT",8)|X_Tmp(1)

    change to:
    tempvar1 = X_Tmp(0)
    tempvar2 = X_Tmp(1)
    tempvar1 = tempvar1 * 256
    EjeX = tempvar1 | tempvar2

    Then, we can use some debug to sort out the issue.

    I hope I am helping with the right problem. its hard to understand the core issue.

     
    Last edit: Anobium 2014-04-25
  • alejandro1957
    alejandro1957
    2014-04-25

    I understand, try the static variables in recursion.
    I'll try and let you know.

     
  • alejandro1957
    alejandro1957
    2014-05-23

    it works fine.
    it was enough to change the configuration for the module in this way and reads the position at the time a grade from 0° to 359°:
    0x3C 0x00 0x70 (8 samples, 15Hz, reading normal mode)
    0x3C 0x01 0x40 (+-1.9Ga)
    do
          0x3C 0x02 0x01 (single read, no read more)
          WAIT 5 ms (not less than)
          Read function
          WAIT 25 ms (not less than)
          Code of administration involving the user prefers
          WAIT 67 ms (not less than)
    loop

     
  • alejandro1957
    alejandro1957
    2014-05-23

    I'm trying to create a library for Atan and Atan2.
    I will let you know.