HMC5883L Magnetometer chip Possibilities.


Closed Thread
Results 1 to 40 of 58

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi,

    is any body try the code from parallax site and get good results.I connect HMC5883L module to PIC18f452 and 2x16LCD display and I can read the RAW values.If I turn module RAW values are changing , but the value oh azimuth and degree dont work.

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


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Quote Originally Posted by visnja30 View Post
    Hi,

    is any body try the code from parallax site and get good results.I connect HMC5883L module to PIC18f452 and 2x16LCD display and I can read the RAW values.If I turn module RAW values are changing , but the value oh azimuth and degree dont work.
    Any chance of sharing code.... We can see what might be happening ??

    BR
    Andy

  3. #3
    Join Date
    Oct 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi,
    here is code with I test the MHC5883L chip.
    I can get some raw readings but I dont now are they correct.Range of readings are from 64000 to 1000.when I turn the module reading are change.

    Code:
    ' HMC5883L_Demo.bs2 - This is a Demo program for the HMC5883L Compass
    ' Module (PN: 29133). It displays the compass heading north, east, south and west
    ' clockwise from north.
    '
    ' Author.... (C) John A. Williams
    '
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    ' ============================================================================
         define OSC 4
    
    'KONFIGURACIJA LCD DISPLEJA 2X16
    
            DEFINE  LCD_DREG PORTB
           	DEFINE  LCD_DBIT 0
        	DEFINE  LCD_BITS 4
        	DEFINE  LCD_RSREG PORTB
        	DEFINE  LCD_RSBIT 4
        	DEFINE  LCD_EREG PORTB
        	DEFINE  LCD_EBIT  5
        	DEFINE  LCD_LINES 2
          	DEFINE  LCD_COMMANDUS 2000
        	DEFINE  LCD_DATAUS 40
     'SET UP PORTS
    TRISA = %00111111    'port a je cijeli ulazni
    TRISB = %00000000
    TRISC = %11111111
    TRISD = %00000000
    TRISE = %1111
    ADCON1 = 7	   ' PORTA i PORTE su digitalni  ISKLJUCI KOMPARATORE I ANALOGNE ULAZE
    ' -----[ Pins/Constants/Variables ]-------------------------------------------
    SDA            var      PORTC.4                   
    SCL            VAR      PORTC.3                   
    
    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
    SCALEFACTOR    CON     4081 'SCALEFACTOR = 65536 * (255/4095) = 4081
    SCALEOFFSET    CON     2048 'The output range of the HMC5883L is -2048 to 2047; therefore to get a 0 (LOWRANGE)to 4095 range
                                'to 4095 (HIGHRANGE) range 2048 must be added to the sensor value.
    ATNFACTOR      CON     127  'the ATN function input range is -127 TO 127. In order to get the positive scaled range of
                                '0 to 255 into the range of -127 to 127, 127 must be subtracted.
    LOWRANGE       CON     0    'see SCALEOFFSET
    HIGHRANGE      CON     4095 'see SCALEOFFSET
    
    X              VAR      Word 'x sensor measured value
    Y              VAR      Word 'y sensor measured value
    Z              VAR      Word 'z sensor measured value
    brads          VAR      Word 'angle in brads
    degr           VAR      Word 'angle in degrees
    azimuth        VAR      Word 'value calculated using X ATN -Y
      degr=0
      
      
      
      
      
     PAUSE 500
      LCDOUT $FE,$01'clear DISPLAY
      LCDOUT $FE,$01,"test "
      PAUSE 500
      LCDOUT $FE,$01'clear DISPLAY
     
     
      
      
    ' -----[ Main Routine ]-------------------------------------------------------
    
    PAUSE 10
       I2Cwrite SDA,SCL, $3c, $00, [$70]  '(8-average, 15 Hz default, normal measurement)                                   
       I2Cwrite SDA,SCL, $3c, $01, [$A0] ; $00 hoogste gain setting
       I2CWRITE SDA,SCL,WRITE_DATA,MODE, [cont_Mode]  ' Send continuous output command
    
    main:
        LCDOUT $FE,$01'BRISI DISPLAY
       GOSUB GetRawReading                       ' Get RAW Compass reading
    
      'RAW X,Y,Z data...
     
      lCDOUT $FE,$80,   "X= " , sdec x
      LCDOUT $FE,$80+8, "Y= ",   sdec y
      LCDOUT $FE,$C0,   "Z= ",  sdec z        
    
    
      'Calculate Azimuth
    
      GOSUB GetAzimuth
    
      brads = azimuth     'get angle
      degr = brads */ 361 'convert to degrees
     
      LCDOUT $FE,$80,"Degree " , sdec degr
     
      pause 500
    goto main
    
    ' -----[ Subroutines ]--------------------------------------------------------
    
    GetRawReading:                                 'Compass module subroutine
    
    
        PAUSE 400                                  'Wait for new data to become available
    
    
        I2CREAD SDA,SCL, READ_DATA, X_MSB, [X.HIGHBYTE]  'Read the data starting at x_msb 3
        I2CREAD  SDA,SCL, READ_DATA ,X_LSB, [X.LOWBYTE]
    
       I2CREAD  SDA,SCL, READ_DATA, Z_MSB, [Z.HIGHBYTE]
       I2CREAD SDA,SCL, READ_DATA ,Z_LSB, [Z.LOWBYTE]
       
        I2CREAD SDA,SCL, READ_DATA, Y_MSB, [Y.HIGHBYTE]
        I2CREAD  SDA,SCL, READ_DATA ,Y_LSB, [Y.LOWBYTE]
    
    RETURN
    
    GetAzimuth:
    'The output range of the HMC5883L is -2048 to 2047 and the input range of the ATN function
    'is -127 to 127.  I used the method to scale the output to the input discribed in Parallax's Smart
    'Sensors Student Guide v1 starting on page 76.
    'SCALEFACTOR = 65536 * (255/4095) = 4081
    'SCALEOFFSET = 2048
    'ATNFACTOR = 127
    'LOWRANGE = 0
    'HIGRANGE = 4095 (2048 + 2047 = 4095)
    
        x = x + SCALEOFFSET
        x = (x MIN LOWRANGE  MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR
    
        y = y + SCALEOFFSET
        y = (y MIN LOWRANGE MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR
    
    
        azimuth = x ATN -y
       LCDOUT $FE,$c0,"azimuth " ,  DEC azimuth
     
    RETURN

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


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    am I wrong or would right shifting the reading 4 give the same answer without all the math
    x var byte
    reading var word
    x= reading>>4

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi,

    Shooting from the hip a bit here....

    Since the ATN expects 8bit two's complement values I'm not sure it's going to work when you give it your x and y variables since they are both 16bit variables - even if the values in them are in +/-127 range. Ah, never mind, it probably will since the low byte of the word will look identical anyway....

    Richard,
    You can't simply shift a two's complement number like that, you'll drop the sign.
    You need to remember the sign, shift the abs value and restore the sign
    Code:
    Sign VAR BIT
    Reading VAR WORD
    x VAR BYTE
    
    Sign = Reading.15
    x = ABS(Reading) >> 4
    If Sign THEN x = -x
    /Henrik.

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,656


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    it works on paper , i'm curious to compile it and see what happens , i'm feeling a cast from word to byte and only a 4 bit r-shift might allow the loss of the sign bit to be ignorable . if it works it saves a lot of steps
    I would christen it as quick and dirty code

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi,
    You're quite right Richard!
    It works provided that x in this case IS declared as a byte (which it was in your code of course)
    Code:
    x = Reading >> 4
    HSEROUT["Reading: ", SDEC Reading, "   X: ", SDEC x,10,13]
    Code:
    Reading: -2048   X: -128
    Reading: 2047   X: 127
    Reading: -100   X: -7
    Reading: 100   X: 6
    If x is declared as a WORD then it fails:
    Code:
    Reading: -2048   X: 3968
    Reading: 2047   X: 127
    Reading: -100   X: 4089
    Reading: 100   X: 6
    /Henrik.

  8. #8
    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 10:17. Reason: typo

  9. #9


    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

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    2,656


    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

  11. #11


    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

  12. #12
    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 !

Similar Threads

  1. 3 axis digital compass sensor HMC5883L
    By ScaleRobotics in forum General
    Replies: 1
    Last Post: - 8th June 2014, 22:19
  2. How to use this IO chip?
    By wdmagic in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th April 2013, 19:06
  3. compass chip
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th October 2009, 01: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, 17:22
  5. Which chip do i use?
    By The Master in forum Off Topic
    Replies: 77
    Last Post: - 2nd December 2007, 22:28

Members who have read this thread : 0

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