The code presented on this page can be downloaded or found in the Arduino examples menu under Examples → SpinWearables → Compass → Calibrated_Compass.

A Compass with Automatic Calibration

#include "SpinWearables.h"
using namespace SpinWearables;

void setup() {
  // Ensure all of the SpinWheel hardware is on.

float minx, miny, minz, maxx, maxy, maxz;

void loop() {
  // Read all sensor data.

  // Measure the magnetic field along the three axes of the sensor.
  float x =;
  float y =;
  float z =;

  if (x > maxx) maxx=x;
  if (y > maxy) maxy=y;
  if (z > maxz) maxz=z;
  if (x < minx) minx=x;
  if (y < miny) miny=y;
  if (z < minz) minz=z;

  float truex = x - (maxx+minx)/2;
  float truey = y - (maxy+miny)/2;
  float truez = z - (maxz+minz)/2;

  // Use this line if you want to skip the correction.
  //uint8_t angle = (-atan2(y, x)+3.1415/2)/2/3.1415*255;
  uint8_t angle = (-atan2(truey, truex)+3.1415/2)/2/3.1415*255;
  SpinWheel.setSmallLEDsPointer(angle, 800, 0xffffff);