SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By Tomadevil
#188042
Hi Guys,

I've been working a project, which involves using a CMPS11 digital compass. I'm an amateur at programming, so please excuse me for my silly questions.
I'm building a robot, which need to know its heading accurately. I chose I2C communication as the update rate of the signals has to be at least 15hz.
I used this program to read out the orientation:
http://www.robot-electronics.co.uk/htm/cmps11i2c.htm

As far as I know, the "angle full" would give me the heading. For some reasons, It's not accurate at all, actually, pretty useless. I want to calibrate the compass, but I'm stuck with it. I tried to send the commands in, but it doesn't seem to work. Can someone help me out and tell me what's wrong?
Code: Select all
#include <Wire.h>

#define ADDRESS 0x60

void setup(){
  Wire.begin();
  Serial.begin(9600);
  }

void loop(){
}

void calibrate(){

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF0);
  delay(25);

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF5);
  delay(25);

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF6);
  Wire.endTransmission();
  delay(25);

}
Regards,
Tamas
By jremington
#188062
You are sending the wrong command (0xF6 instead of 0xF7).

From the web page:
First of all you need to enter the calibration mode by sending a 3 byte sequence of 0xF0,0xF5 and then 0xF7 to the command register, these MUST be sent in 3 separate I2C frames, you cannot send them all at once. There MUST be a minimum of 20ms between each I2C frame. The LED will then extinguish and the CMPS11 should now be rotated in all directions on a horizontal plane, if a new maximum for any of the sensors is detected then the LED will flash, when you cannot get any further LED flashes in any direction then exit the calibration mode with a command of 0xF8.
By Tomadevil
#188067
Thanks for pointing that out. I corrected it, but it still doesn't work. :(
By Tomadevil
#188069
I cannot start the calibration. The LED is not flashing as it is supposed to.
By jremington
#188074
Post the code you are actually using. It appears that you never even call calibrate().
By Tomadevil
#188077
Code: Select all
#include <Wire.h>

#define ADDRESS 0x60

void setup(){
  Wire.begin();
  Serial.begin(9600);
  }

void loop(){
}

void calibrate(){

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF0);
  delay(25);

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF5);
  delay(25);

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF7);
  Wire.endTransmission();
  delay(25);

}
Last edited by Tomadevil on Fri Feb 12, 2016 1:08 pm, edited 1 time in total.
By Tomadevil
#188078
Even if I'm able to start the process of calibration. How do I send the last command (0xF8) to the compass? I think I should it manually, but how?
By jremington
#188086
Please, always use code tags.

You need to actually call the function calibrate. Do that in setup() by adding this line:
Code: Select all
  calibrate(); 
I suggest to call calibrate, delay 30-90 seconds while you perform the procedure, then send the 0xF8.
Or, wait for a button press, then send 0xF8 when done calibrating.
By Tomadevil
#188096
Thank you. I put that into the program. It still doesn't want to start the calibration unfortunately. The program looks like this now:
Code: Select all
#include <Wire.h>

#define ADDRESS 0x60

void setup(){
  Wire.begin();
  Serial.begin(9600);
  while(!Serial);
  calibrate();
}

void loop(){
}

void calibrate(){

  Serial.println("Calibration Mode");
  delay(2000);  //2 second before starting
  Serial.println("Start");

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF0);
  delay(25);

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF5);
  delay(25);

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF6);
  delay(20000);

  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF8);
  Wire.endTransmission();
  Serial.println("done");


}
(Btw, 0xF7 command is for horizontal operation only)
By Valen
#188102
Have you considered removing the potential infinite-loop:
Code: Select all
while(!Serial);
If for whatever reason the serial port on your Arduino does not activate then it gets stuck there.
By Valen
#188112
Do you receive anything on the serial monitor? If so, then it is working. Just not how you expect it to do. And the serial port should have worked or else you couldn't upload the program to it. Though the baudrate might be set differenly. Since you have not explained what sort of microcontroller this is running on, I would like to know more about this. Evidently by the code it is an arduino of some kind. How have you connected the CMPS11 chip to it? Please show how the wires are connected, and if you have pull up resistors applied to the wires.
By Tomadevil
#188121
Yes I do receive messages on the serial monitor, but that doesn't necessarily mean it is working properly.
I'm sorry, you are right. I should have mentioned that this is running on an Arduino UNO. You can see how I connected it to the arduino here, and yes I have pull up resistors in circuit:
http://www.robot-electronics.co.uk/htm/ ... PS11%20I2C