reading pwm on cmp03 compass?

tonto2k

Member
Hi,

I am trying to read the pwm signal from a cmp03 magnetic compass. I have read the documentation (http://www.robot-electronics.co.uk/htm/cmps3tech.htm) but I am unsure what code to use because there are no examples on the documentation? I tried a google search for cmp03 pwm but couldn't find anything :(

All I want to do is get a value from the compass so I can use it in my project ! Could anyone tell me or point me in the right direction to the code I should be using please ?

P.S I had it working when I originally used it over a year ago and I had the code on my PC but I have reformatted it since, and forgotten !

Thanks.
 

Technical

Technical Support
Staff member
Try using the count command over a period of time -the faster the pulses the higher the count value will be.
 

tonto2k

Member
HI,

Thanks for the reply. I tried the count command and tried various times. I was able to get a reading using debug.

However, the reading has nothing to do with the bearing the compass is on and fluctuates from 0 to 200 or so (gradually). It actually stops after about ten seconds as well ! ? ? !

My code is:

symbol range = b1

main:
Count 3,1000,range
debug range
goto main

I might just try and use i2C instead :( !
 

tonto2k

Member
Yeah I will give it a try tomorrow. I must admit, I tried setting it up via i2C today but I had no joy whatsoever ! At least there are some code examples available :)
 

BeanieBots

Moderator
Be careful, if you've messed with the calibration all readings could be meaningless until you get it right. That might be what you are seeing with the PWM measurements.

Been a few years since I played with one but I thought the PWM output was fixed frequency, so count won't work. I could be wrong, I'll dig out the datasheet.
 

tonto2k

Member
Got it working today with i2C :)

I used 2 x 4k7 resistors on the i2C bus and rewired the compass etc on the breadboard. I tried recalibrating it using the momentary switch method but it was too much messing round. Seeing as I am in the UK and the compass default calibration is set for the uk I used writei2c to restore factory calibration :)

Now I have it all working am glad I switched to i2C, I can add more stuff on the bus instead of using up inputs/outputs.

Thanks for all the help.
 

BeanieBots

Moderator
Gald you got it working.
I tried several different types and found the CMP03 to be the best. (in that price range)
Good resolution and very stable.
 
Top