EOS

Instruments

Magnetic Sensor

Programming Exercise in PicAxe

The output of the Philip Harris Magnetic Field Sensor is directly proportional to the strength of the magnetic field at the probe.  The Hall sensor is inside the “foot” end of the probe, with the sensor axis oriented perpendicular to the “foot”.  The Blue Box output voltage ranges from 0 to +1V.  Magnetic fields can be measured on two scales, switch-selected: 10 millitesla and 100 millitesla. 

 

Note on units of magnetic field

The System-International (SI, commonly known as the MKS system) unit of magnetic field strength is the tesla (T), which is the same as kg/(coulomb second)

                          1 T = 104 gauss

                          Gauss is used in the cgs (centimeter-gram-second) system of units.

                          The earth’s magnetic field is about ½ gauss.

Some PicAcxe Basic Commands

First, define some variables such as these:

             symbol  variablename = pinC.2                        ‘for example

             symbol  HallSensor = w0                      ‘w0 and w1 are word variables, i.e., 16 bit

             symbol   Field = w1

             symbol  Toggleswitch = pinC.4                        ‘defines both variable name and which pin it represents

             symbol  left  =                                         ‘either 0 (“low”) or 1 (high)

             symbol right =                                                     ‘either 1 (if left is low) or 0 (if left is high)

 

Commands related to reading the analog or digital logic input:

fvrsetup FVR2048

             adcconfig %011

             readadc10 C.2

             if variablename = 1  then (etc.)            ‘this statement reads the input to check the “if” logic;                                                                                          ‘variablename must be defined with “symbol” statement;

                                                                 ‘”if” query is true when pin is high

Commands related to displaying the output:

             bintoascii

             serout C.1, N2400_4,(254, 128,”                “) 

             serout C.1, N2400_4,(254, 192, “   “,b9,b10,b11,”          “)

                          Note that whatever is within the parenthesis is displayed on the OLED.

                          Make sure that there are exactly 16 characters and/or spaces within the parenthesis.

                          128 is the leftmost position on line 1 of the OLED

                          192 is the leftmost position of line 2 of the OLED

                          bintoascii converts a bit number to a display character.

Commands related to logic and math:

             if   variablename = 0     then . . .

                          endif

goto (linename)

             VariableA = VariableB * 2 + 800 / 7   ‘math protocol strictly from left to right;  integers only

Power Supply on PicAxe Circuit Board

Power Input:                +7.0 V to +35.0V; connector is center positive; nominally +9.0V dc power adaptor is used (+9.0 V battery for portable use, but avoid if possible since current drain is high - battery won’t last long).

Power Output:            +5.0 V, regulated

                                       200mA maximum current

                                       Red LED “on” indicator.

This power supply is hardwired to both the PicAxe and the OLED display; red lead positive/black lead ground, 0V.

Blue Box Manual

Solution

The Assignment

The task here is to write a PicAxe program to read the analog input C.2, convert that to magnetic field strength, continuously update and display the result on the OLED.  The OLED display must include the term “Magnetic Field” centered on the first line.  The second line must display “B = “ followed by the current value written with the proper unit (mT) and some indication, say, a plus or minus sign, of the field orientation, North or South.

Preliminary Tasks

The logic of the toggle switch must be determined.

NOTE: The Blue Box is separate and independent of the PicAxe microprocessor; thus the switch selection on the Blue Box is not automatically reflected in the microprocessor. The purpose of the toggle switch in front of the OLED is to correlate the scale within the microprocessor program to the Blue Box scale.

To display the magnetic field properly on the OLED the toggle switch position must be programmed into the PicAxe and the switch position must match the switch on the Blue Box.

The orientation of the magnetic field direction must be determined. 

The field is measured at the “foot” of the sensor with maximum sensitivity 8mm from the end opposite the sensor handle.

With no magnetic field in the region of the sensor, adjust the “zero” control such that the voltage output of the Blue Box is exactly +0.5 V.

With that adjustment, an output voltage of zero to +0.5 V indicates one orientation of the field (either North or South pointing “up”) and an output voltage of +0.5 V to +1.0 V indicates the opposite orientation.  Use a known magnet to discover the correct orientation. 

