I’m using the CMPS14 ( https://www.robotshop.com/en/tilt-compensated-magnetic-compass-cmps14.html )
Reading register 0x00 tells me: Software version 4
The CMPS14 is connected as in this picture but with an arduino nano 328 (pin28 scl, pin 27 sda):
https://www.robot-electronics.co.uk/images/arduino_cmps12_i2c.png
Connection and communication seem to be working. I used the following code to do some tests:
#include <Wire.h>
#define CMPS14_ADDRESS 0x60
#define buttonA 13
#define buttonB 14
#define buttonC 15
#define buttonD 16
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(buttonC, INPUT_PULLUP);
pinMode(buttonD, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonA) == LOW) {
tone(12, 2000, 50);
sendEnableCalibration();
delay(500);
}
if (digitalRead(buttonB) == LOW) {
tone(12, 2000, 50);
sendDisableCalibration();
delay(500);
}
if (digitalRead(buttonC) == LOW) {
tone(12, 2000, 50);
sendEraseCalibration();
delay(500);
}
getCalState();
delay(100);
}
void getCalState() {
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x1E);
Wire.endTransmission();
Wire.requestFrom(CMPS14_ADDRESS, 1);
while (Wire.available() < 1);
byte calState = Wire.read();
Serial.print(bitRead(calState, 7));
Serial.print(bitRead(calState, 6));
Serial.print(bitRead(calState, 5));
Serial.print(bitRead(calState, 4));
Serial.print(bitRead(calState, 3));
Serial.print(bitRead(calState, 2));
Serial.print(bitRead(calState, 1));
Serial.println(bitRead(calState, 0));
}
void sendEraseCalibration() {
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0xE0);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0xE5);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0xE2);
Wire.endTransmission();
delay(20);
}
void sendEnableCalibration() {
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0x98);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0x95);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0x99);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0x87);
Wire.endTransmission();
delay(20);
}
void sendDisableCalibration() {
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0x98);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0x95);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0x99);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0x80);
Wire.endTransmission();
delay(20);
}
Starting the arduino the output says: 00001100 This is ok.
Pressing buttonA should start the automatic calibration. But the calibration state stays at 00001100.
Pressing buttonC (deletes the stored Calibration) an then buttonA the calibration is enabled. I ican see how the status change.
Pressing buttonC during calibration the status goes back to 00001100. This is ok.
I’d actually be happy with that, but …
Pressing buttonB should end calibration. But it doesn’t work.
Pressing buttonC and then buttonB the calibrations ends. But of course the calibration status is deleted.
So i think the communication is working. But there seems to be a problem with the order of execution of the commands or I totally think wrong.
Maybe it has something to do with the reset of the BNO080 that is executed after the last command of sendEraseCalibration!?!