hmc5883l arduino,arduino compass,magnetometer arduino, compass navigation
Table of Contents
Description:
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.
Amazon Links:
Arduino Nano USB-C Type (Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
About HMC5883L:
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.
Features:
- 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 Bus:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
/* 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 #include <Wire.h> // Reference the HMC5883L Compass Library #include <HMC5883L.h> // Store our compass as a variable. HMC5883L compass; // 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. void setup() { // Initialize the serial port. Serial.begin(9600); pinMode(ledpin, OUTPUT); 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(compass.GetErrorText(error)); 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. Serial.println(compass.GetErrorText(error)); } // Our main program loop. void 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. // delay(66); } // Output the data down the serial port. void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees) { Serial.print("Raw:\t"); Serial.print(raw.XAxis); Serial.print(" "); Serial.print(raw.YAxis); Serial.print(" "); Serial.print(raw.ZAxis); Serial.print(" \tScaled:\t"); Serial.print(scaled.XAxis); Serial.print(" "); Serial.print(scaled.YAxis); Serial.print(" "); Serial.print(scaled.ZAxis); Serial.print(" \tHeading:\t"); Serial.print(heading); Serial.print(" Radians \t"); Serial.print(headingDegrees); Serial.println(" Degrees \t"); //delay(1000); if((headingDegrees >=1)&(headingDegrees <= 45) ) digitalWrite(ledpin,HIGH); else digitalWrite(ledpin,LOW); } |
You should include the HMC5883L.h library,as all the versions I found give upper / lower case errors and function not defined !