Calibrating LSM303 with mag.setOffset(X, Y, Z)

I have an Arduino Uno I am using with an Adafruit LSM303.

I am trying to calibrate it by using mag.setOffset(X, Y, Z) in my code (below) which results in the following errors:

/tmp/780218217/Compass_Test/Compass_Test.ino: In function 'void loop()':
/tmp/780218217/Compass_Test/Compass_Test.ino:24:7: error: 'class Adafruit_LSM303_Mag_Unified' has no member named 'setOffset'
mag.setOffset(0.81, 7.96, 0);

// Adafruit Unified Sensor - Version: Latest
// Adafruit LSM303DLHC - Version: Latest
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>

Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);

void setup(void) {
  Serial.println("Magnetometer Test");
  //Initalise the sensor
  if (!mag.begin())
    Serial.println("No magnometer detected. Check wiring.");
    while (1);
void loop(void) {
  //New sensor event
  sensors_event_t event;
  mag.setOffset(0.81, 7.96, 0);

  float Pi = 3.1415926535897932384626433832795028841971693993751;
  float heading = (atan2(event.magnetic.y, event.magnetic.x) * 180) / Pi;

  //Normalize to 0-360 degrees
  Serial.print("Compass Loaded");
  if (heading < 0)
    heading = 360 + heading;
  Serial.print("Compass Heading: ");

The error message is clear.

Look in the library code to see if there is such a function and what its actual name might be. Case matters.

Thanks. It seems that the mag.offSet is not in the library despite this being a recommended method by Adafruit and the library being from Adafruit.

Does anyone have ideas on how to calibrate an LSM303?

Glad you asked! Here is a comprehensive tutorial showing the best way to do it.

However, the much simpler min/max approach described here sometimes works.