Introduction: How to Use GY511 Module With Arduino [Make a Digital Compass]

About: Passionate about electronics parts and tutorials. We're your go-to source for high-quality components and expert guidance. Join us on our journey of exploration and innovation in the world of electronics.

Overview

In some electronics projects, we need to know the geographic location at any moment and do a specific operation accordingly. In this tutorial, you’ll learn how to use the LSM303DLHC GY-511 compass module with Arduino to make a digital compass. First, you’ll learn about this module and how it works, and then you’ll see how to interface the LSM303DLHC GY-511 module with Arduino.

What You Will Learn

  • What compass module is?
  • Compass module and Arduino interface.
  • Make a digital compass with the GY-511 module and Arduino.

Step 1: General Information About Compass Module

GY-511 module includes a 3-axis accelerometer and a 3-axis magnetometer. This sensor can measure the linear acceleration at full scales of ± 2 g / ± 4 g / ± 8 g / ± 16 g and magnetic fields at full scales of ± 1.3 / ± 1.9 / ± 2.5 / ± 4.0 / ± 4.7 / ± 5.6 / ± 8.1 Gauss.

When this module is placed in a magnetic field, according to the Lorentz law an excitation current induces in its microscopic coil. The compass module converts this current to the differential voltage for each coordinate direction. Using these voltages, you can calculate the magnetic field in each direction and obtain the geographic position.

Tip

QMC5883L is another commonly used compass module. This module, which has a similar structure and application as the LMS303 module, is slightly different in performance. So if you are doing the projects, be careful about your module type. If your module is QMC5882L, use the appropriate library and codes that are also included in the tutorial.

Step 2: Required Components

Step 3: Interfacing GY-511 Compass Module With Arduino

GY-511 compass module has 8 pins, but you need only 4 of them to interface with Arduino. This module communicates with Arduino using I2C protocol, so connect the SDA (I2C output) and SCK (I2C clock input) pins of the module to the I2C pins on the Arduino board.

Note
As you can see, we have used the GY-511 module in this project. But you can use this instruction for setting up other LMS303 compass modules.

Step 4: GY-511 Compass Module Calibration

In order to navigate, you first need to calibrate the module, which means to set the measuring range from 0 to 360 degrees. To do this, connect the module to Arduino as shown below and upload the following code on your board. After executing the code, you can see the minimum and maximum values of the measuring range for X, Y and Z axis in the serial monitor window. You will need these numbers in the next part, so write them down.

Step 5: Circuit

Step 6: Code

In this code, you need the Wire.h library for I2C communication, and LMS303.h library for the compass module. You can download these libraries from the following links.

LMS303.h Library

Wire.h Library

Note
If you are using QMC5883, you’ll need the following library:

MechaQMC5883L.h

Here, we explain the code for LMS303, but you can download the codes for the QMC module as well.

Let’s see some of the new functions:

compass.enableDefault();

Module initialization

compass.read();

Reading the output values of compass module

running_min.z = min(running_min.z, compass.m.z);
running_max.x = max(running_max.x, compass.m.x);

Determining the minimum and maximum values of measurement range by comparing the measured values.

Step 7: Making a Digital Compass

After calibrating the module, we are going to build a compass by connecting a servo motor to the module. So that the servo indicator, always shows us the north direction, like the red arrow on the compass. To do this, first the compass module calculates the geographic direction first and send it to Arduino and Then, by applying an appropriate coefficient, you’ll calculate the angle that the servo motor should rotate so that its indicator points to the magnetic north. Eventually, we apply that angle to the servo motor.

Step 8: Circuit

Step 9: Code

For this part you also need the Servo.h library, that is installed on your Arduino software by default.

Let’s see some of the new functions:

<p> Servo Servo1;</p>

Module initialization

compass.read();

Introducing the servo motor object

<p>Servo1.attach(servoPin); <br> compass.init();
 compass.enableDefault();</p>

Initialization the compass module and servo motor

The Servo1.attach() argument is the number of the pin connected to the servo motor.

<p>compass.m_min = (LSM303::vector){-32767, -32767, -32767};<br>  compass.m_max = (LSM303::vector){+32767, +32767, +32767};</p>

Using these lines you define the minimum and maximum values for measuring the range obtained in the previous part.

<p>float heading =compass.heading((LSM303::vector){0, 0, 1});</p>

The heading() function returns the angle between the coordinate axis and a fixed axis. You can define the fixed axis with a vector in the function argument. For example, here, by defining the (LSM303 :: vector ) {0, 0, 1}, the Z axis is considered as a constant axis.

<p>Servo1.write(heading);</p>

The Servo1.write() function applies the read value by the compass module to the servo motor.

Note
Note that the servo motor may have a magnetic field, so it is better to place the servo motor at a suitable distance from the compass module, that it does not cause the compass module to deviate.

Step 10: What’s Next?

  • Add the ESP8266 module to the system to report your position to your mobile phone at any moment.

If you find this tutorial helpful and interesting please like us on facebook.

Step 11: