GY-271 HMC5883L 3-Axis Magnetic Electronic Compass Module

cachomachine

Senior Member
Has anyone succeeded in getting an accurate reading of the X axis in real degrees on a GY-271 HMC5883L 3-Axis Magnetic Electronic Compass Module?
 

AllyCat

Senior Member
Hi,

You need two Axes (X-Y , X-Z or Y-Z) to determine an angle. Have you managed to read any (signed) values from the device?

One you have data (field strengths) for two (orthogonal) Axes, I've written several Code Snippets (of varying complexity) for the required ATAN2 function.

Cheers, Alan.
 

AllyCat

Senior Member
Hi,

I've not used that device and it's obviously a little complicated, but the data sheet seems quite straightforward. The Default settings look reasonable, but I would at least select the "continuous" mode.

As a "sanity check" I would first read all the Configuration, Mode, Status and Identification registers (0-2 and 9-12) and check that they are "as expected" (from the data sheet). Then you need to convert the three High/Low Byte values to PICaxe Words, noting that the MSB is sent first whilst PICaxe uses LSB first.

You will need to understand the "Twos Complement" number format and send the X, Y and Z values to the Terminal Emulator. Preferably rotate the sensor about 30 - 45 degrees each time around the Z-axis for a full revolution and post what you measure. The following subroutine may help with the 2s complement format (12 bits) as it appears to be used by the device.
Code:
; Demo Values
w1 = 1234 : call showW1signed
w1 = 2048 : call showW1signed
w1 = -4096 : call showW1signed
w1 = 65500 : call showW1signed
end

showW1signed:       ; Send a 2's complement value in w1 to the terminal
w2 = w1
if w2 => $F800 then
      w2 = -w2
      sertxd(" -")
endif
if w2 = $F000 then
      sertxd(" OVERFLOW ")
else if w2 => 2048 then
            sertxd(" ERROR ")
else
      sertxd(#w2," ")
endif
return
Cheers, Alan.
 

PicaxUser

Member
Hi Alan,

I am in the process of designing a Fish Seeker using plastic water pipes. That will use a Picaxe to control it.

(123) Fish Seeker Instructional Video - YouTube

It needs a magnetic compass to make it go straight away from the beach.

You indicated:
I've written several Code Snippets (of varying complexity) for the required ATAN2 function.

Is it possible to get a copy of the ATAN2 function code and maybe scaling software to scale down the X, Z, Y from the HMC5883L to suit the ATAN2 function.

Hoping that you may be able to help.

I plan to publish the project when finished.

Thanks,

Rod.
 

AllyCat

Senior Member
Hi Rod,

Welcome to the forum. Are you located in Australia by any chance? ;)
... I've written several Code Snippets (of varying complexity) for the required ATAN2 function.
Yes, a search of the forum for "ATAN2" and my user name should lead you HERE , but I didn't include that link before because it's a complex function which didn't seem appropriate for the OP (i.e. the Original Post); it uses quite (or even very) "Advanced" Programming concepts (to a typical PICaxe user). The link should lead directly into my recent "Update" post, particularly for the ATAN2 application, but you should also read the thread from the beginning (i.e. post #1) for a full introduction.

As said above, I've never used the HMC5883L , but that code snippet was very much intended to use a resolution of around 12-bits, so the values shouldn't need too much "scaling". However, I'm not a fisherman so I'm not planning to watch a 17 minute video, and the "comments" appear to be turned off (for me?) with that video. There should be "sufficient" information in that full Code Snippet thread, but feel free to ask any questions if any clarification is still required.

Cheers, Alan.
 

PicaxUser

Member
Hi Alan,

Yes we are from sometimes Sunny Melbourne Victoria Australia. :). We live near the the beach on Port Philip Bay.

OK on not including it in the Original Post. Yes ATAN2 programing is advanced complex programing. Thanks for the link to your Post on it. Will read it after sending this post.

Yes watching the video is a bit long. I included it as it gives an idea of what I am doing. The Electronic magnetic compass does not have to be accurate with reference to knowing where North is. As you point it in the direction you want it to go and use the compass to keep it going in that direction. As in the video.

There should be "sufficient" information in that full Code Snippet thread, but feel free to ask any questions if any clarification is still required.

Thanks for your help, I will let you know how we go with it.

Cheers, Rod.
 

AllyCat

Senior Member
Hi,

The relatively complex code in my complete Snippet thread is probably "overkill" for your requirement, so here are two links to discussion of the ATAN function, if you haven't already found them via my snippet thread:

Post #3 HERE and the code in Post #20 HERE. Then sections of my snippet might be adopted to extend the basic ATAN function beyond its raw 0 to 45 degrees.

Cheers, Alan.
 
Last edited:

PicaxUser

Member
Hi,

You need two Axes (X-Y , X-Z or Y-Z) to determine an angle. Have you managed to read any (signed) values from the device?

One you have data (field strengths) for two (orthogonal) Axes, I've written several Code Snippets (of varying complexity) for the required ATAN2 function.

Cheers, Alan.
 

PicaxUser

