CMPS10 - Tilt Compensated Compass Module
I2C communication protocol with the compass module is the same as popular eeprom's such as the 24C04. First send a start bit, the module address with the read/write bit low, then the register number you wish to read. This is followed by a repeated start and the module address again with the read/write bit high. You now read one or two bytes for 8bit or 16bit registers respectively. 16bit registers are read high byte first. The compass has a 23 byte array of registers. organized as below:
|Compass Bearing as a byte, i.e. 0-255 for a full circle
|Compass Bearing as a word, i.e. 0-3599 for a full circle, representing 0-359.9 degrees.
|Pitch angle - signed byte giving angle in degrees from the horizontal plane
|Roll angle - signed byte giving angle in degrees from the horizontal plane
|Magnetometer X axis raw output, 16 bit signed integer with register 10 being the upper 8 bits
|Magnetometer Y axis raw output, 16 bit signed integer with register 12 being the upper 8 bits
|Magnetometer Z axis raw output, 16 bit signed integer with register 14 being the upper 8 bits
|Accelerometer X axis raw output, 16 bit signed integer with register 16 being the upper 8 bits
|Accelerometer Y axis raw output, 16 bit signed integer with register 18 being the upper 8 bits
|Accelerometer Z axis raw output, 16 bit signed integer with register 20 being the upper 8 bits
Register 1 is the bearing converted to a 0-255 value. This may be easier for some applications than 0-3599 which requires two bytes. For those who require better resolution registers 2 and 3 (high byte first) form a 16 bit unsigned integer in the range 0-3599. This represents 0-359.9°. Register 4 is the pitch angle, giving an angle of 0 when the board is flat and up to +/- 85° at maximum tilt in either direction. Register 5 works the same way but with results for the Roll angle. There is then an array of registers (10-21) providing all the raw sensor data from the magnetic and acceleration sensors. Finally Register 22 is the command register and is used to calibrate the compass, change address and if necessary restore the factory default calibration.
Calibration the CMPS10
I would recommend evaluating the CMPS10 performance first before implementing this function. Its purpose is to remove offsets caused by constant magnetic sources around the CMPS10. First of all you need to determine North and align the CMPS10 with it, then enter the calibration mode by writing 0xF0 to the command register (22). To calibrate the first point write 0xF5 to the command register, this should also light the LED, then move the object through 90° and write 0xF5 to the command register. Repeat this twice more so four points are calibrated and the LED should also turn off to confirm calibration completion. Please make sure that the CMPS10 is not located near to ferrous objects as this will distort the magnetic field and induce errors in the reading.
Restoring Factory Calibration
Should you need to revert to the factory calibration then write the following to the command register in 3 separate transactions with 100ms between each transaction: 0x20,0x2A,0x60. These commands must be sent in the correct sequence to restore the calibration, additionally, No other command may be issued in the middle of the sequence. The sequence must be sent to the command register at location 22, which means 3 separate write transactions on the I2C bus. (A write transaction is [start sequence] [I2C address] [register address] [command byte] [stop sequence] then a 100mS delay).
Changing the I2C Bus Address
To change the I2C address of the CMPS10 you must have only one module on the bus. Write the 3 sequence commands in the correct order followed by the address with 100ms between writes. Example; to change the address of a compass currently at 0xC0 (the default shipped address) to 0xC2, write the following to address 0xC0; (0xA0, 0xAA, 0xA5, 0xC2 ) with a 100ms delay after each of the first three bytes. These commands must be sent in the correct sequence to change the I2C address, additionally, No other command may be issued in the middle of the sequence. The sequence must be sent to the command register at location 22, which means 4 separate write transactions on the I2C bus. When done, you should label the CMPS10 with its address, however if you do forget, just power it up without sending any commands. The CMPS10 will flash its address out on the LED. One long flash followed by a number of shorter flashes indicating its address. The flashing is terminated immediately on sending a command the CMPS10.
Take care not to set more than one device to the same address, there will be a bus collision and very unpredictable results.
If you need more information on the I2C bus, have a look at our I2C tutorial.