GY-271 Compass Magnetic Module ENG
GY-271 Compass Magnetic Module ENG
Have fun!
Table of Contents
Introduction....................................................................................................3
Specifications................................................................................................5
Operation modes...........................................................................................7
The pinout...................................................................................................10
How to set-up Arduino IDE..........................................................................11
How to set-up the Raspberry Pi and the Python.........................................15
Connecting the sensor with Uno..................................................................16
Library for Arduino IDE.............................................................................17
Sketch example.......................................................................................18
Connecting the sensor with Raspberry Pi...................................................23
Enabling I2C interface..............................................................................24
Libraries and tools for Python..................................................................25
Python script............................................................................................27
Calculating declination.............................................................................31
-2-
Introduction
-3-
This device is connected to a serial interface, but as a slave device under
the control of a master device, such as the microcontroller. Control of this
device is carried out via the I2C interface. This device supports standard
and fast speed modes, 100kHz and 400kHz, respectively. External pull-up
resistors are required to support all these modes.
-4-
Specifications
The device has an internal clock for internal digital logic functions and timing
management. This clock is not available for external usage.
Some periods of time are required for the device to be fully functional after
the device is powered on. The external power supply requires a time period
for voltage to ramp up, which is typically 50 milliseconds. However, it is not
controlled by the device. The Power On Reset (POR) time period includes
time to reset all the logics, load values in non-volatile memory to proper
registers, enter the stand-by mode and get ready for measurements (around
450 microseconds).
-5-
When the device is powered on, all registers are reset by POR, then the
device transits to the stand-by mode and waits for further commands.
The QMC5883L chip also has a built-in temperature sensor that can provide
temperature reading for other applications.
-6-
Operation modes
Continuous-Measurement Mode
If any of the six data registers is accessed, data protection starts. During the
data protection period, the data register cannot be updated until the last bits
of register 05H have been read.
-7-
During the measurement, it is possible to read the data register which keeps
the previously measured data. Therefore, no interrupt (DRDY bit) will be set
if data reading occurs in the middle of measurement.
If N-th data is skipped, the current data is flushed by the next coming data.
In this case, interrupt (DRDY bit) keeps high until data is read. The DOR bit is
set to “1”, which indicates a set of measurement data has been lost. It will
(DOR bit) turn to “0” once the register 06H is accessed in the next data read
operation.
Data lock is activated once any of the data registers are accessed. If the
register 05H (data unlock) is not accessed until the next measurements
end, current data blocks the next data to update the data register. In this
case, the DOR bit is also set to “1” until the register 06H is accessed in the
next data read.
An interrupt is generated on the DRDY pin each time that the magnetic field
is measured. The interrupt can be disabled by setting: 0AH[0] = 1.
-8-
Stand-by Mode
The stand-by mode is a default state of the chip, upon Power On Reset
(POR) or soft reset. Оnly a few function blocks are activated in this mode,
which keeps power consumption as low as possible. In this state, register
values are held ON by an ultra-low power. The I2C interface can be woken
up by reading register values that are held ON or by writing to any register.
There is no magnetometer measurement in the stand-by mode. Internal
clocking is also halted.
Measurement Example
» Check status register 06H[0]; ”1” means ready.
» Read data registers from 00H through 05H.
Standby Example
» Write the value 0x00 in register 09H.
-9-
The pinout
The GY-271 compass magnetic module has five pins. The pinout is shown
on the following image:
The power supply and logic pins work on voltages in a range from 3.3V up
to 5V.
The DRDY pin is used to indicate that measurement is finished. You can
use it as an interrupt, as a signal to start reading data from the sensor.
- 10 -
How to set-up Arduino IDE
For Windows users, double click on the downloaded .exe file and follow
the instructions in the installation window.
- 11 -
For Linux users, download a file with the extension .tar.xz, which has to
be extracted. When it is extracted, go to the extracted directory and open
the terminal in that directory. Two .sh scripts have to be executed, the first
called arduino-linux-setup.sh and the second called install.sh.
To run the first script in terminal, open terminal in the extracted directory,
and run the following command:
sh arduino-linux-setup.sh user_name
user_name - is the name of a superuser in the Linux operating system.
The password for the superuser has to be entered when the command is
started. Wait for a few minutes for the script to complete everything.
After installation of the first script, the second script, called install.sh,
has to be used. In the terminal (extracted directory), run the following
command: sh install.sh
After the installation of these scripts, go to the All Apps, where the
Arduino IDE is installed.
- 12 -
Almost all operating systems come with a text editor preinstalled (for
example, Windows comes with Notepad, Linux Ubuntu comes with
Gedit, Linux Raspbian comes with Leafpad, etc.). All of these text
editors are perfectly fine for the purpose of the eBook.
Next thing is to check if your PC can detect an Arduino board. Open freshly
installed Arduino IDE, and go to:
Tools > Board > {your board name here}
{your board name here} should be the Arduino/Genuino Uno, as it can
be seen on the following image:
- 13 -
If the Arduino IDE is used on Windows, port names are as follows:
- 14 -
How to set-up the Raspberry Pi and Python
For the Raspberry Pi, first the operating system has to be installed, then to
set-up everything so that it can be used in the Headless mode. The
Headless mode enables remote connection to the Raspberry Pi, without
the need for a PC screen Monitor, mouse or keyboard. The only things that
are used in this mode are the Raspberry Pi itself, power supply and internet
connection. All of this is explained in detail in the free eBook:
Raspberry Pi Quick Startup Guide
- 15 -
Connecting the sensor with Uno
Connect the GY-271 compass magnetic module with the Uno as shown on
the following connection diagram:
- 16 -
Library for Arduino IDE
Several sketch examples come with the library. The code from several
sketch examples is modified to create the sketch example in the next
chapter in order to show how to use all functions that come with the library.
- 17 -
Sketch example
#include <QMC5883LCompass.h>
QMC5883LCompass compass;
void setup() {
Serial.begin(9600);
compass.init();
}
void loop() {
compass.read(); // Read compass values
byte a = compass.getAzimuth();
// Return Azimuth reading
Serial.print("Azimuth: ");
Serial.println(a);
byte d = compass.getBearing(a);
// Output is a value from 0 - 15
// based on the direction of the bearing / azimuth
Serial.print("Direction: ");
Serial.println(d);
char compassLetters[3];
compass.getDirection(compassLetters, a);
Serial.print(compassLetters[0]);
Serial.print(compassLetters[1]);
Serial.println(compassLetters[2]);
delay(1000);
}
- 18 -
Upload the sketch to the Uno and open Serial Monitor (Tools > Serial
Monitor). The result should look like the output on the following image:
To get similar values, rotate the compass board around one of its axis.
- 19 -
The sketch starts with importing a library QMC5883LCompass.
At the beginning of the loop() function the data from the module is read,
with the following line of code: compass.read()
Next, the readings are converted to Azimuth values (what the Azimuth
values are, is not covered in this eBook) with the following line of code:
byte a = compass.getAzimuth();
After this, the data with Azimuth value is displayed in the Serial Monitor.
- 20 -
Then, the Azimuth values are converted to direction numbers and displayed
in the Serial Monitor.
Which can be seen on the compass circle, there are 16 different areas, like
on the following image:
- 21 -
The function called getBearing() has one argument and returns a value.
The argument is an integer number, which represents the Azimuth value.
The return value is also an integer number, in the range from 0 to 15, which
represents one of directions number from the compass circle image.
After this, the direction numbers are converted to direction letters with the
following lines of code:
char compassLetters[3];
compass.getDirection(compassLetters, a);
Where “a” is the Azimuth value. Here, a char array with three elements is
created. Then the function getDirection() is used to convert the
Azimuth value into the direction letter. The function has two arguments and
returns no value. The first argument is the char array, in which the function
store direction letters, and the second argument is the Azimuth value.
At the end of the loop() function the direction letters data is displayed and
the delay() function is used to create a pause of 250 milliseconds
between two loops of the loop() function.
- 22 -
Connecting the sensor with Raspberry Pi
- 23 -
Enabling the I2C interface
In order to use the sensor with Raspberry Pi, the I2C interface on the
Raspberry Pi has to be enabled. To do so, go to:
Application Menu > Preferences > Raspberry Pi Configuration
When a new window opens, find the Interfaces tab. Then enable I2C
radio button and click OK, like on the following image:
- 24 -
Libraries and tools for Python
Next, check the I2C address of the module, by running the following
command: i2c detect -y 1
The result should look like the output on the following image:
- 25 -
If the I2C interface of the Raspberry Pi is not enabled, when the previous
command is executed, the following error will be raised:
Or use the git to clone the repository, by running the following command in
the terminal:
git clone https://github.com/Slaveche90/gy271compass
- 26 -
Python script
sensor = GY271.compass(address=0x0d)
except KeyboardInterrupt:
print('\nScript end!')
- 27 -
Save the script by the name gy271.py in the same directory where you
saved the gy271compass script. To run the gy271.py script open terminal
in the directory where you saved the script and run the following command:
python3 gy271.py
The result should look like the output in the following image:
- 28 -
The script starts by importing external class library gy271compass and the
time library.
Next, the object called compass is created using the following line of code:
sensor = GY271.compass(address=0x0d)
where 0x0d is the I2C address of the module.
Then, the try-except block of code is created. In the try block of code
an indefinite loop (while True:) is created. To read the heading angle in
the indefinite loop, the following line of code is used:
angle = sensor.get_bearing()
and to read the temperature data, use the following line of code:
temp = sensor.read_temp()
The heading angle data is stored in the variable called angle and
temperature data is stored in the variable called temp. These two variables
are then used to display data in the terminal.
- 29 -
The module has several options that can be set at the sensor object
creation. You can pass the following arguments to the compass()
constructor:
address=0x0d - the I2C address of the module
mode=MODE_CONT - Continuous or stand-by mode:
MODE_CONT or MODE_STBY
odr=ODR_10Hz - Output Data Rate, with values:
ODR_10Hz,
ODR_50Hz,
ODR_100Hz or
ODR_200Hz
sens=SENS_2G - Sensitivity: SENS_2G or SENS_8G
osr=OSR_512 - OverSampling Rate, with values:
OSR_512,
OSR_256,
OSR_128 or
OSR_64
d=0.0 - Declination data (explained in the next chapter)
- 30 -
Calculating declination
- 31 -
Now is the time to learn and make the Projects on your own. You can do
that with the help of many example scripts and other tutorials, which can be
found on the internet.
If you are looking for the high quality products for Arduino and
Raspberry Pi, AZ-Delivery Vertriebs GmbH is the right company to get
them from. You will be provided with numerous application examples,
full installation guides, eBooks, libraries and assistance from our
technical experts.
https://az-delivery.de
Have Fun!
Impressum0
https://az-delivery.de/pages/about-us
- 32 -