MSP430LaunchPad

HMC5883L Magnetometer on the Launchpad with Energia

Note: This article is a blurb. Help expand it.

Tags

Tutorial

The following procedure will show you how to set up your launchpad with Energia and the HMC5883L magnetometer/compass and should work for both the MSP-430F5529 and the MSP-430G2553.

Hardware

I will be using the Triple-axis Magnetometer (Compass) Board - HMC5883L from Adafruit.

Breakout-Board Assembly

Assemble the board, by soldering the connector pins:

Circuit

The F5529 board uses the P3.0 (SDA0) and P3.1 (SCL) pins for I2C communication. For more information, see the F5529 board pinout here.

Connect the Launchpad and the magnetometer breakout board according to the following diagram:

The magnetometer is powered by the 3.3 volt line. The RDY pin is not connected in this tutorial, but can be used to receive notification when data is ready for reading, for high frequency sensing applications. The SDA and the SCA pins are connected through a 1.8 kΩ pull-up resistor each to the P3.0 and P3.1 pins on the MSPF5529 board, respectively.

Software

Energia Installation

  1. Download the latest Energia version from here(in my case I used the Windows Binary release).
  2. Extract the .zip file into a working folder.

Magnetometer Library Installation

  1. Download the Adafruit Arduino HMC5883_Unified library. This library can be modified to work with Energia and Launchpads
    1. Extract the .zip file into the /Energia/libraries/Adafruit_HMC5883_Unified folder.
  2. Download the Adafruit Arduino Sensor Driver.
    1. Extract the .zip file into the /Energia/libraries/Adafruit_Sensor folder.
    2. Edit the file Adafruit_Sensor.cpp and delete the line #include <avr/pgmspace.h>
  3. Restart Energia to make sure that the libraries are loaded. You can check if a library is set up, by clicking on Sketch->Import Library...->Adafruit_HMC5883_Unified or Sketch->Import Library...->Adafruit_Sensor, which will add the line #include <Adafruit_HMC5883_Unified.h> or #include <Adafruit_Sensor.h> to your sketch.

Sketch Creation

  1. Add the example that comes with the magnetometer library. To open the example click File->Examples->Adafruit_HMC5883_Unified->magsensor. This will open the examples sketch.
  2. The next step involves editing the sketch for the correct magnetic declination. The magnetic declination compensates for the error in the field reading, given your GPS coordinates. If this value is not properly selected, the readings will be off.
    1. Go to http://magnetic-declination.com/ and find the declination for your location.
    2. The website will give you the declination in degrees minutes seconds and must be first converted to decimal degrees. The conversion is: decimal degrees=degrees + minutes/60, thus for example +14° 44' = 14 + 44/60 = +14.73°
    3. You will then have to convert the declination from degrees to radians according to: radians=degrees * (pi/180), where pi=3.14159. In our example: +14.73°= 0.257 rad.
    4. Now enter the declination on line 91 that says: float declinationAngle = 0.257;

Uploading Sketch

First select the appropriate board (here MSP430F5529) and COM port in Energia. Click upload and the sketch should compile without problems. The Serial Monitor output can be accessed by pressing [Ctrl]+[Shift]+[M] and should show something like this:

The first line shows the sensed magnetic field in the x, y, and z direction in units of micro-tesla (where 1uT=0.000001 T and 1 tesla=10,000 gauss) . The second line uses these values to calculate the heading of the magnetometer (with 0° being North).
Note: Heading readings will be affected by the presence of nearby magnets

Conclusion

In this tutorial you were shown how to interface the HMC5883L Magnetometer with the MSP430F5529 board. The example shows you how to adjust the magnetic declination for your location and how to use the magnetometer to display the heading direction.