Hmc5883l gy-273

wudz

Member
I am in the middle of building an ROV and there is one feature I would like to add.
I have a GY-273 3 axis magnetometer module that I would like to use to sense ferrous metal, ideally I would want the data as a changing tone perhaps via a VCO.

This would be used to help find and retrieve lost anchor chains for our sailing club amongst the other things it will do.

At the moment I have a 18m2 doing most of the control functions and sounder, so would use another picaxe for this job.
Most of the information and data refers mainly for what it is designed for, a compass.
I do not require and graphic or digital O/p, just a tonal change associated to the magnetic changes.

Any help out there to at least get a varying voltage of digital range from the module via the Picaxe which I may then work on.

Thanks in anticipation
 

westaust55

Moderator
Could you use a PWM output (using the PWMOUT command) and change the frequency corresponding to the magnetometer data.
See here for the PWMOUT command informaiton: http://www.picaxe.com/BASIC-Commands/Digital-InputOutput/pwmout/

On an 18M2, pins B.3 and B.6 are available for the PWMOUT command.
You will need to change the period to change the frequency.
There is a wizard in the Programming Editor to help calculate parameters for this command for various frequencies and duties.
 

geoff07

Senior Member
You have a number of possibilities, depending on how you want to generate the audio and how loud it needs to be.

- PWM, as already mentioned, can give you a Vcc square wave at whatever frequency you choose according to the input.

- SOUND will do something similar, where you could vary the pitch and note length

- or you could vary the interval between beeps, getting more frantic and then becoming continuous as you get a stronger signal.

- if you want to be really complicated you could use a DAC output and generate your own tone.

The picaxe will generate a 0/~Vcc signal but only at low power. If you want to drive a loudspeaker you would need an amplifier, either a single transistor for minimum power, an opamp, or anything you choose for more power. The amp cpuld be powered from 12v as long as the picaxe is powered from 5v or less.You could make the tone sound a bit nicer with a low-pass filter (just an RC combination should do it).

You can drive a piezo quite loudly at its resonant frequency (around 3kHz) directly from the picaxe.

I'm assuming that you dangle a sensor on a line. Or get your scuba gear on and go for a swim! Depending on how it works, you might need to consider the line length between the magnetometer and the picaxe, probably best to co locate them and simply transmit the output signal.

Happy fishing!
 

eggdweather

Senior Member
So your compass module (5883) is giving your 3-parameters of x, y and z that denote where the unit is pointing to as a vector relative to magnetic north, so you can use x and y to determine bearing and if not level z to determine the bearing offset changes needed. The challenge then is to convert the x, y and z values to a tone. So maybe convert x, and y to a single vector of amplitude and angle (a) to do that requires an atan(y/x) calculation, the amplitude is sqr(x^2 + y^2) there are picaxe routines to do the atan or use the floating point add-on, from that data as your compass comes close to ferrous materials the angle will change and so that could be converted into a sound.
Conversely take the raw x, y and z values and use those:
0-degress ......000 000 000
90-degrees.....090 090 000
180-degrees...180 000 000

Those numbers are guesses, but won't that far off. You can see the raw x, y and z values, perhaps see if they provide enough information to feed into a tone generator via a PWM?
 

PADJ

Member
Conversely take the raw x, y and z values and use those:
0-degress ......000 000 000
90-degrees.....090 090 000
180-degrees...180 000 000

Those numbers are guesses, but won't that far off.
Please explain - you've lost me completely. Some code might help?
 

rossko57

Senior Member
I can't quite see how this is going to work. If the sensor is to mounted on a ROV, presumably beeping at the sensor end is pointless. Two stages then; extracting the sensor data and sending either it or an interpretation of it back to the surface. And something at the surface to respond to the data and make beeps the operator can hear. What is the nature of your command link to the ROV, what means have you to pass data back up?

As the sensor is geared up to report compass directions, I guess this will vary anyway as the ROV manouevres. How will you discriminate variation caused by nearby masses from the 'normal' motion changes? You might be able to calculate what the 'normal' _should_ look like by following the commanded movements and calculating expected direction. Sounds terribly complicated and unreliable in currents though.

Perhaps you could have two sensors mounted some distance apart, and compare the outputs looking for a difference caused by a local effect.

Not convinced this is the right tool for the job here - what kind of setup do metal dectectorists use?
 

wudz

Member
Well, first may I say thank for all the inputs.
My idea was to generate the sound on the ROV from the Mag IC, using either the Pic or a VCO and send to the surface for analysis, just listening for now and future analysis with better PC software, sort of Magnetic Sonar.
As Rossko57 points out there will be a problem using only one sensor due to normal magnetic changes, so two would hopefully offset that problem.
The biggest issue is understanding and utilising the data sent, how to change it if needed to something I may understand. Either a voltage or digital change that is linear to the magnetic change.
I also thought that I would only have to use one parameter of the three available, as at present I am not interested in E/W/N/S data, just magnetic deviation caused by any ferrous metal, which of the X, Y and Z parameters would be best to use by experiment.
I have read that this particular chip is extremely sensitive, recording a paper clip at room distance.
I was thinking of using a Hall effect ic but not sure if there would be any sensitivity at distances of a meter or more, as the above chip should do the business if the problems are solved.
Normal metal detectors use BFO's that need reset/ tuning in real time so I thought this approach would be easier to use being a remote sensing.
 

