Compass module interfacing (HMC6352) to an 18x

Mathias

New Member
Well let me start this off by saying ARGH!!! I have been unsuccessfully trying to interface Honeywell's HMC6352 compass module that I bought from Sparkfun for quite some time now. This module uses the i2c protocol and following the i2c tutorial from picaxe has yielded no results.

When using the 18x all I can get out of the compass is a single number in the first byte and 255 in the second byte. I believe that my addressing scheme must be the cause but I cannot for the life of me figure out what Im doing wrong! For the record Im attempting to address the device by its published address of $42. To request data I then perform an i2cwrite command, sending the ascii letter "A". When I attempt to read from the same address, all I get are two bytes, the first being the number 3 or 4 and the second 255. Does anyone have any experience using this device or similar units? Thanks in advance
-Matt
 

westaust55

Moderator
More information may help you get a response.

Firstly you give the part number for an IC but you mention "module"

Questions to start with are:

1. What is this for - hobby, school project, etc
2. Is it a Sparkfun or other module - give us a link to a datasheet, etc
3. upload your code so we can see where you might be going wrong

Edit:
sorry, did not read you post well. Yes it is from Sparkfun
 
Last edited:

Brietech

Senior Member
Post your code so we can see it. I looked up the datasheet for the HMC6352, and it looks like the write address is 0x42 and the read address is 0x43. It looks like you are supposed to write 1 byte (the "A") to 0x42, and then try to read 2 bytes back from address 0x43 to get the info back.
 

westaust55

Moderator
Seems that heading data is only as two bytes:

Heading Mode: The heading output data will be the value in tenths of degrees from zero to 3599 and provided in binary format over the two bytes.

In terms of the i2c address:

I2C Slave Address
The I2C slave address byte consists of the 7 most significant bits with the least siginificant bit zero filled. A described
earlier, the default (factory) value is 42(hex) and the legal I2C bounded values are between 10(hex) and F6(hex). This
slave address is written into EEPROM address 00(hex) and changed on the power up.
So your use of the $42 address is correct. The PICAXE’s with i2c comms implemented automatically toggle the LSB of the address to control the read and write modes in line with the BASIC commands within your PICAXE program. Hence the change to $43 for a read is handled automatically by PICAXE parts with i2c comms.
 
Last edited:

daisy8350z

New Member
Anyone got code ?

Does anyone have any working code for use with this module ? I am using the HMC6353 (Same but with tilt info) and am pulling all my hair out !!

I have been using the CMPS03 and have had great results but this new module is more confusing.

Thanks
daisy
 

angrypirate

New Member
perhaps...

I have been struggling with a similar problem. However i get numbers 0-6 and 255. I'm using 3 volt, with 10kohm pull up resistors, a picaxe 18x and a sparkfun hmc6352 board. heres the code I'm using:

main:
i2cslave $42, i2cslow, i2cbyte
i2cwrite ($41)
pause 7
i2cread (w0)
debug w0
goto main

any help would be appreciated.
After some reasearch it looks like the Picaxe readi2c has some problems, it may be possible to use bit banging.
 
Last edited:

Technical

Technical Support
Staff member
Your code is incorrect. What you need is

i2cslave $42, i2cslow, i2cbyte
main:
pause 100
i2cread $41,(b1,b0)
debug w0
goto main

or

i2cslave $42, i2cslow, i2cbyte
main:
i2cwrite ($41)
pause 7
i2cread (b1,b0)
debug w0
pause 100
goto main

We've
- corrected the i2cread line. You need to write the command $41 and then read back two bytes (b1 and b0 do make up w0, but you cannot use w0 directly in the command as the i2c protocol uses bytes, not words).
- i2slave should be just once, not in the loop
- made the pause longer, just easier to see in debug!
 
Last edited:

angrypirate

New Member
argh indeed

Thank you for the corrections, however I still have the same problem. It returns 255 for b0 and a value 0 - 6 for b1 for both sets of code.
 

angrypirate

New Member
Yes, I've tried 3 volt from battery and dc adapter and 4.5 volt from battery and dc adapter. Ive also tried various value resistors and decoupling capacitors to smooth out any noise. I've tried moving the sensor as far away from any magnetic disturbances (ie: CRT monitors, computers, cell phones, etc) and shielding connections from noise from nearby connections. Ive tried changing the pause lengths and various other bits of the code but no matter what I try i get a first byte value of 0-7 and a second byte of 255. Needless to say I'm all out of ideas beyond trying to bit bang the incoming i2c signal. thank you for your help.
 

CIPHERSYSTEMS

New Member
Hi Guys,

I have a thread open and I am having EXACLY the same poblem.

I am going to try and put the unit into other modes, eg continous poll etc.

Has anyone else had luck?
 

westaust55

Moderator
here is some more detaield explanation.

I will stick with older i2c commands for now as none of the enquirers has indicate exactly which PICAXE they have.

From the datasheet I see:
Pull-up resistance values of about 10k ohms are recommended with a nominal 3.0-volt supply voltage.”

A lower value is typically better for higher speeds so although the speed is slow, suggest 4.7KOhm in lieu opf 10kOhm.

