How to use
The CMPS has two interfaces: IIC and Serial mode.
For specific information, please go to the Official website:
Here, we will try the IIC mode on Arduino UNO.
Diagram
Sketch
Copy the sketch into the arduino IDE, and upload.
/*************
This sketch is revised by LEFF in DFRobot, based on the official one with
LCD05 display @ http://www.robot-electronics.co.uk/files/arduino_cmps10.ino
*************/
#include <Wire.h>
#define ADDRESS 0x60 // Defines address of CMPS10
void setup(){
Serial.begin(9600);
Wire.begin(); // Conects I2C
}
void loop(){
byte highByte, lowByte, fine; // highByte and lowByte store high and low bytes of the bearing and fine stores decimal place of bearing
char pitch, roll; // Stores pitch and roll values of CMPS10, chars are used because they support signed value
int bearing; // Stores full bearing
Wire.beginTransmission(ADDRESS); //starts communication with CMPS10
Wire.write(2); //Sends the register we wish to start reading from
Wire.endTransmission();
Wire.requestFrom(ADDRESS, 4); // Request 4 bytes from CMPS10
while(Wire.available() < 4); // Wait for bytes to become available
highByte = Wire.read();
lowByte = Wire.read();
pitch = Wire.read();
roll = Wire.read();
bearing = ((highByte<<8)+lowByte)/10; // Calculate full bearing
fine = ((highByte<<8)+lowByte)%10; // Calculate decimal place of bearing
display_data(bearing, fine, pitch, roll); // Display data to the Serial
delay(500);
}
void display_data(int b, int f, int p, int r){ // pitch and roll (p, r) are recieved as ints instead oif bytes so that they will display corectly as signed values.
Serial.print("CMPS10 Example V:");
Serial.println(soft_ver()); // Display software version of the CMPS10
delay(5); // Delay to allow Serial to proscess data
Serial.print("Bearing = "); // Display the full bearing and fine bearing seperated by a decimal poin on the Serial
Serial.print(b);
Serial.print(".");
Serial.println(f);
delay(5);
Serial.print("Pitch = ");
Serial.println(p);
delay(5);
Serial.print("Roll = ");
Serial.println(r);
}
int soft_ver(){
int data; // Software version of CMPS10 is read into data and then returned
Wire.beginTransmission(ADDRESS);
// Values of 0 being sent with write need to be masked as a byte so they are not misinterpreted as NULL this is a bug in arduino 1.0
Wire.write((byte)0); // Sends the register we wish to start reading from
Wire.endTransmission();
Wire.requestFrom(ADDRESS, 1); // Request byte from CMPS10
while(Wire.available() < 1);
data = Wire.read();
return(data);
}
FAQ
For any questions, advice or cool ideas to share, please visit the DFRobot Forum.