eggdweather

Senior Member
I have a lot of experience of using the 5883 and have found the 5883 is very sensitive to near-by ferrous objects/magnetic fields and you see the variations in the x, y and z values when that happens, so this is feasible. That's why I was suggesting converting the 3 rectangular coordinates to a polar form, to give a value comprised of magnitude and direction. In C coded as:
x = Wire.read() << 8 | Wire.read();
z = Wire.read() << 8 | Wire.read();
y = Wire.read() << 8 | Wire.read();
vector_angle= atan2((double)y,(double)x* (180 / 3.141592654);
x = cos((vector_angle-90)*3.14/180); // calculate X position
y = sin((vector_angle-90)*3.14/180); // calculate Y position

From this you would have the vector (where magnitude =sqrt(x^2+y^2)

Although as I mentioned, you don't need to do any of this if you use the raw x, y or z values and use a combination of all three, so for example at rest the values of xyz will be constant, but bring some ferrous materials near (or a magnetic field) and they will vary, which can be detected by detecting a changed value. The problem will be xyz will vary with a moving ROV, so to resolve that, detect the Rates of Change (ROC) of xyz, either all 3 or just 1, and assume the ROV can only move within a set bound of ROC. My observations are ferrous or magnetic perturbations happen very quickly and the compass recovers very quickly too, so detecting ROC that exceed ROV ROC gives you detection. This then needs to be sent to a tone command to give an audible indication.

You could also use a simple hall effect device, they are very cheap and will detect ferrous substances, perhaps an easier solution for you. They can detect the presence of a ferrous (e.g. iron or steel) objects because ferrous objects would concentrate magnetic fields at the sensor element
 
Last edited:

eggdweather

Senior Member
Please explain - you've lost me completely. Some code might help?
It's simple, the compass provides rectangle coordinates (x, y and z) and the values vary when ferrous objects or magnetic fields are in its proximity, it can therefore be used as a three axis metal detector.
 

wudz

Member
Wow, eggdweather, you are quick off the mark and I appreciate your in depth reply, scuz the pun..:)
You left me behind the math. I think you are saying that if I used xyz then ROV movements would not give false readings, but would give readings when near any ferrous materials.
I am not sure of the format of the data sent, I am OK with most inputs for the Pic but can not get my head around what is being sent by the Mag IC with relation to magnetic changes.
Any clues to start my code, either using one or all of the supplied parameters would get me started..the tone section I can cope with if I get a starting point.
Sorry but my 70yr old brain has slowed down these days.
 

eggdweather

Senior Member
You don't need any maths, just connect the 5883 to your PICAXE and read in the x, y and z values and then process them with some simple comparisons, the rate of change value can come from taking readings say every 1-sec and taking the current value from the last value, if more than a certain amount (to be determined) it means there is something around, this assumes the ROV can't move quickly or you set your threshold high enough to filter out noise. I found this video https://www.youtube.com/watch?v=Bg8Xx2yTnpI on YouTube that shows the three values coming out of the 5883 as the compass is rotated, this should help visualise what I've been saying. You would only need to use say the x channel. There is probably code in this forum for reading the 5883. These devices are typically used in geomagnetic detectors/aka high metal detectors.
 

PADJ

Member
In C coded as:
x = Wire.read() << 8 | Wire.read();
z = Wire.read() << 8 | Wire.read();
y = Wire.read() << 8 | Wire.read();
vector_angle= atan2((double)y,(double)x* (180 / 3.141592654);
x = cos((vector_angle-90)*3.14/180); // calculate X position
y = sin((vector_angle-90)*3.14/180); // calculate Y position

From this you would have the vector (where magnitude =sqrt(x^2+y^2)
And what's the pixaxe equivalent? Let's see some real code please.
 

wudz

Member
Thank again, I have seen some earlier code on the 5883 and it now makes more sense, I will use two 5883's as the ROV motors will have quite an effect as the ROV will be using echo sounders when needed to keep a selected distance off the seabed.
See how the build goes over the weekend.
I may be back:) as I am sure I will get lost with the coding.

I will be using an 18m as the other will be fully utilized on motor control/sonar/lights and camera
So any input please relate to the 18m will be appreciated
 

eggdweather

Senior Member
And what's the pixaxe equivalent? Let's see some real code please.
Well that code extract was from my magnetometer routines, it was an illustration of what could be done. As this is a community forum, I thought all members of it are here to help out others, why don't you post some real code?

Please help out wudz too with some example codes to get him going and I look forward to seeing the results.

And: http://www.picaxeforum.co.uk/showthread.php?7679-Read-Me-First!
 
Last edited:

PADJ

Member
Like the OP I have no experience with the 5883, and am trying to learn. So it would be helpful to me if you could illustrate your posts with picaxe code where appropriate so we may learn from your experience.
 

rossko57

Senior Member
Worth noting that wioth the ideas suggested, there will only be 'output' while the ROV is in motion.
You'd probably have to develop grid-search techniques to use it effectively.

I'm still not convinced it will be easy to distinguish slowly approaching a target from slowly rolling or yawing in currents. To be determined by experiment?

Sounds like the "up channel" from the ROV carries an audio signal then?

Does the ROV have GPS onboard?
a) data from that could usefully be compared to the compass to detect deviations?
b) when you get your target beeping, how will you know where to drag otherwise?
 

