Skip navigation.

Reading the HMR3100 Digital Compass

What a buzz! I managed to scavange some momentry switches, LED's, a Floppy drive power cable and a serial cable from some old PC's, so I could easily breadboard the Digital Compass circuitry.

I was initally going to connect the Digital Compass - HMR3100 to a PC using two MAX232 chips to do the TTL RS232 - 12V voltage translation, as it uses three wires to handle the data capture (RX, TX & RTS). It then occured to me that I could possibly get a second PICAXE 28X chip to handle the Digital Compass interface, and I would get some extra IO pins to boot!

After assemblying the initial circuit, I realised that the HMR3100 defaults to 9600 baud, however my PICAXE 28X could only handle 4800 baud. I could send away for an 8MHz resinator, but that would take a few days to get here. So with fear and trepidation, I desoldered the 0.5mm jumper and moved it to the jumper 3 position on the HMR3100. I had to dig up a magnifier glass to make sure it was all in place properly. Here is how the circuit looked:

Digital Compass testing Circuit

Next step was to write some test code. I had a few issues with the 'serin' command. Once I got that figured out I was away. I placed some extra LED's on the board so I could see what was going on. For a while the circuit would lock up after 10 minutes or so. Breaking out the old Tektronix 465B Oscilloscope, I found that there was a 0.5V digital ripple on the incoming 5V power line. I added a 0.1 micro Farad capacitor, and haven't had a problem since. Here is the Test assembly:

Testing the Digital Compass

The next night I added a calibration switch and LED. Pressing the calibration switch once, would force the RTS and RX lines low, putting the Compass into calibration mode. I can then carefully (lots of cables hanging off the board), rotate the assembly twice, and then press the calibration button again. The program then raises the RTS and RX lines high, and goes back to the main program. Here is the compleated breadboarded circuit:

Breadboarding the HMR3100 Digital Compass

It really worked! I can rotate the compass to face magnetic north, press the 'take a reading' button, and I then get the Compass reading on my PC.

Here is the PICAXE code I used:

 

' This is a test code to manually put the HMR3100 into and out of Calibration mode,
' and to take a Compass Header reading and transmit it back to a computer via a serial port

main:
' make sure the RTS and RX pins are set high
high 7 ' turn on the RTS LED
high 6 ' turn on the RX LED
' set the interrupt pin and condition, ie. pin 2 going low will interrupt the routine
setint %00000000, %00000100
high 4
pause 1000
if pin3=0 then calibration ' has the calibration button been pressed
low 4
pause 1000
if pin3=0 then calibration ' has the calibration button been pressed
goto main

interrupt:
low 4
high 5
pause 500
if pin2 = 0 then interrupt ' wait till the user has finished pressing the button before continuing
low 7
pause 1 ' wait for 2 mSec.
high 7
' ok, now that the RTS pin has gone low, lets read in the serial compass data
serin 7,T4800,b1,b2,b3
w3=b2 * 256 + b3
w4=w3/2
if b1=128 then skip
sertxd (" Warning Calibration needed! ",13,10,13,10)
skip:
' sertxd (" Serial Data 1: ", #b1,13,10) ' debug
' sertxd (" Serial Data 2: ", #b2,13,10) ' debug
' sertxd (" Serial Data 3: ", #b3,13,10) ' debug
sertxd (" Heading is: ", #w4,13,10)
' sertxd (" ",13,10) ' send a blank line to the PC ' debug
low 5
return

calibration:
' this places the compass in calibration mode, until the button is pressed again
if pin3=0 then calibration
pause 100 ' wait to debounce the switch
high 3 ' turn on the calibration LED
low 7 ' place the RTS pin low
low 6 ' place the RX pin low
cal_debounce:
if pin3=0 then cal_cont
pause 200
goto cal_debounce
cal_cont:
low 3 ' turn off the calibration LED
goto main