Skip to content

Compass TinyShield Tutorial

Ever been lost and need a fun hobby project to help you find your way? Look no further than the Compass TinyShield!

To learn more about the TinyDuino Platform, click here


Description

This TinyShield features the high performance and low power Honeywell HMC5883L 3-axis compass. The HMC5883L includes state-of-the-art, high-resolution HMC118X series magneto-resistive sensors plus an ASIC containing amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy.

The Compass TinyShield incorporates level shifters and a local power supply to ensure proper and safe operation over the entire TinyDuino operating voltage range up to 5V.

To see what other TinyShields are compatible with this TinyShield, see the TinyShield Compatibility Matrix

Technical Details Honeywell HMC5883L Compass
  • 3-axix (X, Y, & Z)
  • Digital resolution: 12bit (2 milli-gauss Field Resolution in ±8 Gauss Fields)
  • 1° to 2° Degree Compass Heading Accuracy
  • Wide Magnetic Field Range (+/-8 Oe)
  • Low Power: 100uA
TinyDuino Power Requirements
  • Voltage: 3.0V - 5.5V 
  • Current: 100uA Due to the low current, this board can be run using the TinyDuino coin cell option.
Pins Used
  • A5/SCL - I2C Serial Clock line
  • A4/SDA - I2C Serial Data line
Dimensions
  • 20mm x 20mm (.787 inches x .787 inches)
  • Max Height (from lower bottom TinyShield Connector to upper top TinyShield Connector): 5.11mm (0.201 inches)
  • Weight: .96 grams (.03 ounces)

Notes

  • You can also use this shield without the TinyDuino – there are 0.1″ spaced connections for power, ground, and the two I2C signals along the side of the TinyShield to allow you to connect a different system. 
  • Previous versions of this board will look a bit different and have the board number ASD2613-R, however they are functionally equivalent to this updated version and the compass circuitry is identical.  Earlier versions also had two interrupt pins broken out to solder points, these are not present on the current version of this board.

Materials

TinyZero and Compass TinyShield

Hardware

Software


Hardware Assembly

Create a TinyDuino stack of the boards by attaching the processor of your choice to the Compass TinyShield using the tan 32-pin connectors. Just like this:

An assembled stack of a TinyZero and Compass TinyShield.

Now you can plug your micro USB cable from your computer to your TinyDuino stack.


Software setup

You do not need any additional libraries to read data from the HMC5883L sensor, as all of the data retrieval is done in the .ino program that is included in a zip file under Software.

Open this program in the Arduino IDE.


Upload Program

Once the program is open, make the proper Tools selections for your processor and COM port. In this tutorial, we used a TinyZero so the proper Tools selections for that board is shown:

  • Board: TinyZero
  • Build Option: Default
  • Port: COM## (This will depend on how your computer labels it)

If you're using a TinyDuino, the proper Tools selections will be:

  • Board: Arduino Pro or Pro Mini
  • Processor: ATmega328P (3.3V, 8MHz)
  • Port: COM## (This will depend on how your computer labels it)

Tools selections for the TinyScreen+ will look very similar to those for the TinyZero besides the change to Board: TinyScreen+.

Code
//-------------------------------------------------------------------------------
//  TinyCircuits Compass TinyShield Example Sketch
//  Using Honeywell HMC5883 in I2C mode to read out the x, y, and z axis compass
//  data
//
//  Created 2/16/2014
//  by Ken Burns, TinyCircuits http://TinyCircuits.com
//  Modified 7/23/2019
//  By Laverena Wienclaw, TinyCircuits
//
//  This example code is in the public domain.
//
//-------------------------------------------------------------------------------

#include <Wire.h>
#ifndef ARDUINO_ARCH_SAMD
#include <EEPROM.h>
#endif

#define HMC5883_I2CADDR     0x1E

int CompassX;
int CompassY;
int CompassZ;

#if defined(ARDUINO_ARCH_SAMD)
  #define SerialMonitor SerialUSB
#else
  #define SerialMonitor Serial
#endif

void setup()
{
  Wire.begin();
  SerialMonitor.begin(9600);
  HMC5883Init();
}

void loop()
{
  HMC5883ReadCompass();

  // Print out the compass data to the Serial Monitor (found under Tools)
  SerialMonitor.print("x: ");
  SerialMonitor.print(CompassX);
  SerialMonitor.print(", y: ");
  SerialMonitor.print(CompassY);
  SerialMonitor.print(", z:");
  SerialMonitor.println(CompassZ);

  // Delay a second
  delay(1000); 
}

void HMC5883Init()
{
  //Put the HMC5883 into operating mode
  Wire.beginTransmission(HMC5883_I2CADDR);
  Wire.write(0x02);     // Mode register
  Wire.write(0x00);     // Continuous measurement mode
  Wire.endTransmission();
}

void HMC5883ReadCompass()
{
  uint8_t ReadBuff[6];

  // Read the 6 data bytes from the HMC5883
  Wire.beginTransmission(HMC5883_I2CADDR);
  Wire.write(0x03); 
  Wire.endTransmission();
  Wire.requestFrom(HMC5883_I2CADDR,6);

  // Retrieve from all six data registers
  for(int i = 0; i < 6;i++)
  {
    ReadBuff[i] = Wire.read();
  }

  int16_t tempVal=0;
  tempVal = ReadBuff[0] << 8;
  tempVal |= ReadBuff[1];
  CompassX = tempVal;   // typecast to keep everything pretty

  tempVal = ReadBuff[2] << 8;
  tempVal |= ReadBuff[3];
  CompassZ = tempVal;

  tempVal = ReadBuff[4] << 8;
  tempVal |= ReadBuff[5];
  CompassY = tempVal;
}

Once you have the program outputting some data, you may want to know what all those numbers actually mean. To understand them better, we'll take a look at page 4 of the datasheet for the HMC5883L's directional explanation:

Directional Explanation from Datasheet

This image tells us that the arrows indicate the direction of the magnetic field that generates a positive output reading for the Normal Measurement configuration. If you want more detailed information on the sensor and the type of data it outputs, make sure to look at the HMC5883L datasheet available under Downloads.


Downloads


Contact Us

If you have any questions or feedback, feel free to email us or make a post on our forum. Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it.

Thanks for making with us!