Member
Hi Alan,

I got lost here in Forums. And getting a reply back. So I am posting from here.

Thanks for the link to the ArcTan routine. That looks good for 45 deg. 45 deg which is 1/4 of 360 deg.

I understand that the x and y data from the HMC5883L can be positive of negative. That maybe what can indicate which 1/4 of 360deg the X and Y data is for?

I have on order a HMC5883L chip and are waiting to receive it. And have programed a Arduino UNO to read the X and Y data from it. And see what can be used from that?

I only have a Picaxe 18M2 chip. So I need to order a 28X1 that has Sin and Cos functions.

Thanks,

Rod.
 

AllyCat

Senior Member
Hi Rod,
I only have a Picaxe 18M2 chip. So I need to order a 28X1 that has Sin and Cos functions.
The 28X1 is an "obsolete" chip (it's normally now a 28X2) and a 20X2 might be a better choice because it fits in the same socket as a 20M2 (which is basically similar to the 18M2, but has minor improvements on it). However, IMHO (In My Opinion) the SIN and COS functions are not a good reason for upgrading from the M2 to X2 family. Note that details of these (and many similar) functions can be found in the "Variables - Unary Mathematics" section of the PICaxe User Manual (Part 2). You can also test out the functions by using the PICaxe Simulator within PE5 or PE6. For example, try the following (which can also indicate which functions are available in each chip), seeing if you can understand the value it produces (197) : ;)
Code:
#picaxe 20x2
#terminal 9600          ; Use 4800 for M2 family
b1 = cos 135            ; Degrees
sertxd(#b1)
do : loop               ; Keep Terminal window open in PE5 simulator
The PICaxe SIN and COS functions basically use just a lookup table in 2 degree steps, which is probably just about "good enough" for your application (but most of the M2s also have plenty of spare TABLE memory). The X2 functions do support angles greater than 90 degrees, BUT they use an (embedded) "flag" within the number (= +128) to indicate negative numbers, rather than than the conventional "Two's Complement" number representation. Two's Complement is perhaps a "difficult" concept, but it's well worth learning, because it's the format that the HMC5883L and the Arduino (and any "serious" mathematical calculations) will use. Unfortunately, PICaxe Basic doesn't directly "support" 2's complement (i.e. Negative) numbers, but it is quite easy to use Subroutines or Macros to work with them.

There are a number of examples of using Negative numbers and Trigonometry in programs from me (and others) on the forum, but they do tend to be quite "Advanced", for example HERE , HERE and HERE. Tip: I usually open Hot Links with a Right-Click and then "Open New TAB" or "Open New Window". :)

Cheers, Alan.
 

PicaxUser

Member
I Alan,

Thanks for the links I had a quick look at them. Will study them more after sending this post.

I was thinking that for my project I will not have a display to show the heading in degrees. Because I just point the Fish seeker in the direction I want it to go. Press a button to read the values of Y and X from the HMC5883L which will show the direction it is to travel (which 90 deg section follows later) . ArcTan (Opp/Adj) is used to calculate the degrees. As I do not need to know the direction in degrees. I only need to know the ratio of Y and X which is (Opp/Adj).

Y and X can be either a positive number or negative number. That gives 4 values (+Y, +X) (+Y, -X) (-Y, +X) (-Y, -X). This may? indicate which 90 deg 1/4 of the 360 deg circle.

After pressing the button to indicate which direction to travel in. It has stored the ratio of Y and X and which 90 deg 1/4 of 360 deg.

To travel in the correct direction. 1st get into the correct 90 deg 1/4 of 360 deg. If (Y/X) ratio increases with reference to the stored direction. Then turn the steering wheel to correct the direction. If the (Y/X) ratio decreases then turn the steering wheel in the opposite direction to correct the direction.

I am still waiting for the arrival of the HMC5883L to see if the above is correct. I could be wrong?

This also allows me to use the 18M2 Picaxe that I have.

Thanks,

Rod.
 

PicaxUser

Member
Hi Alan,

Ok that the 28X1 is an "obsolete" chip and keeping with the 18M2 chip, and using subroutines to do mathematic functions. Good idea. I will stay with the 18M2 chip. Yes the HMC5883L output are in 2's complement numbers. We have no problems with 2's Complement numbers as we have been with them for many years now. OK in that a subroutine will be needed to handle 2's complement numbers in Picaxe Basic.

I am still working through the examples of using Negative numbers and Trigonometry in programs from yourself and others. These are good to learn about and how to use them.

As well as doing the Fish seeker project, just for fun I still want to produce a compass read out in degrees using the HMC5883L.

For everyone's information:
Honeywell has decided to abandon its MHV5883L/HMC5783L product line suddenly this Q1. A Alternative/Replacement is the Isentek IST8310 chip a Pin2Pin alternative to the HMC5883L. With a more powerful 14 bits ADC, a broader dynamic range (+/- 1600uT), a better linearity performance, and meanwhile a much lower hysteresis & noise level. Says Suffic Ind. Tech. Ltd.

Thanks for your help,

Rod.
 
Top