Complete guide to using the HMC5883L with Arduino

  • Connecting the HMC5883L to Arduino using I2C communication.
  • Code to read the X, Y, Z axes of the magnetic field.
  • Create a digital compass by adjusting the magnetic declination.

hmc5883l arduino-3 code

The HMC5883L magnetometer is a sensor widely used in Arduino projects for its ability to measure magnetic fields in three axes. This makes it an excellent choice for creating digital compasses, navigation systems for drones and other autonomous vehicles. In addition to being affordable, its integration is simple thanks to its I2C interface, making it ideal for beginners.

In this article we will go into the main features of the HMC5883L, its connection with various Arduino boards and practical examples for its use. At the end, you will be able to implement your own digital compass project with Arduino, taking full advantage of the potential of this powerful sensor.

What is the HMC5883L and how does it work?

The HMC5883L is a three-axis magnetometer which measures the components of the Earth's magnetic field in the X, Y and Z axes. This not only allows the presence of magnetic fields to be detected, but also allows the orientation to be determined by calculations based on the measurements obtained. These characteristics make it the ideal sensor for making orientation systems, such as a digital compass.

The sensor is usually found in modules such as the GY-273 model, which also includes the necessary electronics to facilitate its direct connection to Arduino boards or other microcontrollers. Its power supply is flexible, and can be powered with either 3.3V or 5V, making it very versatile in terms of compatibility with different systems.

Arduino Connections

Connecting the HMC5883L to Arduino is really easy thanks to the I2C interface it uses. This communication only requires two pins: SDA and SCL, which must be connected to the corresponding pins on the Arduino board.

  • FOR Arduino Uno, Nano and Mini: SDA connects to pin A4 and SCL to A5.
  • If you are using an Arduino Mega or Due: SDA should be connected to pin 20 and SCL to pin 21.
  • In the case of the Arduino Leonardo: SDA goes to pin 2 and SCL to pin 3.

Let's not forget that the magnetometer works with a fixed I2C address of 0x1E, so it is not possible to change it. This address is unique for this type of sensor, which means that we cannot connect multiple HMC5883L devices on the same I2C bus.

Reading the Magnetic Field

The main method of operation of the HMC5883L is to read the magnetic field values ​​on the three axes. To get these values ​​on an Arduino, we use a library developed by Jeff Rowberg. You can find this library at your GitHub repository and download it for use in the Arduino IDE.

Once the library is installed, the sensor can be initialized and start reading magnetic field values. Below is a simple example that does just that:

#include "Wire.h"
#include "I2Cdev.h"
#include "HMC5883L.h"
HMC5883L magnetometro;
int16_t mx, my, mz;

void setup() {
Serial.begin(9600);
Wire.begin();
magnetometro.initialize();
}

void loop(){
magnetometro.getHeading(&mx, &my, &mz);
Serial.print("mx: "); Serial.print(mx);
Serial.print(" my: "); Serial.print(my);
Serial.print(" mz: "); Serial.println(mz);
delay(100);
}

In this code, the magnetometer is initialized and the values ​​in the three axes are read in a loop. Each time we execute the `loop()`, the values ​​of the magnetic field in X, Y and Z will be displayed on the serial monitor.

Setting the range and gain

The HMC5883L allows you to adjust the magnetic field measurement range between ±0.88 Gauss and ±8.1 Gauss with different gain levels. The default range is ±1.3 Gauss, and you can modify it to suit the needs of your project. This can be done using the following line within the code:

magnetometro.setGain(value);

Where value is an integer between 0 and 7, which will select the profit level. Below is a table with the corresponding values:

Valor Range Gain (LSB/Gauss)
0 ±0.88 Ga 1370
1 ±1.3 Ga 1090
2 ±1.9 Ga 820
3 ±2.5 Ga 660
4 ±4.0 Ga 440
5 ±4.7 Ga 390
6 ±5.6 Ga 330
7 ±8.1 Ga 230

Building a digital compass

One of the most common uses of the HMC5883L is the construction of a digital compass. To do this, we need to calculate the angle between the sensor and magnetic North using the X and Y axis readings. This is achieved using the formula:

float angulo = atan2(my, mx) * 180 / M_PI;

This value will give you the orientation angle relative to magnetic North. To adjust this angle to the Geographic north, you must take into account the magnetic declination of your location, which you can consult on different websites such as www.ign.es or www.ngdc.noaa.gov.

Correcting magnetic declination

Once you have the magnetic declination of your position, you just have to subtract it from the angle obtained. For example:

angulo -= declinacion;

When the angle is negative, you can add 360 degrees to always get a positive value adjusted between 0 and 360 degrees:

if (angulo < 0) angulo += 360;

This setting will provide you with a compass that will always point precisely to geographic North.

In practice, with this sensor and some libraries you can have a compass in a few minutes. However, keep in mind that the HMC5883L is a sensor sensitive to external interference, such as nearby metals or fields generated by high currents, which can alter the readings.


Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.