The electronic compass is designed to detect the earth’s magnetic field and provide magnetic heading data to a microcontroller like Arduino. The one we’ll be using today is the GY-271 module, based on the HMC5883L sensor.
Equipped with a Hall-effect sensor, the GY-271 offers reasonable accuracy for measuring magnetic orientation. It incorporates a three-axis system that detects variations in the magnetic field on the X, Y and Z axes. This data can be used to determine the direction in which the module is oriented in relation to magnetic north.
Here’s the circuit diagram for connecting the GY-271. It uses the i2C of the Arduino board:
To download the program for the GY-271 compass you’ll need to install the adafruit library for the HMC5883, which is the chip that controls our compass: