Making Tracks!

Now let's use the LSM303 module to do some simple navigation!
The images on this page show the older LSM303DLH. You can use the newer LSM303AGR as well but you'll need to refer to the Pinouts page to determine which pins to use
One day, making tracks
In the prairie of Prax,
Came a North-Going Zax
And a South-Going Zax.
This Zax-O-Meter is the perfect navigation instrument for either a North or a South-going Zax. This project demonstrates how to use the LSM303 magnetometer output to implement a simple navigation system. No matter which way you turn, the pointer will always rotate to the desired direction of travel.
Never budge! That’s my rule.
Never budge in the least!
Not an inch to the west!
Not an inch to the east!
The Zax-O-Meter uses the computed compass heading as feedback to a continuous rotation servo. When the compass heading is zero degrees (due north), the servo stops rotating. Any deviation from that value causes the servo to rotate in the opposite direction to compensate. This basic principle of negative feedback can be used to build a navigation system for an autonomous robot.


To build the Zax-O-Meter, you will need:

Calibrate your servo:

For the Zax-O-Meter to function accurately, you first need to find the 'neutral' point of your continuous rotation servo. That is the servo output value that results in minimum rotation. This value is typically about 90, but varies somewhat between servos. Run this sketch and modify the value until you get minimum rotation. That is the value you should use for ServoNeutral in the Zax-O-Meter sketch.
Download: file
#include <Servo.h> 
Servo servo;  

void setup() 
  servo.attach(9);  // attaches the servo on pin 9 to the servo object 
  servo.write(90);  // change this value to achieve minimum rotation!
void loop() 

Mount the servo

Mark and widen the opening in the enclosure to fit the servo. Insert the servo with the rotor toward the center of the enclosure. Press down until the flanges are flush with the surface, the servo should snap into place and be held securely.

Mount the Uno and wire it up

Mount the Uno in the enclosure with the supplied screws. Wire the servo and sensor as follows:

  • Black -> Gnd
  • Red -> 5v
  • White -> Digital Pin 9

  • Gnd -> Gnd
  • Vin -> 3.3v
  • SDA -> Analog 4
  • SCL -> Analog 5
Then route the sensor wire through the opening next to the servo and close up the enclosure.

Add a pointer

Cut a pointer from some stiff cardboard or foam-core and attach it to the servo horn with some double-sided foam tape.

Attach the sensor to the underside of the arrow with some more double-sided foam tape. Locate the sensor as far away from the servo body as possible to avoid magnetic interference from the motor.

Add Zaxen!

Find an image of your favorite Zax. Use Paint or other image editing tool to make a mirror image pair.

Print the image on some heavy card-stock and fold it over to make a double-sided image.

Cut it out and mount to the top of your indicator arrow with some double-sided tape.


Load the code below. Don't forget to change "ServoNeutral" to the neutral value from the Servo Calibration step.

The example code is for a North-going Zax and uses a targetHeading of 0. If you are a Zax of the South-going persuasion, a targetHeading of 180 will keep you in your South-going groove. For those with a rebellious streak, choose any targetHeading setting from 0 - 360 degrees and start making tracks in the heading of your choice!
Download: file
// **********************************************
// Zax-O-Meter Sketch 
// for the Adafruit LSM303 Magnetometer Breakout
// Written by Bill Earl for Adafruit Industries
// **********************************************

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Servo.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);

// This is our continuous rotation servo
Servo servo;

// Pi for calculations - not the raspberry type
const float Pi = 3.14159;

// This is the value that gives you minimal rotation on
// a continuous rotation servo.  It is usually about 90.
// adjust this value to give minimal rotation for your servo
const float ServoNeutral = 97;  

// This is the desired direction of travel
// expressed as a 0-360 degree compass heading
// 0.0 = North
// 90.0 = East
// 180.0 = South
// 270 = West
const float targetHeading = 0.0;

void setup(void) 
  Serial.println("Magnetometer Test"); Serial.println("");
  /* Initialise the sensor */
    /* There was a problem detecting the LSM303 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
  servo.attach(9);  // Attach servo to pin 9

void loop(void) 
  /* Get a new sensor event */ 
  sensors_event_t event; 
  // Calculate the angle of the vector y,x
  float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
  // Normalize to 0-360
  if (heading < 0)
    heading = 360 + heading;
  // Calculate the error between tha measured heading and the target heading.
  float error = heading - targetHeading;
  if (error > 180)
    error = error - 360;  // for angles > 180, correct in the opposite direction.
  // A non-zero difference between the heading and the 
  // targetHeading will bias the servoNeutral value and 
  // cause the servo to rotate back toward the targetHeading.
  // The divisor is to reduce the reaction speed and avoid oscillations
  servo.write(ServoNeutral + error / 4 );

This guide was first published on Mar 10, 2013. It was last updated on Mar 10, 2013. This page (Making Tracks!) was last updated on Dec 11, 2019.