HMC5883l Arduino- This Tutorial is about the HMC5883L magnetometer 3 axis sensor. This sensor is most commonly used in robotics for navigation purposes. Using this sensor you can easily find the direction. This tutorial covers the circuit connections, programming, and testing.
In this tutorial, you will learn how to use the HMC5883L magnetometer 3-axis sensor with Arduino. In this tutorial you will also learn how to connect HMC5883L with Arduino, You will also learn how to use HMC588L to make a Compass. We will display the angle on the computer screen and will control an led when the compass is at a particular angle. Such a project can be easily modified in building an autonomous robot that can help a robot to turn at a particular angle. In this project, you will also learn to program. In this tutorial, we will also discuss the ic2 communication bus, wiring, and programming.
Other Tools and Components:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
The module includes a state-of-the-art, High-Resolution HMC118x series magneto-resistive sensor, Plus an ASIC containing amplification, Automatic degaussing strap Drivers, Offset cancellation, and a 12-bit ADC that enables 1 to 2-degree compass heading accuracy. The I2C serial bus allows for easy interface.
- The Honeywell HMC5883L is a surface mount, multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as low cost compassing and magnetometry.
- The 12-Bit ADC coupled with low noise AMR sensors
- Low voltage operations and low power consumption, support built-in self-test.
- built-in strap drive circuits, wide magnetic field range, I2C digital interface
- working voltage 3.3v to 5v.
I2C communication Bus has become very popular and now commonly used by thousands of electronic devices because of its easy implementation. By the easy implementation, I mean that such devices need only 2 wires. This way we can connect so many devices with for example Arduino using only two wires. Using the I2C communication bus we can connect many devices at the same time using only two wires as each device has its own unique address.
HMC5883L Arduino Connections Diagram:
Arduino GND -> HMC5883L GND
Arduino 3.3V -> HMC5883L VCC
Arduino A4 (SDA) -> HMC5883L SDA
Arduino A5 (SCL) -> HMC5883L SCL
HMC5883L Arduino Programming:
HMC5883L_Example.pde - Example sketch for integration with an HMC5883L triple axis magnetomerwe.
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
// Reference the I2C Library
// Reference the HMC5883L Compass Library
// Store our compass as a variable.
// Record any errors that may occur in the compass.
int error = 0;
int ledpin = 13;
// Out setup routine, here we will configure the microcontroller and compass.
// Initialize the serial port.
Serial.println("Starting the I2C interface.");
Wire.begin(); // Start the I2C interface.
Serial.println("Constructing new HMC5883L");
compass = HMC5883L(); // Construct a new HMC5883 compass.
Serial.println("Setting scale to +/- 1.3 Ga");
error = compass.SetScale(1.3); // Set the scale of the compass.
if(error != 0) // If there is an error, print it out.
Serial.println("Setting measurement mode to continous.");
error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
if(error != 0) // If there is an error, print it out.
// Our main program loop.
// Retrive the raw values from the compass (not scaled).
MagnetometerRaw raw = compass.ReadRawAxis();
// Retrived the scaled values from the compass (scaled to the configured scale).
MagnetometerScaled scaled = compass.ReadScaledAxis();
// Values are accessed like so:
int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(scaled.YAxis, scaled.XAxis);
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
// Mine is: 2� 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
float declinationAngle = 0.0457;
heading += declinationAngle;
// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;
// Check for wrap due to addition of declination.
if(heading > 2*PI)
heading -= 2*PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;
// Output the data via the serial port.
Output(raw, scaled, heading, headingDegrees);
// Normally we would delay the application by 66ms to allow the loop
// to run at 15Hz (default bandwidth for the HMC5883L).
// However since we have a long serial out (104ms at 9600) we will let
// it run at its natural speed.
// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
Serial.print(" Radians \t");
Serial.println(" Degrees \t");
if((headingDegrees >=1)&(headingDegrees <= 45) )