wudz

Member
I am hoping to utilize this Spectrum Analyser which has GPS use available, though the coordinates will be a bit off as the gps rx will be on the boat.

http://www.qsl.net/dl4yhf/spectra1.html

I agree that I could use the compass co ordinates once I try the simplistic approach of just a metal detector to see if that is the correct device to use.

In an ideal world I would love side scanning sonar but complexity and cost are too great!

I hope once I have a magnetic deviation reading the camera will help locate the item, that said the the water is quite murky as it is adjacent to quite a large estuary, so more reliance on the metal detection will be needed to narrow the search area.
 

srnet

Senior Member
Well that code extract was from my magnetometer routines, it was an illustration of what could be done. As this is a community forum, I thought all members of it are here to help out others
Well its actually a PICAXE support forum paid for by Revolution Education, which sells the PICAXE.

Posting code in C for competing platforms, is not likley to be of benefit for PICAXE users, particularly where the translation into PICAXE code is a long way from straightforward.
 

eggdweather

Senior Member
In PICAXE coded as:
symbol x = w0;
symbol y = w1;
symbol z = w2;

symbol xx = w3;
symbol yy = w4;
symbol zz = w5;
Symbol vector_angle = w6
symbol mm = w7

start:
x = 5883_x_MSB + 5883_x_LSB
z = 5883_z_MSB + 5883_z_LSB
y = 5883_y_MSB + 5883_y+LSB
;The following lines cannot be easily achieved in PICAXE BASIC, but can if the Floating Point module is added:
; FP Unit supports atan2
vector_angle= atan2((double)y,(double)x* (180 / 3.141592654);
; reuse x and y variables
; PF Unit supports sin and cos
x = cos((vector_angle-90)*3.14/180); // calculate X position
y = sin((vector_angle-90)*3.14/180); // calculate Y position

;From this you would have the vector (where magnitude =sqrt(x^2+y^2)
xx = x * x
yy = y * y
mm = xx + yy
;FP Unit supports SQRT, to send data to the FP Unit, do this
' send mm to uM-FPU
writei2c 0, (SELECTA, mm, LOADWORD, b15, b14)
writei2c,0, (SQRT)
GOSUB print_floatFormat
' And so-on
GOTO start

print_floatFormat:
writei2c 0, (FTOA, format) ' convert floating point to formatted ASCII
gosub fpu_wait ' wait until uM-FPU is ready
writei2c 0, (READSTR)
print_string2:
readi2c 0, (dataByte) ' display zero terminated string
if dataByte = 0 then Print_String3
if dataByte > 127 then Print_String3
sertxd(dataByte)
goto Print_String2
print_string3:
return ' end of string


If the OP wishes to pursue use of the FP unit I will gladly provide help as no-one else is, just criticism. Any use of C code was to illustrate how functions could be achieved. If there was a published pseudo or intermediate code I would gladly use that, because PICAXE is too limited to explain the outline principles and they get lost.

Does that help, as it is now 70% PICAXE code and will of course need further development?

I did not know I had to post finished PICAXE code rather than illustrative examples of the principles, is that the case then when we post?

Like PADJ who asks me for code, but never offers his own or help, I'd like to see some examples from those who are very experienced in PICAXE showing us how it should or could be done if the use of C as an example is a long way from straightforward...

Other observations are reading the HMC5883 can be achieved with the PICAXE and displaying the resultant vector is possible to a reasonable degree of practical resolution even without the FP Unit, noting of course the FP Unit would make the result every bit as good as another platform. I'm all for pushing the products boundaries as the building blocks are there.
 

eggdweather

Senior Member
I agree, and I think I may have seen an atan routine, but can't find it on the forum, maybe even a lookup table would be suffice for most applications.

I like challenges like this, the problem is with a full-time job I just don't have the time to devote to a full solution. I have spent a long time with the 5883 and that tells me it can be done, but the level of knowledge to convey to the op is high. Thanks for the help.

The maths is Pythagoras from a triangle made up of adjacent (x) and opposite (y) to get the hypotenuse or amplitude (z^2=x^2+y^2) and the direction is the atan calculation. It would need an accelerometer to establish true z and hence get a location in space.

I have found the compass is very sensitive to ferrous objects and only in the middle of a field with no known objects around do I get a reading that is what I expect.

Taking its average output and then detecting rate of change will give a detection of materials, but the software will need to do some integration, which is best done with a rolling average and so the task goes on.
 
Top