The Solution

The simplest solution is to write a Flowchart in Logicator first, then convert the Flowchart into Basic language.  Inevitably, the Flowchart will not be complete so a modification of the program will be necessary.   Use Programming Editor 6 to complete the program.  The Flowchart might look something like this:

Toggle Switch                          (in front of the OLED)

Two position toggle switch – single pole, double throw.

Switch is hardwired to – and switch logic is read at – Picaxe microprocessor pin C.4

Black lead connected to ground, 0V / White lead hardwired to PicAxe microprocessor pin C.4

To find the logic of the switch, use a voltmeter on the microprocessor actual leg of pin C.4 (check the diagram above, and be careful not to short other actual legs).  Zero volts means logic state “low”, or 0; +5.0 V means logic state “high”, or 1.

Switch circuit diagram:

Switch circuit analysis:

If the switch is in the position as shown, pin C.4 is shorted to ground and is thus low.

In the other position, no connection, the pin C.4 is connected to +5.0 V through the 10 kΩ resistor.  In this case the pin is high (at +5.0 V) because a) no d. c. current flows through the capacitor, and b) the microprocessor input impedance is quite high; therefore it draws no current through the 10 kΩ resistor and thus there is no voltage drop across the 10 kΩ resistor – making the voltage at the pin equal to +5.0 V.

The 560 Ω resistor and the 1μF capacitor act as a filter to “debounce” the switch.  Mechanical switches often make-and-break several times when the toggle is thrown only once.  This filter prevents the voltage at the pin from fluctuating and driving the microprocessor crazy when this situation occurs.

Philip Harris Magnetic Field Sensor            Blue Box

 

Use only the red and black banana jack connectors on the Blue Box.  Never use the blue banana jack.

 

Power requirements:                 +6.0 V; separate external battery pack

                                                    Battery pack red lead positive/Black lead ground, 0V

 

Output (red/black banana jacks):

             Output ranges from 0 Volts to +1 Volt, and is linear in magnetic field strength sensed by the probe. 

             Red jack, voltage out; Black jack, ground, 0V.

 

Field orientation and zero field:

             With no magnetic field in the region of the sensor, adjust the “zero” control such that the voltage output of the Blue Box is exactly +0.5 V.

             With that adjustment, an output voltage of zero to +0.5 V indicates one orientation of the field (either North or South pointing “up”) and an output voltage of +0.5 V to +1.0 V indicates the opposite orientation.  Use a known magnet to discover the correct orientation. 

 

Switch in “BATT” position:

             The Blue Box output will be +1.0 V if the battery voltage is +6.0 V.   Replace batteries if this output is below +0.75 V (corresponding to a battery voltage of +4.5 V).

 

Two scales, switch selected, correspond to a maximum magnitude of magnetic field of 10 milliTesla and 100milliTesla.

 

NOTE:      The Blue Box scale switch selection must match the toggle switch selection for the output to display the field correctly on the OLED.

NOTE:      Do not use blue banana jack – negative voltage on the PicaAxe input may damage the microprocessor.

