How to use the GY-271 module with Arduino to create a digital compass

  • The GY-271 measures the magnetic field in three axes and communicates its data via I2C.
  • Calculating the orientation relative to north requires correction for magnetic declination.
  • The GY-271 is used in robotics, drones and autonomous navigation systems.

GY-271 Arduino module

On this occasion, we are going to talk about one of the sensors that is most used in Arduino projects related to navigation and orientation: the GY-271. This module incorporates the sensor HMC5883L, which is a three-axis magnetometer capable of detecting magnetic fields and therefore giving us orientation relative to magnetic north.

If you are thinking of integrating it into an Arduino project, throughout this article we will explain all its details: from its features, how to connect and program it, to examples of use and tips to obtain the best precision. So keep reading and discover how to create a digital compass with Arduino!

What is the GY-271 sensor?

The sensor GY-271 It is a module that integrates the magnetometer HMC5883L. This chip is capable of measuring the magnetic field in the three axes (X, Y and Z) and, with this data, it is possible to know the orientation with respect to the Earth's magnetic field. This sensor has a high precision and is widely used in projects navigation in robots or autonomous vehicles.

Communication between this module and the Arduino is done through the I2C bus, making it much easier to obtain the measured data. The HMC5883L has a measurement range of ±0.88 Gauss to ±8.1 Gauss, depending on the configuration, allowing it to cover a wide range of applications.

Connections and assembly with Arduino

Connecting the GY-271 to your Arduino is really simple, you just need a few wires and follow the basic diagram:

  • Connect the pin GND of the module with the GND pin of the Arduino
  • The PIN VCC of the GY-271 must be connected to the 5V of the Arduino
  • Connect the pin SDA from the GY-271 to the A4 pin of the Arduino (or SCL on some models like the Mega)
  • The PIN SCL must go to pin A5 of the Arduino (or SDA in some cases)

Once you have everything connected, the module is ready to go. If your goal is to obtain magnetic field data and create a digital compass, you already have the basics. However, keep in mind that the environment where you place the sensor must be free of magnetic interference, since nearby metals or electronic devices may alter the measurements.

Arduino code examples

Here is a basic example of how to read the X, Y and Z values ​​of the magnetic field using the appropriate library. This library will facilitate I2C communication and sensor readings:

#include <Wire.h>
#include <HMC5883L.h>

HMC5883L compass;
int16_t mx, my, mz;

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

void loop() {
  compass.getHeading(&mx, &my, &mz);
  Serial.print("X: ");
  Serial.print(mx);
  Serial.print(" Y: ");
  Serial.print(my);
  Serial.print(" Z: ");
  Serial.println(mz);
  delay(500);
}

This code is ideal for obtaining the magnetic field components in the three axes. Once you have these values, you can calculate the orientation of the sensor relative to magnetic north using the function atan2, which will allow us to convert the X and Y axes into an angle.

Calculating the angle relative to the north

Now that you have the magnetic field readings, the next step is to calculate the orientation relative to magnetic north. To do this, you can use the following formula:

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

This calculation will give us an angle in degrees that represents the direction towards magnetic north. However, you must take into account the magnetic declination, which is the difference between magnetic north and geographic north. Depending on your geographic location, this value may vary, and it is important to correct it to obtain a more accurate compass.

Additional settings and operating modes

The GY-271 offers several settings that allow you to adjust its operation to your needs. For example, you can choose between two operating modes:

  • Continuous way: The magnetometer continuously takes measurements and updates the corresponding records (X, Y, Z).
  • Single measurement mode: The sensor only takes a reading when the Arduino requests it, which can be useful if you want to save power.

Additionally, you can adjust the sensitivity of the sensor, modifying the measuring range. Available ranges range from ±0.88 Ga to ±8.1 Ga, allowing you to adapt the sensor to different environments and working conditions.

Remember that to change the measurement range, you must use the function setGain from the library, which allows you to set the sensor gain based on the magnetic range you want to measure.

Applications of GY-271

The GY-271 sensor has numerous applications in the field of robotics and navigation. Being a relatively cheap and easy-to-implement device, it is used in projects such as:

  • Autonomous Rovers: Allows robots to know which direction they are facing.
  • Quadcopters: Helps maintain the drone's orientation relative to north in flight.
  • navigation systems: Any vehicle that needs to know its position and orientation can benefit from this module.

One of the most curious details is that, although the GY-271 has great precision under controlled conditions, its measurement can be affected by interferences, such as the presence of metals or electromagnetic fields nearby. This can be corrected by techniques of calibration combined with accelerometers or gyroscopes (IMU), which is typical in more advanced navigation systems.

Combining this sensor with accelerometers, for example, allows for the construction of more precise devices that are resistant to magnetic noise, which opens up a range of possibilities for use in projects with Arduino and other microcontrollers…


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.