If you are referring to the MLX90217 hall sensor Parallax sells, there is additional information on the Parallax website along with sample code as a rule. You will need to take the initiative and research this. As a general rule, the air gap can not be changed more than a millimeter or so.
I am using the Melexis Hall Effect sensor to count the number of times a magnet swings by a sensor. The magnet is part of a SunSwinger Pendulum Kit from Solarbotics. I was curious to see how many times the magnet would swing in a day so I wrote a BS2 program to count the swings and display them on a Parallax serial LCD. Here is my program. Hopefully it will give you an idea how to use the sensor.
The magnet swings about 5 mm from the sensor.· It is a pretty strong magnet.
I found that a WORD overflowed in a day so I kludged the code to support more swings by having a hicounter that counts the number of times the counter exceeds 9999.· It is not pretty, but it works.
Link Describing the SunSwinger:
Kit#12 - http://www.solarbotics.com/products/k_pn/
BS2 Program: (leveraged Parallax demo program for the LCD so there is some extra stuff that is not needed)
'
[noparse][[/noparse] I/O Definitions ]
TX PIN 0 ' serial output to LCD
HALLSENSOR PIN 8 ' input signal from the Hall Effect Sensor
LED PIN 11 ' output to LED
'
[noparse][[/noparse] Constants ]
#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T2400 CON 396
T9600 CON 84
T19K2 CON 32
#CASE BS2SX, BS2P
T2400 CON 1021
T9600 CON 240
T19K2 CON 110
#ENDSELECT
'LCD Constants
LcdBaud CON T19K2
LcdBkSpc CON $08 ' move cursor left
LcdRt CON $09 ' move cursor right
LcdLF CON $0A ' move cursor down 1 line
LcdCls CON $0C ' clear LCD (use PAUSE 5 after)
LcdCR CON $0D ' move pos 0 of next line
LcdBLon CON $11 ' backlight on
LcdBLoff CON $12 ' backlight off
LcdOff CON $15 ' LCD off
LcdOn1 CON $16 ' LCD on; cursor off, blink off
LcdOn2 CON $17 ' LCD on; cursor off, blink on
LcdOn3 CON $18 ' LCD on; cursor on, blink off
LcdOn4 CON $19 ' LCD on; cursor on, blink on
LcdLine1 CON $80 ' move to line 1, column 0
LcdLine2 CON $94 ' move to line 2, column 0
LcdLine3 CON $A8
LcdLine4 CON $BC
LcdCC0 CON $F8 ' define custom char 0
LcdCC1 CON $F9 ' define custom char 1
LcdCC2 CON $FA ' define custom char 2
LcdCC3 CON $FB ' define custom char 3
LcdCC4 CON $FC ' define custom char 4
LcdCC5 CON $FD ' define custom char 5
LcdCC6 CON $FE ' define custom char 6
LcdCC7 CON $FF ' define custom char 7
'
[noparse][[/noparse] Variables ]
idx1 VAR Byte
idx2 VAR Byte
char VAR Byte
newChar VAR Byte
counter VAR Word
hicounter VAR Word
currState VAR Byte
prevState VAR Byte
'
[noparse][[/noparse] EEPROM Data ]
' C#---- Data
CC0 DATA LcdCC0, $0E, $1F, $1C, $18, $1C, $1F, $0E, $00
CC1 DATA LcdCC1, $0E, $1F, $1F, $18, $1F, $1F, $0E, $00
CC2 DATA LcdCC2, $0E, $1F, $1F, $1F, $1F, $1F, $0E, $00
Smiley DATA LcdCC3, $00, $0A, $0A, $00, $11, $0E, $06, $00
Msg2 DATA "IS VERY COOL! ", 3
'
[noparse][[/noparse] Initialization ]
Reset:
HIGH TX ' setup serial output pin
PAUSE 100 ' allow LCD to initialize
DnLoad_Custom_Chars:
FOR idx1 = 0 TO 35 ' download 4 characters
READ CC0 + idx1, char ' get data from table
SEROUT TX, LcdBaud, [noparse][[/noparse]char] ' send to LCD
NEXT
'
[noparse][[/noparse] Program Code ]
' Clear the display and remove cursor and blinking attributes.
Main:
· prevState = 1 · counter = 0 · hicounter = 0 · INPUT HALLSENSOR · OUTPUT LED · SEROUT TX, LcdBaud, [noparse][[/noparse]LcdBLon, LcdOn1, LcdCls]
Melexis: · SEROUT TX, LcdBaud, [noparse][[/noparse]LcdBLon, LcdOn1, LcdLine1] · SEROUT TX, LcdBaud, [noparse][[/noparse]"Pin 8 = "] · currState = HALLSENSOR · ' Only print pin state if it changed (allows us to poll input faster) · IF currState = 0 AND prevState = 1 THEN ··· SEROUT TX, LcdBaud, [noparse][[/noparse]"0"] ··ENDIF · IF currState = 1 AND prevState = 0 THEN ··· SEROUT TX, LcdBaud, [noparse][[/noparse]"1"] · ENDIF · IF currState <> prevState THEN ··· IF currState = 0 THEN ' sensor was tripped ····· counter = counter + 1 ····· IF counter = 10000 THEN ······· counter = 0 ······· hicounter = hicounter + 1 ····· ENDIF ····· LED = 1 ····· SEROUT TX, LcdBaud, [noparse][[/noparse]LcdLine2, "Count = ", DEC5 hicounter, DEC4 counter] ····· PAUSE 200 'Debounce timeout ····· LED = 0 ··· ENDIF ··· prevState = currState · ENDIF
GOTO Melexis
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
http://www.parallax.com/Store/Sensors/PressureFlexRPM/tabid/177/CategoryID/52/List/0/Level/a/ProductID/90/Default.aspx?SortField=ProductName%2cProductName
http://www.parallax.com/Portals/0/Downloads/docs/prod/compshop/MLX90217.pdf
·
The magnet swings about 5 mm from the sensor.· It is a pretty strong magnet.
I found that a WORD overflowed in a day so I kludged the code to support more swings by having a hicounter that counts the number of times the counter exceeds 9999.· It is not pretty, but it works.
Link Describing the SunSwinger:
Kit#12 - http://www.solarbotics.com/products/k_pn/
BS2 Program: (leveraged Parallax demo program for the LCD so there is some extra stuff that is not needed)
'
' {$STAMP BS2e}
' {$PBASIC 2.5}
'
' =========================================================================
'
[noparse][[/noparse] Program Description ]
'
[noparse][[/noparse] Revision History ]
'
[noparse][[/noparse] I/O Definitions ]
TX PIN 0 ' serial output to LCD
HALLSENSOR PIN 8 ' input signal from the Hall Effect Sensor
LED PIN 11 ' output to LED
'
[noparse][[/noparse] Constants ]
#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T2400 CON 396
T9600 CON 84
T19K2 CON 32
#CASE BS2SX, BS2P
T2400 CON 1021
T9600 CON 240
T19K2 CON 110
#ENDSELECT
'LCD Constants
LcdBaud CON T19K2
LcdBkSpc CON $08 ' move cursor left
LcdRt CON $09 ' move cursor right
LcdLF CON $0A ' move cursor down 1 line
LcdCls CON $0C ' clear LCD (use PAUSE 5 after)
LcdCR CON $0D ' move pos 0 of next line
LcdBLon CON $11 ' backlight on
LcdBLoff CON $12 ' backlight off
LcdOff CON $15 ' LCD off
LcdOn1 CON $16 ' LCD on; cursor off, blink off
LcdOn2 CON $17 ' LCD on; cursor off, blink on
LcdOn3 CON $18 ' LCD on; cursor on, blink off
LcdOn4 CON $19 ' LCD on; cursor on, blink on
LcdLine1 CON $80 ' move to line 1, column 0
LcdLine2 CON $94 ' move to line 2, column 0
LcdLine3 CON $A8
LcdLine4 CON $BC
LcdCC0 CON $F8 ' define custom char 0
LcdCC1 CON $F9 ' define custom char 1
LcdCC2 CON $FA ' define custom char 2
LcdCC3 CON $FB ' define custom char 3
LcdCC4 CON $FC ' define custom char 4
LcdCC5 CON $FD ' define custom char 5
LcdCC6 CON $FE ' define custom char 6
LcdCC7 CON $FF ' define custom char 7
'
[noparse][[/noparse] Variables ]
idx1 VAR Byte
idx2 VAR Byte
char VAR Byte
newChar VAR Byte
counter VAR Word
hicounter VAR Word
currState VAR Byte
prevState VAR Byte
'
[noparse][[/noparse] EEPROM Data ]
' C#---- Data
CC0 DATA LcdCC0, $0E, $1F, $1C, $18, $1C, $1F, $0E, $00
CC1 DATA LcdCC1, $0E, $1F, $1F, $18, $1F, $1F, $0E, $00
CC2 DATA LcdCC2, $0E, $1F, $1F, $1F, $1F, $1F, $0E, $00
Smiley DATA LcdCC3, $00, $0A, $0A, $00, $11, $0E, $06, $00
Msg2 DATA "IS VERY COOL! ", 3
'
[noparse][[/noparse] Initialization ]
Reset:
HIGH TX ' setup serial output pin
PAUSE 100 ' allow LCD to initialize
DnLoad_Custom_Chars:
FOR idx1 = 0 TO 35 ' download 4 characters
READ CC0 + idx1, char ' get data from table
SEROUT TX, LcdBaud, [noparse][[/noparse]char] ' send to LCD
NEXT
'
[noparse][[/noparse] Program Code ]
' Clear the display and remove cursor and blinking attributes.
Main:
· prevState = 1
· counter = 0
· hicounter = 0
· INPUT HALLSENSOR
· OUTPUT LED
· SEROUT TX, LcdBaud, [noparse][[/noparse]LcdBLon, LcdOn1, LcdCls]
Melexis:
· SEROUT TX, LcdBaud, [noparse][[/noparse]LcdBLon, LcdOn1, LcdLine1]
· SEROUT TX, LcdBaud, [noparse][[/noparse]"Pin 8 = "]
· currState = HALLSENSOR
· ' Only print pin state if it changed (allows us to poll input faster)
· IF currState = 0 AND prevState = 1 THEN
··· SEROUT TX, LcdBaud, [noparse][[/noparse]"0"]
··ENDIF
· IF currState = 1 AND prevState = 0 THEN
··· SEROUT TX, LcdBaud, [noparse][[/noparse]"1"]
· ENDIF
· IF currState <> prevState THEN
··· IF currState = 0 THEN ' sensor was tripped
····· counter = counter + 1
····· IF counter = 10000 THEN
······· counter = 0
······· hicounter = hicounter + 1
····· ENDIF
····· LED = 1
····· SEROUT TX, LcdBaud, [noparse][[/noparse]LcdLine2, "Count = ", DEC5 hicounter, DEC4 counter]
····· PAUSE 200 'Debounce timeout
····· LED = 0
··· ENDIF
··· prevState = currState
· ENDIF
GOTO Melexis
END
'
[noparse][[/noparse] Subroutines ]
Post Edited (dwalton) : 3/12/2009 4:23:40 PM GMT