Text Box: 001	'BASIC
002	
003	
004	{ ;Symbols
005	symbol HallSensor = w0
006	symbol Field = w1
007	symbol Toggleswitch = pinC.4  
008	symbol left = 0
009	symbol right = 1
010	}
011	main:
012		let dirs = 0
013				
014	Cell_7_4:
015		'if toggleswitch = 0, pin is low, switch "left side", and on the 10mT scale
016		'if Toggleswitch = 1, pin is high, switch "right side", and on the 100mT scale
017			'however, only need to test for one of those conditions
018		'fvrsetup FVR2048  'set adc voltage reference to 2.048 Volts (not +5)
019		'adcconfig %011    'first bit (=0) means: adc low reference point is 
020				'Vref- (=0)
021				'next two bits (11) mean: use fvrsetup command to
022				'set adc high reference point Vref+ (2.084 V)
023				'repeat before each readadc10 command (?)
024		fvrsetup FVR2048
025		adcconfig %011
026		readadc10 C.2, HallSensor
027		if HallSensor >= 0 and HallSensor <= 250 then   'zero field at .5 Volts (= 250 bits)
028			goto Cell_7_6     				'negative below 250
029		end if
030		if HallSensor >= 251 and HallSensor <= 500 then 	'postitive above 250
031			goto Cell_13_7
032		end if
033		      'otherwise, input is > 1V, or, "offscale"
034			serout C.1, N2400, (254, 128, " Magnetic Field ")
035			serout C.1, N2400, (254, 192, "  (off scale)   ")
036		goto Cell_7_4
037		
038		
039				'positive field
040	Cell_13_7:  				
041		if HallSensor < 251 or Hallsensor > 500 then    	
042			serout C.1, N2400, (254, 128, " Magnetic Field ")
043			serout C.1, N2400, (254, 192, "  (off scale)   ")
044		endif
045		let Field = HallSensor - 250 * 4
046		if HallSensor = 1 then
047			let Field = 1000
048		endif       	
049		bintoascii Field, b8,b9,b10,b11,b12
050		
051					'on the 10mT scale:
052			if Toggleswitch = left then   'if yes, switch is to the left and on 10mT scale
053				if Field = 1000 then    	'here only if mag field is exactly +10.00mT
054					serout C.1,N2400,(254,128," Magnetic Field ")
055					serout C.1,N2400,(254,192,"  B = +10.00 mT ")
056					goto Cell_7_4
057				endif
058			serout C.1,N2400,(254,128," Magnetic Field ")   	
059			serout C.1,N2400,(254,192,"  B = + ",b10,".",b11,b12," mT ")
060					goto Cell_7_4
061			endif
062	
063					'on the 100mT scale:    'switch is to the right; on 100mT scale
064			if Field = 1000 then    		'here only if mag field is exactly +100.0mT
065				serout C.1,N2400,(254,128," Magnetic Field ")
066				serout C.1,N2400,(254,192,"  B = +100.0 mT ")
067				goto Cell_7_4
068			endif 
069				serout C.1, N2400, (254, 128, " Magnetic Field ")
070				serout C.1, N2400, (254, 192, "  B = + ",b10,b11,".",b12," mT ")
071					goto Cell_7_4
072			
073				'negative field
074	Cell_7_6:   
075			if HallSensor = 0 or HallSensor > 250 then
076			serout C.1, N2400, (254, 128, " Magnetic Field ")
077			serout C.1, N2400, (254, 192, "  (off scale)   ")
078				goto Cell_7_4
079			endif
080		let Field = 250 - HallSensor * 4
081		if HallSensor = 1 then
082			let Field = 1000
083		endif 
084		bintoascii Field, b8,b9,b10,b11,b12
085	
086					'on the 10mT scale:
087		if Toggleswitch = left then   'if yes, switch is to the left and on 10mT scale
088			if Field = 1000 then    		'here only if mag field = -10.00mT
089				serout C.1,N2400,(254, 128, " Magnetic Field ")
090				serout C.1,N2400,(254, 192, "  B = -10.00 mT ")
091					goto Cell_7_4
092			endif
093			serout C.1,N2400,(254, 128, " Magnetic Field ")
094			serout C.1,N2400,(254, 192, "  B = - ",b10,".",b11,b12," mT ")    
095				goto Cell_7_4
096		endif
097				
098					'on the 100mT scale:    'switch is to the right; on 100mT scale
099		if Field = 1000 then    			'here only if mag field = -100.0mT
100			   serout C.1,N2400,(254, 128, " Magnetic Field ")
101			   serout C.1,N2400,(254, 192, "  B = -100.0 mT ")
102			      goto Cell_7_4
103		endif
104		serout C.1,N2400,(254, 128, " Magnetic Field ")
105		serout C.1,N2400,(254, 192, "  B = - ",b10,b11,".",b12," mT ")
106				goto Cell_7_4
107								
108	#no_data    'reduce download time
109

 

Switch Left

Switch Right

Logic state of pin C.4

(high or low)