Magnetic sensor required

Colinpc

New Member
Looks good though its requirements for flat surface seems to preclude a farm track.

The means of detection and sensitivity keep improving.

Fred
 

Colinpc

New Member
This is my test program and description for anyone interested.


Code:
#rem
Test of magnetometer type HMC5883L. 

At power up, initialise the reference value that will be used to compare with test read values.
The initialise routine takes 5 readings and this is averaged to give the reference value. 
The store is words W9 to W13.

The reference value is then calculated on a rolling average of the previous  saved readings. 
The reading is saved every 5000 readings and allows for slow variations over time.

The test read is made and compared to the reference value. If it is more than "alarmlvl" from
the reference value, the alarm activates. Once activated, it loops until the alarm situation is removed
before returning to the read and test loop.

Portions of this code directly related to the setup and reading of the magnetometer were supplied by 
members of the Picaxe forum. With thanks.

#endrem

Symbol x = w0						'x, y, z are values read from the magnetometer
Symbol x.lsb = b0
Symbol x.msb = b1

Symbol y = w1
Symbol y.lsb = b2
Symbol y.msb = b3

Symbol z = w2
Symbol z.lsb = b4
Symbol z.msb = b5


Symbol storex = w3					'store the reference value 
Symbol strlow = w4					'low reference value
Symbol strhigh = w5					'high reference value

symbol i = b12						'i is use as a counter

symbol alarmlvl = b13					'offset from average value to trigger alarm	
alarmlvl = 10

symbol n = w8						'counter for update average value loop

#rem
symbol str1 = w9						'reserved for storage
symbol str2 = w10
symbol str3 = w11
symbol str4 = w12
symbol str5 = w13
#endrem

bptr = 18							'point to start of storage area

'****************************************************************************************************

Main:

hi2csetup i2cmaster, $3C, i2cslow_16, i2cbyte 	'Setup PICAXE I2C port

hi2cout $00, ($18) 					'Set mag req A for non-average,75Hz,normal measurement
hi2cout $01, ($00) 					'Set mag req B for range of +/- 0.88Ga (Earth = +/-0.5Ga)

gosub init							'Initialise magnetometer alarm values

'Main program loop starts here

repeat:

gosub readmag						'Get x, y & z readings

x = x+y+z/3							'get average value

'check for alarm before saving and calculating new reference values.

if x > strhigh  then 					'Compare with the reference values
gosub alarm
goto repeat
elseif x < strlow  then 				
gosub alarm
goto repeat							'do not save this value into store.
endif

n=n+1								'count the number of readings done

if n = 5000 then
gosub update						'update reference value
endif

goto repeat							'go and get another reading

'**************************************************************************
init:
'Initialise magnetometer default alarm setting by averaging 5 readings at power on. 
'This is used to compare future reading to determine if vehicle is close.
'5 readings of x,y & z (15 values) are averaged.

for i = 1 to 5						'take 5 readings and get average

gosub readmag

x = x+y+z/3							'get the average of the x, y and z readings

gosub update

@bptrinc = b0						'save it in the store and increment ptr
@bptrinc = b1

if bptr = 28 then 
	 bptr =18						'end of store, goto start
endif

next i	

storex = w9+w10+w11+w12+w13/5				'add all stored values and get average

strlow = storex-alarmlvl				'set reference limit values
strhigh = storex+alarmlvl

return

'*************************************************************************
readmag:							'Read the magnetometer

hi2cout $02, ($01) 					'Request single measurement Reg 02 = mode reg.
pause 100 							'Allow time for measurement and display

HI2cIn $03, (x.msb, x.lsb, z.msb, z.lsb, y.msb, y.lsb)	'Read from reg 03 onwards into variables

x = x+2048

y = y+2048

z = z+2048

return 						

'*****************************************************************************
alarm:

high c.4							'turn on the relay
high c.0							'turn on LED
pause 500				

low c.4							'turn off the relay

Gosub alarmover						'wait until the alarm condition clears before going back
low c.0
return

'**********************************************************************************
alarmover:							'check for alarm still active.

gosub readmag

x = x+y+z/3							'get average value

if x > strhigh  then 					'Compare with the reference value before the alarm detected.
goto alarmover						'loop around until alarm condition gone
elseif x < strlow  then 
goto alarmover
endif

return

'************************************************************************************
Update:							'save every 5000th reading in store


@bptrinc = b0						'save it in the store and increment ptr
@bptrinc = b1

if bptr = 28 then 
	bptr =18						'end of store, set pointer to start
endif

storex = w9+w10+w11+w12+w13/5				'add all values and get average

strlow = storex-alarmlvl				'set reference limit values
strhigh = storex+alarmlvl

n = 0								'reset the update counter

return
 

MFB

Senior Member
Thanks for sharing your development work with us. It would be good to start a readily accessible library on the forum for smart sensor interface code, as nearly all such new devices now offer an I2C option. Looking forward to hearing how the field trials go.
 

radiogareth

Senior Member
Thanks for sharing your development work with us. It would be good to start a readily accessible library on the forum for smart sensor interface code, as nearly all such new devices now offer an I2C option. Looking forward to hearing how the field trials go.
I agree - nearly everything has already been answered somewhere on this forum but its really hard getting search to find it sometimes......
 

Colinpc

New Member
Now this thread has been brought back, a quick update is due.

The detection is good in that it will pick a paper clip from a few cms, a moving chair from 40cms (sensor on desk) and it picked the car being moved from a mtr off the side. I still have not actually used it at the gate as I have not yet got a transmitter/receiver that will cover 300m though trees.

If you are tempted to play with this device, they are available from dx.com for a good price with free shipping.

Fred
 

radiogareth

Senior Member
Just wired mine up and yes it does work :)

Now to see if I can get an LCD display showing X,Y & Z in degrees....suggestions welcome :)
 

MFB

Senior Member
You could use the original code I posted ages ago, to output data to a PC, to drive a serial LCD module.
 

radiogareth

Senior Member
I have had a search but only found this thread. Its outputting data fine to terminal, its converting 4096 into degrees I want...

w0=w0*360/4096 = 'Heading' sort of thing.....i.e, 'Heading = 0....359 degrees'
 

MFB

Senior Member
Ah, my original code only sent raw data for display and recording. Any post-processing would have been done by the host PC.
 

AllyCat

Senior Member
Hi,

display showing X,Y & Z in degrees....suggestions welcome :)
Basically you just need to calculate ARCTAN for two planes (pairs of axes). For one degree accuracy it seems possible to get away with just interpolating along a two-straight-lines "lookup table" (covering 0 - 45 degrees).

However, my recommended "trick" is to NOT code directly in degrees (or radians), but to make a byte (or word) represent "one revolution" (i.e. 360 degrees). Then the two (or three) most significant bits represent the quadrants (or octants) of the angles (i.e. their sign relative to the planes) and the remaining bits can be scaled accordingly (i.e. by multiplying by 45/32, or as appropriate).

Sorry, I haven't yet finished my own attempts at a general-purpose ARCTAN algorithm, but I recently gave a "preview" in #20 of this thread.

Cheers, Alan.
 

RobertN

Member
This maybe a little late for the discussion here. We have a commercial passive vehicle detector under our drive, it works by sensing the disturbance in the earths magnetic field due to metal objects moving near by. Its an iron armature wound with about 8000 turns of small gauge copper wire. Walking over it with steel toed shoes or occasionally lighting will trip it. The manufacturer says it will work well if buried along side the drive.
It uses an analog circuit, with rather old components, for detecting the signal and driving a relay. Would something like this sensor be a candidate for energy harvesting for a low power transmitter?
 

AllyCat

Senior Member
Would something like this sensor be a candidate for energy harvesting for a low power transmitter?
No, generation of useful power requires a rapidly changing magnetic field; also the Earth's magnetic field is very weak compared to that used in motors or generators. Furthermore, I believe that the sensor discussed in this thread is a magneto-resistive type, so it does not actually generate any power, but (like a Light-Dependent Resistor) only modulates (i.e. varies) the power from another source.

Theoretically, a large coil as described in #52 could act as an antenna (aerial) to "harvest" (low frequency) radio waves (if moderately close to a broadcast transmitter), but a location on (or under) the ground is not a good place for an antenna. The rectification and power (voltage) conversion would also be challenging, so "hacking" a Garden Solar Light from a Pound (or Dollar) store is likely to be much more productive for powering a low-power transmitter.