From the datasheet I see:
“HMC6352 7-bit slave address is 42(hex) for write operations”
And
“The data rate is the standard-mode 100kbps rate as defined in the I2C Bus Specification 2.1.”

Note that the PICAXE will look after the 8th bit for write and read control so always just leave that as “0” so the slave address is always $42.


So the setup line is:
i2cslave $42, i2cslow, i2cbyte


from the manual:
“After the master device sends the 7-bit slave address with the 1-bit Read/Write,
and gets the 1-bit slave device acknowledge bit returned;
the next one to three sent data bytes are defined as the input command and argument bytes.”

So a command to the compass module is in the format:

I2cwrite (command, [extra1], [extra2])

where [extra1[ and [extra2[ are optional and inserted as required without the square brackets.

In query mode, the command byte to read the data is the letter “A” which equates to hex value $41.

So to have the compass module perform a heading detection and provide the heading data we need:

i2cwrite ($41)

Note that after the first “A” command the compass will keep performing heading detections and making the data available at the next read so repeated “A” commands are not required.

The compass will then send two bytes of date for the heading value with the MSB first and the LSB second. Datasheet table 3 indicates a 6000usec delay = 3 milliseconds so first we can add:

Pause 3 ; may later find there is enough system delays to reduce or remove this but use it to start with.


To read this heading data into two byte variables we must use:

I2cread (b1,b0)


Byte variables b1 and b0 equate to the MSB and LSB respectively for word variable w0 so we can see the data in w0.

To see the value in w0 on the PC screen under the programming editor we add the command

Debug


We can then add another pause for a short delay

Pause 10

Then go back and read the compass module again as above.

So in summary:

Code:
i2cslave $42, i2cslow, i2cbyte 
i2cwrite ($41)

PAUSE 3
FOR b3 = 0 to 100
   I2cread (b1,b0)

   DEBUG
   PAUSE 10

NEXT b3
I have not delved into all the other modes, registers, etc her but as I read it, the compass should start in a default mode that the above will work.



If you are looking for more help please:
1. indicate what you project is for (assignment, hobby, etc)
2. which PICAXE chip you are using - affects which comamnds we use
3. advise which PICAXE starter/protoboard you are using - then we know if youa re connecting correctly
4. post your code written so far
5. upload your circuit diagram so we can see if that is correct then we know if you are connecting correctly

5 try to get a clear photo of the entire circuit showing all components and clearly showing where each wire is going
 

alband

Senior Member
Hi,
I'm interested in using that compass module and was wondering if you got it working according to west's instructions?
If so, what is it like? - how badly is it effected by surroundings?
 

CIPHERSYSTEMS

New Member
Hi westaust55,

I have 2 4.7k pull up resistors,

followed your code, but I am still getting -

b0= 255
b1 = seems to be a good result.

you do seem to have to poll the thing with a 'A' to get the next set manually.

So I am about to give up and get a different compass!!!!

Anymore ideas?


Dave
 

westaust55

Moderator
Sparkfun HMC6352 compass

Okay, had another read of the datasheet.

Yes in the default (standby) mode, you will have to send the “A” to the compass to initiate a new measurement. It is the query mode thatdoes not need repeated issue of the “A” command.

Also had a look also at the C program code in Portuguese and after some translation not greatly different. He did have a slightly longer pause between writing the “A” ($41) and reading the data. Turns out this is exactly the same as the C program code in the wiring information section on the sparkfun website.

So the code for default mode in PICAXE BASIC would be like this:
Code:
i2cslave $42, i2cslow, i2cbyte 


FOR b3 = 0 to 100
   I2CWRITE ($41)
   PAUSE 10

   I2CREAD (b1,b0)

   DEBUG
   PAUSE 10

NEXT b3
I cannot see immediately why the lower byte of data (b0) is always returning 255.


Is the higher byte (b1) varying through the range 0 to 14 ?


Have you tried doing a calibration?

With the Robot Electronics CMPS03 module which uses different hardware, it is essential to calibrate otherwise the data can definitely be very strange looking. Robot Electronics state that even disconnection with the microcontroller still powered-up (bad practice at anytime) can lead to loss of compass calibration.

User Calibration
The HMC6352 provides a user calibration routine with the “C” command permitting entry into the calibration mode and the “E” command to exit the calibration mode.

Once in calibration mode, the user is requested to rotate the compass on a flat surface at least one full circular rotation while the HMC6352 collects several readings per second at various headings with the emphasis on rotation smoothness to gather uniformly spaced readings. Optimally two rotations over 20 seconds duration would provide an accurate calibration. The calibration time window is recommended to be from 6 seconds up to 3 minutes depending on the end user’s platform.
The calibration routine collects these readings to correct for hard-iron distortions of the earth’s magnetic field. These hard-iron effects are due to magnetized materials nearby the HMC6352 part that in a fixed position with respect to the end user platform.
An example would be the magnetized chassis or engine block of a vehicle in which the compass is mounted onto. Upon exiting the calibration mode, the resulting magnetometer offsets and scaling factors are updated
 
Top