Is it possible to calibrate the compass?


Active Member
Licensed User
Longtime User
The compass of my milestone2 is sometimes +-45° wrong
When I start a other Compass that can calibrate it'S ok.

But is there a Way to calibrate the compass with b4a?

in Java it looks like this

import icommand.nxt.comm.NXTProtocol;

public class CompassSensor extends I2CSensor {

private final static byte BEGIN_CALIBRATION = 0x43;
private final static byte END_CALIBRATION = 0x44;
private final static byte COMMAND = 0x41;

* Begins calibrating the compass sensor resulting in more accurate measurements. Rotate compass
* at least two times, taking at least 20 seconds per rotation. Issue stopCalibration() when done.
* NOTE: Once the compass is calibrated, you do not have to recalibrate even when the NXT is
* turned off. The calibration settings are stored in internal non-volatile memory on the NXT.

public void startCalibration() {

public void stopCalibration() {

Gruss Björn


Active Member
Licensed User
Longtime User
I think it's not only the Offset, the b4a compass give me really exact
angle in all directions after calibration.

In this java class it is possible to send the calibration commands to the compass. - bot-commander - Remote control Lego NXT robots from Android. - Google Project Hosting



is there a way to integrade this in the b4a produced java code?

thanks Björn
Last edited:
Upvote 0


Licensed User
Longtime User
I don't think that this code affects the built-in compass.
You can calibrate built-in compass:
start compass app or gps app that shows compass information.

take your phone with one hand.
put it horizontal then "draw" a horizontal half circle
after this draw a vertical half circle above your head.
and put your phone vertical and roll one circle

order is not important, but after do this compass sensor is calibrated.

look this: it works on my samsung galaxy mini
Upvote 0


Licensed User
Longtime User
public class CompassSensor extends I2CSensor

The above line is referencing I2C which is a protocol to communicate with firmware
embedded into hardware components.. this indicates to me that the code is sending
hardware-specific commands to the MCU part that's responsible for managing the

My two cents.
Upvote 0


Licensed User
Longtime User
Yes it looks to be a hardware specific piece of code, the source code states:

Currently supports Compass V1.1 and 2.0 (CMPS-Nx) and HiTechnic compass sensors.
Auto-detects appropriate model.

So it's not generic code that can be used on all devices, no doubt it'll cause an exception on devices that don't have the supported hardware.

Upvote 0