Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
Hi brains trust, trying to interface a micromag3 magnetic sensor in MMBasic
This is an Altronics Z6300 purchase, aro Jim Rowes article in Silicon Chip Mag (June 2024 p64), which was encoded in Arduino language which I have not studied as I tend to a-{void} it, However the module requires a reset on pin 6 to clear previous data, and an SPI interface at 9600 baud, with a 40 mS delay following its data request, as Jim mentioned followed by DOUT request for, respectively x,y,z magnetic fields as byte Cmdx = B01100001 , and for Y ....01 and Z ....011. followed by 16 bit data in... Question is: what code in MMBasic is required ? Its ok I know the Pins Pin x, high 1 for 10 uS ? Open "Com1:9600, 9 bit" as #1 ? and so on. Not quite sure, and struggling here
regards Geodav ( a newbie)
panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1145
Posted: 05:35am 10 Nov 2026
Copy link to clipboard
Print this post
Hi GeoDav,
Welcome to TheBackShed forum. To assist you, can you say what MicroMite you plan to use to talk to the MicroMag3 and also what version of MMBasic you have installed (there are some minor differences in the SPI usage between versions and models of MicroMite/PicoMite)? Also, does your MicroMite have a display and if so, what type?
Doug
The following is a translation by Gemini - it should be close to what you need - you will need to adapt it to your particular platform and MMBasic version.
' ============================================================================== ' MicroMag3 Magnetic Sensor Control Program ' Translated to MMBasic for Micromite / Maximite / PicoMite ' Based on Silicon Chip June 2024 (Jim Rowe) and September 2024 updates ' ==============================================================================
Option Explicit
' Define Pin Numbers (Adjust these to match your hardware layout) Const pinSSNOT = 1 ' Slave Select / Chip Select (Output) Const pinRESET = 2 ' Reset Line (Output) Const pinDRDY = 3 ' Data Ready Line (Input) ' Note: Hardware SPI pins (SDI, SDO, SCK) are automatically managed by SPI OPEN.
' Global tracking variables Dim x_val, y_val, z_val Dim heading_deg Dim PI : PI = 4 * Atn(1)
' Initialise I/O Pins SetPin pinSSNOT, DOUT : Pin(pinSSNOT) = 1 ' Set High (Idle) SetPin pinRESET, DOUT : Pin(pinRESET) = 0 SetPin pinDRDY, DIN
' Initialise Hardware SPI channel ' Configures SPI Mode 0, Master, 8-bit data, and a safe clock speed (e.g., 100kHz) SPI Open 100000, 0, 8
' ============================================================================== ' FUNCTION: ReadAxis ' Pulses the MicroMag3, sends the axis command, and extracts 16-bit signed data ' ============================================================================== Function ReadAxis(axisNum) Local cmdByte, highByte, lowByte, rawWord
' Formulate the command byte based on the desired axis: ' Core measurement control configuration: division ratio /2048 (Bits: 01100) ' End bits map: X=01 (1), Y=10 (2), Z=11 (3) If axisNum = 1 Then cmdByte = &B00110001 ' X Axis ElseIf axisNum = 2 Then cmdByte = &B00110010 ' Y Axis Else cmdByte = &B00110011 ' Z Axis EndIf
' 3. Transmit the command byte via SPI SPI Write 1, cmdByte
' 4. Wait for the Data Ready line to clear/assert or fall back on a safe timeout ' Alternatively, if bypassing IO6 as per Silicon Chip, use a hard delay (e.g., Pause 15) Local timeout = 0 While Pin(pinDRDY) = 0 And timeout < 500 timeout = timeout + 1 Pause 1 Wend
' 5. Retrieve 16 bits of measurement data (2 Bytes) highByte = SPI(0) lowByte = SPI(0)
' Deselect the sensor Pin(pinSSNOT) = 1
' Combine bytes into a raw 16-bit integer rawWord = (highByte * 256) + lowByte
' Handle 16-bit two's complement sign conversion If rawWord >= 32768 Then ReadAxis = rawWord - 65536 Else ReadAxis = rawWord EndIf End Function
' ============================================================================== ' FUNCTION: GetHeading ' Implements a manual 4-quadrant arctan safely using the standard MMBasic ATN() ' ============================================================================== Function GetHeading(x, y) Local angle
' Trap zero denominators cleanly to avoid division-by-zero math errors If x = 0 Then If y < 0 Then angle = PI / 2 If y >= 0 Then angle = 3 * PI / 2 Else ' Basic single-variable arctan angle = Atn(y / x)
' Quadrant correction logic matching the module's magnetic framework If x < 0 Then angle = PI - angle ElseIf x > 0 And y < 0 Then angle = -angle ElseIf x > 0 And y > 0 Then angle = (2 * PI) - angle EndIf EndIf
' Convert Radians to Degrees and normalise Local deg = angle * (180 / PI) If deg < 0 Then deg = deg + 360
GetHeading = Int(degMod(deg)) End Function
' Helper to tightly clamp degrees inside a 0-359 compass bounds Function degMod(d) Dim outD = d Do While outD >= 360 : outD = outD - 360 : Loop Do While outD < 0 : outD = outD + 360 : Loop degMod = outD End Function Use code with caution.
Essential Conversion Notes
1. SPI Implementation: Rather than bit-banging the individual pins, the translation implements MMBasic's native hardware SPI OPEN and SPI Write / SPI() stream functions. This reflects the design principles highlighted in the original Silicon Chip article. 2. Quadrant Math (ATN vs ATAN2): Because standard MMBasic does not natively expose a two-argument ATAN2 function, the GetHeading routine features safety boundaries for X = 0 along with explicit block filters to accurately isolate the four vector quadrants. 3. Data Handling: The code transforms raw High/Low incoming bytes back into standard signed variables using 16-bit two's complement logic explicitly parsed for standard BASIC limitations. To tailor this exactly to your hardware setup, could you let me know: • Which specific hardware platform are you using to run MMBasic (e.g., a custom Micromite, a Maximite, or a PicoMite)? • Do you need to adjust the default SPI channel assignments or pin numbers inside the code to match how your module is wired? Try without personalization • Silicon Chip September 2024 September 9, 2024 — There is no ATAN2 function on the Micromite, so one has to use the ATN function and then work out which quadrant the heading lies ... • Geoff's Projects MMBasic MMBasic is a Microsoft BASIC compatible implementation running on hardware like Maximite (PIC32) and DOS, featuring floats, string... • MMBasic BASIC Interpreter Overview - MMBasic MMBasic is a full featured interpreter for the BASIC language. It's focus is on ease of use and rapid software development. This p... Show all
Edited 2026-11-10 16:12 by panky... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1145
Posted: 12:07am 11 Nov 2026
Copy link to clipboard
Print this post
Further to my response above, can you advise what MicroMite platform you are using to talk to the MicroMag?
I have tested an updated version of the converted software (see below) and it runs on a Silicon Chip Packpack V2 (PIC32MX170 based) running MMBasic V5.05.05. As I don't have a MicroMag, I can't say if the SPI section is sending and receiving correctly.
If you want assistance in loading or testing the software any further, let me know.
'======================================================================== ' MicroMag3 Magnetic Sensor Control Program ' Translated to MMBasic for Micromite / Maximite / PicoMite ' Based on Silicon Chip June 2024 (Jim Rowe) and September 2024 updates '========================================================================
Option Explicit
' Define Pin Numbers (Adjust these to match your hardware layout)' ' - currently set for Sil Chip Backpack MicroMite running MMBasic V5.05.05 Const pinSSNOT = 15 ' Slave Select / Chip Select (Output) Const pinRESET = 16 ' Reset Line (Output) Const pinDRDY = 17 ' Data Ready Line (Input) ' Note: Hardware SPI pins (SDI, SDO, SCK) are automatically managed by SPI OPEN.
' Global tracking variables Dim x_val, y_val, z_val Dim heading_deg
Dim outD
' Initialise I/O Pins SetPin pinSSNOT, DOUT : Pin(pinSSNOT) = 1 ' Set High (Idle) SetPin pinRESET, DOUT : Pin(pinRESET) = 0 SetPin pinDRDY, DIN
' Initialise Hardware SPI channel ' Configures SPI Mode 0, Master, 8-bit data, ' and a safe clock speed (e.g., 100kHz) SPI Open 500000, 0, 8
' ============================================================================== ' FUNCTION: ReadAxis ' Pulses the MicroMag3, sends the axis command, and extracts 16-bit signed data ' ============================================================================== Function ReadAxis(axisNum) Local cmdByte, highByte, lowByte, rawWord
' Formulate the command byte based on the desired axis: ' Core measurement control configuration: ' division ratio /2048 (Bits: 01100) ' End bits map: X=01 (1), Y=10 (2), Z=11 (3) If axisNum = 1 Then cmdByte = &B00110001 ' X Axis ElseIf axisNum = 2 Then cmdByte = &B00110010 ' Y Axis Else cmdByte = &B00110011 ' Z Axis EndIf
' 3. Transmit the command byte via SPI SPI Write 1, cmdByte
' 4. Wait for the Data Ready line to clear/assert or fall back ' on a safe timeout ' Alternatively, if bypassing IO6 as per Silicon Chip, use a ' hard delay (e.g., Pause 15) Local timeout = 0 While Pin(pinDRDY) = 0 And timeout < 500 timeout = timeout + 1 Pause 1 Wend
' 5. Retrieve 16 bits of measurement data (2 Bytes) highByte = SPI(0) lowByte = SPI(0)
' Deselect the sensor Pin(pinSSNOT) = 1
' Combine bytes into a raw 16-bit integer rawWord = (highByte * 256) + lowByte
' Handle 16-bit two's complement sign conversion If rawWord >= 32768 Then ReadAxis = rawWord - 65536 Else ReadAxis = rawWord EndIf End Function
' ============================================================================== ' FUNCTION: GetHeading ' Implements a manual 4-quadrant arctan safely using the standard MMBasic ATN() ' ============================================================================== Function GetHeading(x, y) Local angle
' Trap zero denominators cleanly to avoid division-by-zero math errors If x = 0 Then If y < 0 Then angle = PI / 2 If y >= 0 Then angle = 3 * PI / 2 Else ' Basic single-variable arctan angle = Atn(y / x)
' Quadrant correction logic matching the module's magnetic framework If x < 0 Then angle = PI - angle ElseIf x > 0 And y < 0 Then angle = -angle ElseIf x > 0 And y > 0 Then angle = (2 * PI) - angle EndIf EndIf
' Convert Radians to Degrees and normalise Local deg = angle * (180 / PI) If deg < 0 Then deg = deg + 360
GetHeading = Int(degMod(deg)) End Function
' Helper to tightly clamp degrees inside a 0-359 compass bounds Function degMod(d) outD = d Do While outD >= 360 : outD = outD - 360 : Loop Do While outD < 0 : outD = outD + 360 : Loop degMod = outD End Function ' Use code with caution.
' Essential Conversion Notes
' 1. SPI Implementation: Rather than bit-banging the individual pins, the ' translation implements MMBasic's native hardware SPI OPEN and ' SPI Write / SPI() stream functions. This reflects the design principles ' highlighted in the original Silicon Chip article. ' 2. Quadrant Math (ATN vs ATAN2): ' Because standard MMBasic does not natively expose a two-argument ' ATAN2 function, the GetHeading routine features safety boundaries ' for X = 0 along with explicit block filters to accurately isolate ' the four vector quadrants. ' 3. Data Handling: ' The code transforms raw High/Low incoming bytes back into standard ' signed variables using 16-bit two's complement logic explicitly ' parsed for standard BASIC limitations.
... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
Wow... thank you so much Panky, this is more than I expected from our Backshed specialists as the request is quite simplistic on my behalf as a newbie. Initially I shall use the PIC32M250 on MM basic ver 4.5, but shall continue with a Picomite2 V6 once she's working. Shall adjust the CPU frequency accordingly, and have observed the COM pinouts etc However having seen this translation of yours, it gives one a very good idea of how to interrogate the MicroMag for data out at once per second, and how to go from here.
Dont have a screen attached to this one, as it is to be used as a driveway sentry, for vehicles which would register at over 1000 uT magnetic field easily and then transmit the data at 433 MHz to my receiver where Ime working.
However you have thought through everything, and can only thanks you for this, sir