HMC5883L Magnetometer chip Possibilities. - Page 2


Closed Thread
Page 2 of 2 FirstFirst 12
Results 41 to 58 of 58
  1. #41
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi George

    This is the exact one I am using ... The ebay link has Pics and Circuit... I am running it at 5v.. As the LCD needs 5v

    http://www.ebay.co.uk/itm/1112141408...%3AMEBIDX%3AIT

    BR
    Andy

  2. #42


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    So you are connecting the clock and data from the PIC directly to the break-out board pins? Do you have pull-ups? if so to 5 or 3.3?

    george

  3. #43
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi George

    I wondered about this

    I have measured 2K2 to the 5v rail on the breakout board.

    The Ic2 link seems to run OK so happy with that.

    My issue remains to be the conversion from radians to degrees.

  4. #44
    Join Date
    May 2013
    Location
    australia
    Posts
    2,368


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    I bought one to play with

    after looking at the raw data I came up with this , works ok if device is held level , tilt compensation is elusive at this stage



    Code:
    '****************************************************************
    '*  Name    : hmc5883l.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 29/04/2011                                        *
    '*  Version :                                              *
    '*  Notes   :                                                *
    '*          :  12f1822     
    '*          :  
                 '
    '****************************************************************
     #CONFIG
    cfg1 = _FOSC_INTOSC
    cfg1&= _WDTE_ON
    cfg1&= _PWRTE_OFF
    cfg1&= _MCLRE_ON
    cfg1&= _CP_OFF
    cfg1&= _CPD_OFF
    cfg1&= _BOREN_ON
    cfg1&= _CLKOUTEN_OFF
    cfg1&= _IESO_ON
    cfg1&= _FCMEN_ON
      __CONFIG _CONFIG1, cfg1
    cfg2 = _WRT_OFF
    cfg2&= _PLLEN_OFF
    cfg2&= _STVREN_ON
    cfg2&= _BORV_19
    cfg2&= _LVP_OFF
      __CONFIG _CONFIG2, cfg2
    #ENDCONFIG
     
    
          osccon=$6A    '4 mhz
          anselA=0       'dig i/o
            
          trisA=1110
      
      
      
      
       pause 2000
      serout2 porta.0,84, ["ready",13,10]
      lata.0=1
      
      
       SDA            var      porta.2                 'transceives to/from SDA
       SCL            var      porta.4                  'sends clock pulses
    WRITE_DATA     CON     $3C  'Used to perform a Write operation
    READ_DATA      CON     $3D  'Used to perform a Read operation
    MODE           CON     $02  'Read/Write Register, Selects the operating mode. Default = Single measurement
                                 'Send $3C $02 $00 on power up to change to continuous measurement mode.
    X_MSB          CON     $03  'Read Register, Output of X MSB 8-bit value.
    X_LSB          CON     $04  'Read Register, Output of X LSB 8-bit value.
    Z_MSB          CON     $05  'Read Register, Output of Z MSB 8-bit value.
    Z_LSB          CON     $06  'Read Register, Output of Z LSB 8-bit value.
    Y_MSB          CON     $07  'Read Register, Output of Y MSB 8-bit value.
    Y_LSB          CON     $08  'Read Register, Output of Y LSB 8-bit value.
    Cont_Mode      CON     $0
    X              VAR      byte 'x sensor measured value
    Y              VAR      byte 'y sensor measured value
    Z              VAR      byte 'z sensor measured value
    READX         VAR      WORD 
    READY         VAR      WORD
    READZ         VAR      WORD
    brads          VAR      Word 'angle in brads
    degr           VAR      Word 'angle in degrees
    Radians        VAR      Word 'value calculated using X ATN -Y
    
    ' -----[ Main Routine ]-------------------------------------------------------
    PAUSE 50                                     ' Give sensor needed power up time
      I2CWRITE SDA,SCL,WRITE_DATA,MODE,[0,1,Cont_Mode]  ' Send continuous output command
    PAUSE 10 
    DO
      GOSUB GetRawReading                       ' Get RAW Compass reading
      serout2 porta.0,84, ["x ",SDEC x," y ",SDEC y ," z ",SDEC z,13,10]
      
        brads = -x ATN -y ;
        degr = brads */360 'convert to degrees
       ;serout2 porta.0,84, ["xy ",SDEC Radians ,","]
        serout2 porta.0,84, ["xy ",DEC degr ,13,10]
         brads = -x ATN -z ;
         degr = brads */360 'convert to degrees
       ; serout2 porta.0,84, ["xz ",SDEC Radians ,","]
        serout2 porta.0,84, ["xz ",DEC degr ,13,10]
           brads = -y ATN -z
           degr = brads */360 'convert to degrees
        ;serout2 porta.0,84, ["yz ",SDEC Radians ,","]
        serout2 porta.0,84, ["yz ",DEC degr ,13,10]
        
       PAUSE 2000 
    LOOP
    ' -----[ Subroutines ]--------------------------------------------------------
    GetRawReading:                                 'Compass module subroutine
                                        'Wait for new data to become available
        I2CREAD SDA,SCL,READ_DATA, X_MSB,[READX,READZ,READY]  'Read the data starting at x_msb 3
      
        x = readx >>2  
        Y = readY >>2
        Z = readZ >>2
    RETURN

  5. #45
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi Richard

    Had put this on the back burner for a whilst....

    Will revisit it later... I see you have done a few things differently!

    Thank you for Ideas. Andy
    Last edited by andybarrett1; - 22nd January 2015 at 11:17. Reason: typo

  6. #46


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi Richard,

    you need to add a 3-axis accelerometer to do tilt compensation, something like LSM303DLM.

    George

  7. #47
    Join Date
    May 2013
    Location
    australia
    Posts
    2,368


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    turns out that the premise of using this chip alone as a compass is basically flawed. it can't do its own tilt compensation because the earths magnetic field is not necessarily parallel to the earth .
    as George says an accelerometer needs to be added to compensate for tilt. (read, complicated math probably needs floats for any decent accuracy)

    no wonder they are so cheap , not a total loss it makes a pretty sensitive 3 axis hall effect sensor , nice colour

  8. #48


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Richard,

    Not if your application will not be required to tilt or has some mechanical (gimbals) way to negate the problem.

    George

  9. #49
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi both

    Yeah.... I put your code in Richard ... Does pretty much same as before... So I think I need find a better chip
    Or use a real needle

    Thank you again for ideas .... Appreciated !

  10. #50
    Join Date
    Dec 2012
    Location
    Türkiye
    Posts
    103


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Quote Originally Posted by richard View Post
    I bought one to play with

    after looking at the raw data I came up with this , works ok if device is held level , tilt compensation is elusive at this stage



    Code:
    '****************************************************************
    '*  Name    : hmc5883l.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 29/04/2011                                        *
    '*  Version :                                              *
    '*  Notes   :                                                *
    '*          :  12f1822     
    '*          :  
                 '
    '****************************************************************
     #CONFIG
    cfg1 = _FOSC_INTOSC
    cfg1&= _WDTE_ON
    cfg1&= _PWRTE_OFF
    cfg1&= _MCLRE_ON
    cfg1&= _CP_OFF
    cfg1&= _CPD_OFF
    cfg1&= _BOREN_ON
    cfg1&= _CLKOUTEN_OFF
    cfg1&= _IESO_ON
    cfg1&= _FCMEN_ON
      __CONFIG _CONFIG1, cfg1
    cfg2 = _WRT_OFF
    cfg2&= _PLLEN_OFF
    cfg2&= _STVREN_ON
    cfg2&= _BORV_19
    cfg2&= _LVP_OFF
      __CONFIG _CONFIG2, cfg2
    #ENDCONFIG
     
    
          osccon=$6A    '4 mhz
          anselA=0       'dig i/o
            
          trisA=1110
      
      
      
      
       pause 2000
      serout2 porta.0,84, ["ready",13,10]
      lata.0=1
      
      
       SDA            var      porta.2                 'transceives to/from SDA
       SCL            var      porta.4                  'sends clock pulses
    WRITE_DATA     CON     $3C  'Used to perform a Write operation
    READ_DATA      CON     $3D  'Used to perform a Read operation
    MODE           CON     $02  'Read/Write Register, Selects the operating mode. Default = Single measurement
                                 'Send $3C $02 $00 on power up to change to continuous measurement mode.
    X_MSB          CON     $03  'Read Register, Output of X MSB 8-bit value.
    X_LSB          CON     $04  'Read Register, Output of X LSB 8-bit value.
    Z_MSB          CON     $05  'Read Register, Output of Z MSB 8-bit value.
    Z_LSB          CON     $06  'Read Register, Output of Z LSB 8-bit value.
    Y_MSB          CON     $07  'Read Register, Output of Y MSB 8-bit value.
    Y_LSB          CON     $08  'Read Register, Output of Y LSB 8-bit value.
    Cont_Mode      CON     $0
    X              VAR      byte 'x sensor measured value
    Y              VAR      byte 'y sensor measured value
    Z              VAR      byte 'z sensor measured value
    READX         VAR      WORD 
    READY         VAR      WORD
    READZ         VAR      WORD
    brads          VAR      Word 'angle in brads
    degr           VAR      Word 'angle in degrees
    Radians        VAR      Word 'value calculated using X ATN -Y
    
    ' -----[ Main Routine ]-------------------------------------------------------
    PAUSE 50                                     ' Give sensor needed power up time
      I2CWRITE SDA,SCL,WRITE_DATA,MODE,[0,1,Cont_Mode]  ' Send continuous output command
    PAUSE 10 
    DO
      GOSUB GetRawReading                       ' Get RAW Compass reading
      serout2 porta.0,84, ["x ",SDEC x," y ",SDEC y ," z ",SDEC z,13,10]
      
        brads = -x ATN -y ;
        degr = brads */360 'convert to degrees
       ;serout2 porta.0,84, ["xy ",SDEC Radians ,","]
        serout2 porta.0,84, ["xy ",DEC degr ,13,10]
         brads = -x ATN -z ;
         degr = brads */360 'convert to degrees
       ; serout2 porta.0,84, ["xz ",SDEC Radians ,","]
        serout2 porta.0,84, ["xz ",DEC degr ,13,10]
           brads = -y ATN -z
           degr = brads */360 'convert to degrees
        ;serout2 porta.0,84, ["yz ",SDEC Radians ,","]
        serout2 porta.0,84, ["yz ",DEC degr ,13,10]
        
       PAUSE 2000 
    LOOP
    ' -----[ Subroutines ]--------------------------------------------------------
    GetRawReading:                                 'Compass module subroutine
                                        'Wait for new data to become available
        I2CREAD SDA,SCL,READ_DATA, X_MSB,[READX,READZ,READY]  'Read the data starting at x_msb 3
      
        x = readx >>2  
        Y = readY >>2
        Z = readZ >>2
    RETURN
    So according to your code , we get x , y , z raw readings as byte datas .... and xy xz yz calculations obtained using some trigonometry .. But what do those datas mean ? I think these xy xz yz readings are referring a direction but which one is what ? Does xy give me the magnetic north ? or is it xz or maybe yz ...

    I'd be so happy if any of you guys could help me with that ...

    Thanks ..

  11. #51
    Join Date
    May 2013
    Location
    australia
    Posts
    2,368


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    maybe this will help

    http://www.starlino.com/imu_guide.html
    Attached Images Attached Images  

  12. #52
    Join Date
    Dec 2012
    Location
    Türkiye
    Posts
    103


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    I still can't understand which vector shows me the magnetic north in this picture ... But as far as I read the datasheet of this IC , it mentions about a term called Hnorth which is the resultant force of X and Y vectors ... so which one is it then ? Is it XY calculation that shows me the north ?

  13. #53
    Join Date
    May 2013
    Location
    australia
    Posts
    2,368


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    if the device is held so that the xy plane is perfectly!!!! level then the xy angle is magnetic north (sort of). if the device is tilted then the xy angle needs to compensated for that tilt . the device alone cannot really be used as a compass . the device output is a 3d vector as drawn by the black line in my previous post, as you can see the result is relative to the devices orientation , if that is unknown (and it is without an external reference ) then its virtually impossible to ascertain a direction from its output. even a small tilt creates substantial error.
    furthermore magnetic field lines are not parallel with the earths surface they tilt into the earth or up to the sky depending on location. not to mention magnetic inclination/deviation etc
    basically the concept of using this chip as a compass is unworkable see post 47
    Last edited by richard; - 3rd February 2015 at 11:57. Reason: typo

  14. #54
    Join Date
    May 2013
    Location
    australia
    Posts
    2,368


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    I must add , the vector does not point north . it indicates the [ direction and magnitude ] of the magnet field relative to the device orientation.
    the link I posted is for accelerometers but the math is the same . the big difference is that gravity always points to the centre of the planet so orientation can be established
    Last edited by richard; - 3rd February 2015 at 12:13.

  15. #55
    Join Date
    Dec 2012
    Location
    Türkiye
    Posts
    103


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Quote Originally Posted by richard View Post
    if the device is held so that the xy plane is perfectly!!!! level then the xy angle is magnetic north (sort of). if the device is tilted then the xy angle needs to compensated for that tilt . the device alone cannot really be used as a compass . the device output is a 3d vector as drawn by the black line in my previous post, as you can see the result is relative to the devices orientation , if that is unknown (and it is without an external reference ) then its virtually impossible to ascertain a direction from its output. even a small tilt creates substantial error.
    furthermore magnetic field lines are not parallel with the earths surface they tilt into the earth or up to the sky depending on location. not to mention magnetic inclination/deviation etc
    basically the concept of using this chip as a compass is unworkable see post 47
    That was a nice a explanation Richard ... As you've previously said , we can at least use this IC to observe the pitch then ... For example I'm intending to read XZ or YZ to see the pitch angle for the control of an inverted pendulum ... I personally thought it's a low cost way of doing it compared to an encoder ... Bye the way .. For my module , should I keep it horizontally flat just like in the picture or upside down , does it matter ?

    Here is my module


  16. #56
    Join Date
    May 2013
    Location
    australia
    Posts
    2,368


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    For example I'm intending to read XZ or YZ to see the pitch angle for the control of an inverted pendulum ... I personally thought it's a low cost way of doing it compared to an encoder
    not sure how effective that will be , it would probably need to be calibrated every time you relocate it (or maybe even rotate it) . don't forget its own orientation is its only reference point . an accelerometer might be more appropriate

  17. #57
    Join Date
    Dec 2012
    Location
    Türkiye
    Posts
    103


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Well , Yeah actually I'm planning to calibrate it right before every operation with a quick push button click ... So that It can assume the present values as exact zero .. It's all about math I guess .... I'm also aware of that It won't be that accurate ... But I still wanna use it ... Thanks for your all prompt answers Richard ...

  18. #58


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    For example I'm intending to read XZ or YZ to see the pitch angle for the control of an inverted pendulum ... I personally thought it's a low cost way of doing it compared to an encoder
    Surely what you need is an accelerometer, you know that the period is always the same, it just moves faster when first set in motion (I'm not totally sure that's correct, my knowledge of basic physics is pretty ragged). If that's correct, there will be a relationship between angle and acceleration. With calibration you should end up with a look up table of acceleration/angle. Of course you'll only need to generate the table once.

    Also I cant imagine how you would orient the magnetometer to even hint at the angle.

    BTW anyway, you couldn't use an encoder if you want the pendulum to be "true", pivot must be frictionless.

    Sounds like an interesting project, have fun.

    George

Similar Threads

  1. 3 axis digital compass sensor HMC5883L
    By ScaleRobotics in forum General
    Replies: 1
    Last Post: - 8th June 2014, 23:19
  2. How to use this IO chip?
    By wdmagic in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th April 2013, 20:06
  3. compass chip
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th October 2009, 02:42
  4. Do you use ICD possibilities of MicroCode Studio
    By octal in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 14th December 2007, 18:22
  5. Which chip do i use?
    By The Master in forum Off Topic
    Replies: 77
    Last Post: - 2nd December 2007, 23:28

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts