Using a QMC5883L 3 axis compass sensor


+ Reply to Thread
Results 1 to 12 of 12

Thread: Using a QMC5883L 3 axis compass sensor56 days old

  1. #1
    Member basparky's Avatar
    Join Date
    Oct 2006
    Posts
    253
    Thumbs Up
    Received: 10
    Given: 31
    Total Downloaded
    751.89 MB

    1 Not allowed!

    Default Using a QMC5883L 3 axis compass sensor

    Hi,

    I bought a 3 axis compas magnetometer sensor and would like to use it in a project.

    The sensor is mounted on a breakout board See:http://wiki.sunfounder.cc/index.php?title=QMC5883L

    I
    have connected the board to a dspic33FJ128MC706A with the pullups on the sda and scl lines. There is also an external eeprom connected to these lines which functions as expected.

    Following the datasheet https://www.tinytronics.nl/shop/image/data/Producten/Sensoren/Magnetisch veld/Datasheet-QMC5883L-1.0 .pdf
    it tells me on page 13 the registers i need to setup.
    I made:
    Code:
    Proc InitCompass()BStart
    BusOut 011010
    BusOut 0xB
    BusOut 0x01
    BStop
    
    
    BStart
    BusOut 011010
    BusOut 0x9
    BusOut 0x1D
    BStop
    
    
    BusOut 011010
    BusOut 0xA
    BusOut 0x80
    BStop
    DelayMS 5
    
    
    EndProc
    Following this datasheet on page 15 it tells me how to write and read.

    I thought by doing above for writing the config registers and this for reading the data registers i would be able to get some data from the module...

    BStart BusOut 011010,[0x00]
    DelayMS 1
    BStart
    BusIn 011011,[0x00]
    BusIn XCom1
    BusAck
    BusIn XCom2
    BStop
    This doesnt give any results.

    Does anyone has some experience with this compass sensor?

    Thanks for any help!

  2. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Member basparky's Avatar
    Join Date
    Oct 2006
    Posts
    253
    Thumbs Up
    Received: 10
    Given: 31
    Total Downloaded
    751.89 MB

    0 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    Got it working!
    Different approach as above with success:
    At startup i do:
    Code:
    Proc InitCompass()    
        BusOut 011010, 0xB,[0x01]        'Set reset period, Datasheet page 13 for application example.
        DelayMS 5
        BusOut 011010, 0x9,[000001]   'Continuous measurement mode
    EndProc
    then i read every 100ms the first 6 registers. Datasheet page 16 and calculate the heading:
    Code:
    Proc GetCompass()     
         BusIn  011011,0x00,[XCom.LowByte,XCom.HighByte, YCom.LowByte,YCom.HighByte,ZCom.LowByte,ZCom.HighByte]
         Heading = ReadHeading()
    EndProc
    Code:
    Proc ReadHeading(),WordDim x As SWord
    Dim y As SWord
    Dim fx As Float
    Dim fy As Float
    Dim Heading As SWord
    Dim DeclinationAngle As Float
    
    
        If XCom < Xlow Then Xlow = XCom
        If XCom > XHigh Then XHigh = XCom
        If YCom < Ylow Then Ylow = YCom
        If YCom >YHigh Then YHigh = YCom
        
        If Xlow = XHigh Or Ylow = YHigh Then 
            Heading = 0
            Result = 0
            Return
        EndIf
        
        DeclinationAngle = (1 + (56/60))/(180/pi)  'For Grootebroek in the Netherlands;)
        
        x = XCom - (XHigh + Xlow) /2
        y = YCom - (YHigh + Ylow) /2
        
        fx = x/(XHigh - Xlow)
        fy = y/(YHigh - Ylow)
            
        Heading = 180 * ATan2( fy,fx)/ Pi
        Heading = Heading - 80 ; Somehow i need to correct with -80 (compared to telephone compass which looks ok).   
        If Heading <=0 Then Heading = Heading + 360
        
        HRSOut "Xcom= ",SDec XCom," xlow= ",SDec Xlow," xhigh=",Dec XHigh," x= ",SDec x," fx= ",SDec fx," Heading= ",Dec Heading ,"DeclinationAngle= ",SDec DeclinationAngle,"\r\n"
        
    Result = Heading
    
    
    EndProc
    There is a huge error from +80 for the calculated heading which i still need to fix somehow..

  4. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,899
    Thumbs Up
    Received: 385
    Given: 196
    Total Downloaded
    2.09 GB

    1 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    Excellent code! Proton makes it look so easy, even though it is not. :-)

    Make sure you are converting degrees to radians for the compiler's trig procedures, and back again for viewing etc...

    I've been caught out like that in the past, when I forgot to convert, or didn't get the divisions etc in the correct place. LOL

    I can see a 180 and 360 values and Pi in the code, so make sure they are in the correct positions for radian/degrees conversions etc...
    Last edited by top204; 1st December 2020 at 20:35.

  6. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    Member david's Avatar
    Join Date
    Jan 2019
    Posts
    114
    Thumbs Up
    Received: 5
    Given: 1
    Total Downloaded
    174.52 MB

    0 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    Proton certainly makes OUR code look easy and there are times when I think it's ridiculously concise compared to other languages.
    I've not used the QMC5883L (Chinese knock-off) but have used the original Honeywell HMC5883. The two are almost identical with only some minor changes in the config set up.

    This is what I used to read the bare sensor values of the HMC5883-

    Declare Hbus_Bitrate 100 '100kHz or 400kHz


    '************* I2C comms set up **************************************
    SSPCON1=%00111000 'enable port, 7 bit address, master mode


    '*********** Set up *********************
    Symbol MAG_W= %11110000 'Device address and write
    Symbol MAG_R= %11110001 'Device address and read 7 6 5 4 3 2 1 0


    '********** ADC variables ******************
    Dim MAGX As Word
    Dim MAGY As Word
    Dim MAGZ As Word


    'initialize
    HBusOut MAG_W,[0,112] 'Write, Reg 0, 112 112=8avg, 15Hz rate
    HBusOut MAG_W,[1,160] 'Write, Reg 1, Gain 5


    begin:
    HBusOut MAG_W,[2,1] 'Write, Reg 2, Single shot mode
    DelayMS 10 'pointer auto increments
    HBusIn MAG_R,[MAGX, MAGY, MAGZ] 'returns 16 bit value of 3 mag axis



    '*************** output data ****************************


    HRSOut "X=",Dec MAGX," ","Y=",Dec MAGY, " ","Z=",Dec MAGZ,13 '9600 direct - no inverter.
    DelayMS 500
    GoTo begin

    If you take away the comments there's not much left. Proton really is a work of art.

    Cheers,
    David

  8. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  9. #5
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,899
    Thumbs Up
    Received: 385
    Given: 196
    Total Downloaded
    2.09 GB

    0 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    I've just noticed your code is:

    Code:
    Proc ReadHeading(),WordDim x As SWord
    I know this is a forum thread issue that has removed a carriage return from the line, so I'll just alter it for other users. It should have read:

    Code:
    Proc ReadHeading(),Word
    
    Dim x As SWord
    Again.... Excellent code from basparky and David. It is such a good feeling when people with skills and knowledge are using a language I created and finding it useful. :-)
    Last edited by top204; 1st December 2020 at 23:20.

  10. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  11. #6
    Member flosigud's Avatar
    Join Date
    Oct 2013
    Posts
    226
    Thumbs Up
    Received: 19
    Given: 16
    Total Downloaded
    3.28 GB

    0 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    A HMC5883L library that I adapted from somewhere.


    Code:
    '    ----------------------------------------------------------------------------'    Name    : HMC5883L.INC                                      
    '    Author  : Flosi Guđmundsson                                                          
    '    Date    : 24.10.2020                                        
    '    Version : 1.0                                               
    '    Notes   :                                                   
    '            :                                                   
    '    ----------------------------------------------------------------------------
    
    
    
    
    '    HMC5883L_init
    '    Float = GetHeading
    
    
    
    
    '
    '    HMC5883 registers
    '    ----------------------------------------------------------------------------
    '
        Symbol HMC5883_CRA_REG            = 0        ' Configuration_Register_A
        Symbol HMC5883_CRB_REG            = 1        ' Configuration_Register_B
        Symbol HMC5883_MR_REG            = 2        ' Mode_Register
        Symbol HMC5883_OUT_X_H            = 3        ' Data_Output_X_MSB_Register
        Symbol HMC5883_OUT_X_L            = 4        ' Data_Output_X_LSB_Register
        Symbol HMC5883_OUT_Z_H            = 5        ' Data_Output_Z_MSB_Register
        Symbol HMC5883_OUT_Z_L            = 6        ' Data_Output_Z_LSB_Register
        Symbol HMC5883_OUT_Y_H            = 7        ' Data_Output_Y_LSB_Register
        Symbol HMC5883_OUT_Y_L            = 8        ' Data_Output_Y_MSB_Register
        Symbol HMC5883_SR_REG            = 9        ' Status_Register_Read
        Symbol HMC5883_IRA_REG            = 10    ' Identification_Register_A
        Symbol HMC5883_IRB_REG            = 11    ' Identification_Register_B
        Symbol HMC5883_IRC_REG            = 12    ' Identification_Register_C
        Symbol HMC5883_TEMP_OUT_H        = $31    '
        Symbol HMC5883_TEMP_OUT_L        = $32    '
    
    
        
        Symbol X_MSB = $03                         ' Read Register, Output of swXaxis MSB 8-bit value.
        Symbol X_LSB = $04                        ' Read Register, Output of swXaxis LSB 8-bit value.
        Symbol Z_MSB = $05                        ' Read Register, Output of swZaxis MSB 8-bit value.
        Symbol Z_LSB = $06                        ' Read Register, Output of swZaxis LSB 8-bit value.
        Symbol Y_MSB = $07                        ' Read Register, Output of swYaxis MSB 8-bit value.
        Symbol Y_LSB = $08                        ' Read Register, Output of swYaxis LSB 8-bit value.
    
    
        Symbol WRITE_ADDRESS = $3C                ' Requests Write operation
        Symbol READ_ADDRESS = $3D                ' Requests Read operation
        Symbol MODE = $02                        ' Mode setting register
        Symbol Pi = 3.141592654
        Symbol Pi2 = 6.283185307
       
        Symbol DeclinationAngle = -13.233333333333333/57.295779
        
    '
    '    HMC5883L_init()
    '    ----------------------------------------------------------------------------
    '
        $define HMC5883L_init _HMC5883L_init    
        Proc _HMC5883L_init()
    '        Global Dim swXaxis As SWord                        ' swXaxis sensor measured value
    '        Global Dim swYaxis As SWord                        ' swYaxis sensor measured value
    '        Global Dim swZaxis As SWord                        ' swZaxis sensor measured value
        
            Global Dim Heading As Float
            HBusOut WRITE_ADDRESS, HMC5883_CRA_REG, [$70]    ' (8-average, 15 Hz default, normal measurement)
            HBusOut WRITE_ADDRESS, HMC5883_CRB_REG, [$A0]    ' Gain=5
            HBusOut WRITE_ADDRESS, HMC5883_MR_REG, [$00]    ' Send continuous output command
        EndProc
    
    
    ' 
    '    GetHeading()
    '    ----------------------------------------------------------------------------
    '
        $define GetHeading _GetHeading
        Proc _GetHeading(), Float
            HBusIn READ_ADDRESS, $03, [swXaxis.HighByte, swXaxis.LowByte, swZaxis.HighByte, swZaxis.LowByte, swYaxis.HighByte, swYaxis.LowByte]
            Dim fXaxis As Float = swXaxis                    ' Convert the numbers to Float
            Dim fYaxis As Float = swYaxis                    ' Else Result will be garbage
            Dim swXaxis As SWord                            ' swXaxis sensor measured value
            Dim swYaxis As SWord                            ' swYaxis sensor measured value
            Dim swZaxis As SWord                            ' swZaxis sensor measured value
            Result = ATan2(fYaxis, fXaxis)                    ' Calculate the ATan2
            Result = Result + DeclinationAngle                ' DeclinationAngle is different in every place
            If Result < 0 Then Result = Result + Pi2        ' Add 2 * Pi
            If Result > Pi2 Then Result = Result - Pi2        ' Subtract 2 * Pi
            Result = Result * 57.295779                        ' Convert radians to degrees
        EndProc

    This is how I use it.

    Code:
    '    ----------------------------------------------------------------------------'    Name    : HMC5883L_demo.BAS                                      
    '    Author  : Flosi Guđmundsson                                           
    '            : All Rights Reserved                               
    '    Date    : 10.7.2020                                         
    '    Version : 1.0                                               
    '    Notes   :                                                   
    '            :                                                   
    '    ----------------------------------------------------------------------------
    
    
    '    Include "..\..\33fj128GP802.inc"
        Include "..\..\33FJ12MC202.inc"
    '    Include "..\..\..\33FJ32MC302.inc"
    
    
    '    ----------------------------------------------------------------------------
    
    
        Include "HMC5883L.Inc"
    
    
    
    
        HMC5883L_init()
        DelayMS 200
        Do
            Heading = GetHeading()
            HRSOut "Heading : ",Dec Heading," N   ",13
            DelayMS 200
        Loop 
        
    '    ----------------------------------------------------------------------------
    Flosi Guđmundsson
    Reykjavík

  12. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  13. #7
    Member basparky's Avatar
    Join Date
    Oct 2006
    Posts
    253
    Thumbs Up
    Received: 10
    Given: 31
    Total Downloaded
    751.89 MB

    0 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    Hi,

    I noticed the following:
    If i use:
    Code:
         
    Dim wHeading as Sword
    
    If wHeading < -90 Then wHeading = -90
    If wHeading > 90 Then wHeading = 90
    wHeading is always -90

    If i use:
    Code:
         
    Dim MinH As SWordMinH = -90
    If wHeading < MinH Then wHeading = -90
    If wHeading > 90 Then wHeading = 90
    It works as expected.

    Is this my mistake?

  14. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  15. #8
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,899
    Thumbs Up
    Received: 385
    Given: 196
    Total Downloaded
    2.09 GB

    0 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    I've just ran a test on an 18F device and an enhanced 14-bit core device and the comparison is working as expected. With this code snippet:

    Code:
        Dim wHeading As SWord
    
        wHeading = -100
        If wHeading < -90 Then
            HRSOutLn "Less Than"
        Else
            HRSOutLn "Greater or Equal"
        EndIf
    It transmits the corrects texts for the correct comparison depending on what value is loaded into wHeading
    Last edited by top204; 16th December 2020 at 20:29.

  16. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  17. #9
    Member basparky's Avatar
    Join Date
    Oct 2006
    Posts
    253
    Thumbs Up
    Received: 10
    Given: 31
    Total Downloaded
    751.89 MB

    0 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    Thanks for your time.

    If i test the following in a dsPic33FJ32MC204 i always get the value -90 as result. Doenst matter what value i put in ActualHeading..
    Code:
    Dim ActualHeading As SWord
    
    
    ActualHeading = 500
    If ActualHeading < -90 Then ActualHeading = -90
    If ActualHeading > 90 Then ActualHeading = 90 
    
    
    HRSOut SDec ActualHeading,"\r\n"
    If i use your code with the value 500 in wHeading i also get "Less than" as result

    If is use the following it works as i thought it should and gives me as result "Greater or Equal"
    Code:
        Dim wHeading As SWord
        Dim Value As SWord
        Value = -90
        wHeading = 500
        If wHeading < Value Then
            HRSOutLn "Less Than"
        Else
            HRSOutLn "Greater or Equal"
        EndIf
    Last edited by basparky; 17th December 2020 at 14:30.

  18. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  19. #10
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,899
    Thumbs Up
    Received: 385
    Given: 196
    Total Downloaded
    2.09 GB

    0 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    This is in the Proton ver 3 section of the forum, so I automatically assumed it was a possible anomaly with an 8-bit device!

    I'll check out Proton24 now.

  20. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  21. #11
    Member basparky's Avatar
    Join Date
    Oct 2006
    Posts
    253
    Thumbs Up
    Received: 10
    Given: 31
    Total Downloaded
    751.89 MB

    0 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    Sorry! did not realize i was in the wrong section

  22. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  23. #12
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,899
    Thumbs Up
    Received: 385
    Given: 196
    Total Downloaded
    2.09 GB

    1 Not allowed!

    Default Re: Using a QMC5883L 3 axis compass sensor

    There is an anomaly in Proton24 for this comparison. The compiler is producing the negative constant value as 32-bit within its source, so it does not see the values correctly when negative.

    Change the variable "ActualHeading" to an SDword type and the comparison works as expected.

    I've now corrected the anomaly and looked for any more that may cause the issue with negetive constants on Byte and Word comparisons and will have an update for the anomaly fix ASAP.

    Thanks for spotting that obscure anomaly. :-)

  24. Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Amicus 18 and pressure compass sensor HDPM01
    By Broderic in forum Amicus 18
    Replies: 5
    Last Post: 9th June 2020, 10:19
  2. H3LIS331DL low-power high-performance 3-axis linear accelerometer
    By normnet in forum New devices and techniques
    Replies: 0
    Last Post: 23rd April 2015, 16:31
  3. New MAX21105 3-axis gyroscopes plus 3-axis accelerometer
    By normnet in forum New devices and techniques
    Replies: 0
    Last Post: 21st February 2015, 17:47
  4. New ST LIS2HH12 three-axis linear accelerometer
    By normnet in forum New devices and techniques
    Replies: 0
    Last Post: 21st February 2015, 17:31
  5. [SOLVED !] HM55B Compass need help
    By dsmith in forum Proton Plus Compiler v3
    Replies: 1
    Last Post: 30th October 2005, 20:33

Posting Permissions

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