RobotCompassCalibration

Calibrate the compass module so it rotates evenly.

You may have noticed, the compass module on your Arduino Robot doesn't always give accurate readings. If you turn around the robot, towards certain directions the read out is more "dense" than the others. Magnetic sources near by, chassis of the robot, not to mention the earth's magnetic field distribution all affects the compass module. In this tutorial you'll learn to calibrate the compass on your robot with a few simple steps.

This is a tutorial for old models of Arduino robot. More specifically, ones with Honeywell HMC 6352 mounted. To identify which sensor your robot is using, simply look at the chip shown in pictures below.

LottieLemon HoneywellHMC6352

The sensor communicates through I2C, and there're a number of things you can configure. To know its full capabilities, check out their datasheet.

Below is the sketch for calibration.

1#include <Wire.h>
2
3#define ADDRESS 0x21
4
5void setup(){
6
7 Wire.begin();
8
9 Serial.begin(9600);
10
11 while(!Serial);
12
13 calibrate();
14}
15
16void loop(){
17}
18
19void calibrate(){
20
21 Serial.println("Calibration Mode");
22
23 delay(1000); //1 second before starting
24
25 Serial.println("Start");
26
27 Wire.beginTransmission(ADDRESS);
28
29 Wire.write(0x43);
30
31 Wire.endTransmission();
32
33 for(int i=0;i<15;i++){ //15 seconds
34
35 Serial.println(i);
36
37 delay(1000);
38
39 }
40
41 Wire.beginTransmission(ADDRESS);
42
43 Wire.write(0x45);
44
45 Wire.endTransmission();
46
47 Serial.println("done");
48
49}

So basically it's about establishing I2C with the compass, send over a calibration signal, and then send an end of calibration signal when it's done. All the command signals used here can be found in the datasheet. There're a few lines of code used for printing out debug messages, they're not necessary but helps greatly for guiding through the process.

After uploading the sketch, open the serial monitor. You'll see it displaying "Calibration Mode", and 1 second later "start". At this point, the sensor will read its position and self-calibrate for the next 15 seconds. What you need to do is slowly turn the robot around a few circles within the time period. You can see the count down from serial monitor.

When the calibration is done, you see "done" printed in serial monitor.

Now the compass module should be calibrated. Run the robot example "Robot_Control->learn->compass" and see if the readings are even across 360 degrees.

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.