Example Code for Arduino-IIC Mode Communication
This article provides example code for using the IIC mode on Arduino UNO to read and display bearing, pitch, and roll values from the CMPS10 sensor via the serial port. The project is compatible with CMPS12 and offers a practical guide for Arduino enthusiasts, revised by LEFF.
Software Preparation
Arduino IDE; Wire library (included in Arduino IDE by default). Copy the sketch into the arduino IDE, and upload.
Wiring Diagram
Sample Code
/*************
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);
}
Result
The serial port will output the CMPS10 software version, bearing (with one decimal place), pitch, and roll values. The expected output format is as follows:
CMPS10 Example V: [version]
Bearing = [value].[fine]
Pitch = [value]
Roll = [value]
Additionally, the following image shows the product in use:
Was this article helpful?