Cheers, Alan.
 

alphamike27

New Member
Hi freddag
I have a sensor similar to that indicated by RobertN
It operates by sensing changes in the earths magnetic field due to the presence of a ferrous object
I originally purchased it to operate an automatic gate, but unfortunately I found that the installation was too close to a public road and large trucks passing by would activate it
Unit operates from 12VDC and is fully contained in a PVC tube about 25 x 400 mm in size (easy to bury)
I am prepared to let this unit go at a heavily discounted price if you would like to PM me
Apologies to admin if this contravenes advertising protocols
Regards
 

Colinpc

New Member
Thanks to Alphamike, I have a CS-202S from EMX Industries. VERY sensitive. Tested just vibrating a chair, just perceptable movement and triggered. Works on detecting a change in the earths magnetic field.

With my use, it should be perfect. I am waiting for the rain to soften the clay so I can bury it.

Now a new request: I need to get the output (relay contact) to a place about 300 metres away (some trees in path). Any ideas on TX/Rx that will do it for up to $100- 150?
Prefer cheaper but may have to lash out! No preference as to frequency. I would like contact closure on Tx to switch an output on Rx.

Sorry to be drifting away from Picaxe but the situation has changed a little. I will use the original magnetometer with Picaxe to turn on lights as I get close to the camp at night.

Fred
 
Last edited:

AllyCat

Senior Member
Hi Fred,

I am waiting for the rain to soften the clay so I can bury it.
You're obviously NOT located in the UK then. :)

But seriously, you must specify your location (at least the Continent) before any serious advice can be given on (legal) 300+ metre range radio communications.

Cheers, Alan.
 

Colinpc

New Member
I have modified my profile to let you know why I have to wait!

South west , West Australia is rock hard at this time of year.

Fred
 

Paix

Senior Member
In the UK our upbringing may have been sufficiently sheltered for us not to know! Not sufficiently sheltered to avoid the effects of the rain though.

I used to think it sufficient to know that Blue had red hair . . .
 

Skiwi

New Member
Yes Fred, your name-sake is a Kiwi character, real name John Clarke.
from his version of the Newcastle Song (covered from an Aussie character) ' dont you ever let a chance go by...'

To return to topic, thanks for all the code in this thread, i am about to have a go at making an HMC5883L work.
 

binary1248

Senior Member
The HMC5883L mentioned in the above post by MFB would be the way I would go. It is very small and can be remotely located a few feet from a picaxe so it is easily buried with a very small footprint. It looks so cool I am temped to try one just for fun, well, not know, need to finish up my PicAxe powered wooden clock.
 

Nelixuk2003

New Member
The HMC5883L mentioned in the above post by MFB would be the way I would go. It is very small and can be remotely located a few feet from a picaxe so it is easily buried with a very small footprint. It looks so cool I am tempted to try one just for fun, well, not know, need to finish up my PicAxe powered wooden clock.
I realise that this is an extremely old thread and I apologise for resurrecting it, however, I have a question. It is not clear which PicAxe chip was used with the HMC5883L. Was it the 08M2?

Regards
 

mikeyBoo

Senior Member
Cheapest, simplest, most reliable method:
Two sealed rubber hoses tied to pressure switch. Photo taken when front wheels hit 2nd hose.
You will receive your speeding ticket in the mail. Y'all come on back down to south Georgia now you he'nyuh.
 

AllyCat

Senior Member
Hi,

I realise that this is an extremely old thread and I apologise for resurrecting it, however, I have a question. It is not clear which PicAxe chip was used with the HMC5883L. Was it the 08M2?
The age of the thread is not an issue, but the length of it is. Which part of the 60+ posts is of interest to you? It's usually best to say how you want to use the sensor.

The sensor in question uses an I2C bus, so in principle any (recent) PICaxe could be used (including the 08M2). Then it depends how the acquired data is to be used, or what (else) you want the PICaxe to do. If the data needs a moderate amount of mathematical processing then an X2 chip might be more appropriate, but more complex calculations can be done even with an 08M2, by using a few dedicated subroutines.

Cheers, Alan.
 

Nelixuk2003

