Find Your Way With a GY-271 Compass Module and Arduino
In modern DIY projects, it’s pretty common to find advanced sensors such as accelerometers and magnetometers being interfaced with microcontrollers. While you may have used these sensors on your phone, it’s hard to imagine how these modules work, but it’s surprisingly simple.
The GY-271 compass magnetometer sensor enables orientation sensing for various applications. Here we’ll explore the process of interfacing the GY-271 sensor with an Arduino microcontroller, how to calibrate it and look at the problems you may face while doing so.

What Is the GY-271 Compass Module?
The GY-271 compass module is a highly sensitive magnetometer that can measure magnetic fields in three dimensions. It is used to detect the Earth’s magnetic field and determine the direction or orientation of devices in applications such as navigation systems, robotics, and drones. Think of it like a very precise digital compass. This sensor is equipped with a compact and sturdy design that makes it easy to install and use in a wide range of projects.
How Does the GY-271 Compass Module Work?
The GY-271 HCM5883L compass module contains a 3-axis magnetometer sensor, which is based on anisotropic magneto-resistive (AMR) technology. The sensor measures the strength and direction of the Earth’s magnetic field by detecting the changes in the resistance of its internal AMR elements.
When the sensor is exposed to a magnetic field, the AMR elements' resistance changes, generating a voltage that is proportional to the magnetic field’s strength and direction. The sensor’s digital signal processor then converts this voltage into a digital signal, which can be read by a microcontroller. By measuring the voltage in all three axes, the sensor can provide highly accurate readings of the magnetic field’s strength and direction in three dimensions.
Connecting the GY-271 to an Arduino Uno
Connecting this module to the Arduino is pretty straightforward. We’ve also covered othereasy-to-use Arduino modulesthat can help scale your project in the future. The GY-271 module has a total of five pins, includingVCC,GND, and three signal pins (SDA,SCL, andDRDY).
You will need the following components:

First, connect theVCCandGNDpins of the GY-271 module to the5VandGNDpins of the Arduino Uno, respectively.
Next, connect theSDAandSCLpins of the module to theA4andA5analog pins of the Arduino Uno, respectively.

Installing the Necessary Libraries
Before it’s possible to start working with the GY-271 compass module and Arduino, you need to install the necessary libraries to communicate with the sensor.
The first library you need to include in your sketch is the Wire library, which is used for I2C communication between the Arduino and the sensor. The Wire library comes pre-installed with the Arduino IDE, so you don’t need to download it separately. To install the second one, theHMC5883Llibrary, you will need to follow these steps:

Open the Arduino IDE and go toSketch>Include Library>Manage Libraries.
In the Library Manager, search forHMC5883Lin the search bar.
Select theHMC5883Llibrary specifically from Adafruit. Once the library is installed, you can include it in your Arduino sketch by going toSketch>Include Library>HMC5883L.
Understanding the Code
You need to write a program that initializes the sensor, reads its data, and calculates the Earth’s magnetic field’s strength and direction. The full code can be downloaded from our officialGitHub repository.
At the start of the code, you need to include the necessary libraries you just downloaded.
Don’t forget to assign a unique ID to the sensor. Use this line to do that:
In the setup code, begin the serial monitor and check if the sensor is responding. You can print messages to the monitor to update the status of the sensor:
In the loop, first, get a new sensor event:
Then display the readings of the sensor to the serial monitor. Don’t forget to set the baud rate to9600.
Hold the module so thatZis pointing “up” and measure the heading withXandY. By doing this, you can then calculate the heading when the magnetometer is level. This line of code helps with that is:
Once you have yourheading, you must then add yourdeclinationAngle, which is the error of the magnetic field in your location.
You can conveniently find the declination angle of your local area on theMagnetic Declination website. If for some reason you can’t find your declination angle, comment out the two lines from your code.
Commenting out the two lines of code may result in faulty sensor readings. Try your best to calibrate the sensor before use.
Finally, print the heading to the serial monitor and add a delay to wait for half a second before reading again.
From here, you can customize the code to suit your project’s needs and add additional features such as data logging.
Should you prefer to use the Adafruit library, we recommend sourcing it fromAdafruit’s GitHub repository.
Reading and Interpreting Compass Data
The sensor data displayed in the serial monitor shows the magnetic field vector values measured by the magnetometer in microteslas (μT) for the X, Y, and Z axes, respectively. These values can be used to determine the orientation and magnitude of the magnetic field.
The heading value displayed in degrees is also important and represents the direction of the magnetic north pole relative to the sensor’s position. This reading can be used in projects where the direction of a moving object needs to be determined, such as in robotics, navigation, and geolocation systems.
Sensor Calibration
If you have already installed a different library for the HMC5883L module, you’ll need to uninstall or delete it. Otherwise, you may get compile errors or see warnings in your serial monitor during calibration. We recommend usingJarzebski’s libraryavailable on GitHub since it has the complete calibration resources needed for this section.
First, download the zip file from the repository by clicking onCode>Download ZIP.
Next, open the Arduino IDE and click onSketch>Include Library>Add Library.
Select theArduino-HMC5883zip file that you just downloaded, and the Arduino IDE will then install the library automatically for you.
Run the calibration sketch available in the repository while moving the sensor around on a flat surface upon upload. The values obtained from the serial monitor are your sensor’s offset values, which you can use to correct your readings while using the compass during your projects.
Problems You May Run Into
There are several errors that someone may run into when interfacing the compass module with Arduino. Here are a few examples and their workarounds:
Compilation errors: These can occur if the necessary libraries are not included or if there are syntax errors in the code. To fix this, make sure you have installed the correct libraries and check your code for any syntax errors. As we’ve seen many times, you may have accidentally installed multiple HCM5883L libraries.
Communication errors: If you are unable to communicate with the compass module, make sure you have connected it to the correct pins on your Arduino and that your wiring is correct. You may also need to adjust the I2C address of the module in your code if it is different from the default—which is 0x1E.
Magnetic interference: If the compass readings are unstable or fluctuating, it may be due to magnetic interference from nearby objects. Move the module away from any magnets or other sources of magnetic interference to get more stable readings.
Rev Up Your Projects With the GY-271 Compass Module
Being able to use the GY-271 HCM5883L compass module opens up a door to building more complex DIY projects using Arduino such as GPS-guided rovers and DIY drones. The possibilities are endless, especially when you add other sensors.
To make the most of your Arduino, you’ll want to hook it up to a variety of sensors. Here’s how they work.
I plugged random USB devices into my phone and was pleasantly surprised by how many actually worked.
Your iPhone forgets what you copy, but this shortcut makes it remember everything.
You don’t need to fork out for expensive hardware to run an AI on your PC.
This small feature makes a massive difference.
OneDrive is one of the best, but it has a catch.