New Member
Hi,

The age of the thread is not an issue, but the length of it is. Which part of the 60+ posts is of interest to you? It's usually best to say how you want to use the sensor.

The sensor in question uses an I2C bus, so in principle any (recent) PICaxe could be used (including the 08M2). Then it depends on how the acquired data is to be used, or what (else) you want the PICaxe to do. If the data needs a moderate amount of mathematical processing then an X2 chip might be more appropriate, but more complex calculations can be done even with an 08M2, by using a few dedicated subroutines.

Cheers, Alan.

Thanks Alan

I am wanting to make an extremely small sensor something that can be hidden in the hand that will activate a small coin vibration motor. it needs to detect a small magnet (about the size of a £1 coin) from approx 12".

Does the original poster still have a schematic for this?
 

AllyCat

Senior Member
Hi,

... small sensor something that can be hidden in the hand ......to detect a small magnet (about the size of a £1 coin) from approx 12".
I believe the original post concerned a fixed detector to monitor changes in the Earth's magnet field, caused by the presence of a large magnetic object such as a motor car. So I believe your requirement is rather different.

In particular, if the detector is "hidden" in a hand then presumably the direction of the Earth's magnetic field will change as the hand is moved? Therefore your magnet needs to generate a field which is "large" compared with the Earth's magnetic field (or at least any directional changes in it).

A Pound coin is physically quite large compared with some available Neodymium magnets, but (at best) you have to contend with the "Inverse Square Law", e.g. moving the sensor ten times further away makes the field one hundred times smaller. In practice, it might be even worse, because at "large" distances (compared with the size of the magnet) the North and South poles "cancel each other out" and you'd see no detectable magnetic field at all.

The basic schematic need be no more than four wires (Supply, Ground, SDA and SCL) between the sensor and PICaxe (plus a battery and supply decoupling capacitor, etc.), but the main design will be concerned with the magnetic "circuit", which might need much "trial and error".

Obviously it depends on the exact application requirements, but if I was developing something that I was fairly sure would work, then I'd be considering an "active" electromagnetic transmitter, i.e. a tiny battery, coil (antenna) and oscillator (which might be anything from a few Hz up to many MHz, depending on the "sensor") fitted into the space of a "Pound Coin".

Cheers, Alan.
 
I am wanting to make an extremely small sensor something that can be hidden in the hand that will activate a small coin vibration motor. it needs to detect a small magnet (about the size of a £1 coin) from approx 12".
If it helps, I recently got 10 off SS49E magnetic sensors for 94p (inc. P&P) from a Chinese supplier via a well known auction site. As they're in a TO92 case, they're very small but do need added circuitry to do anything useful.

Basically, they provide a mid-level voltage output (typically 2.5V on a 5V supply) which varies up or down depending on the field strength and polarity, i.e. a north pole will deviate the signal one way, and a south pole the other way.

Coupled to a small CMOS op-amp configured as an amplifier, you could make quite a cheap but sensitive basic magnetometer.
 

Nelixuk2003

New Member
If it helps, I recently got 10 off SS49E magnetic sensors for 94p (inc. P&P) from a Chinese supplier via a well known auction site. As they're in a TO92 case, they're very small but do need added circuitry to do anything useful.

Basically, they provide a mid-level voltage output (typically 2.5V on a 5V supply) which varies up or down depending on the field strength and polarity, i.e. a north pole will deviate the signal one way, and a south pole the other way.

Coupled to a small CMOS op-amp configured as an amplifier, you could make quite a cheap but sensitive basic magnetometer.
I've had a little bit of a play with Hall Effect sensors like the SS49E mentioned and Op-Amps and initially had good results however, When I left the system running for a while a get drift in the zero point. At first I thought it was problems with the amp circuit, but I narrowed it down to being the null drift as a function of temperature. In other words, if the sensor itself gets warmer or cooler, the voltage output for zero Gauss moves. When using the sensor in its intended range (+/-670 Gauss,) this drift is tiny and can be mostly ignored. But when you amplify that drift by 100x, it becomes fairly significant. I'm seeing drift of 2-3 Gauss, which means being able to reliably detect accurate Gauss fields is probably not realistic hence the thought of trying a magnetometer.
 
Top