This is only a preview of the November 2018 issue of Silicon Chip. You can view 41 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "Oh Christmas tree, oh Christmas tree...":
Items relevant to "USB digital and SPI interface board":
Items relevant to "Insomnia and Tinnitus killer":
Items relevant to "El cheapo modules, part 20: two tiny compass modules":
Articles in this series:
Items relevant to "GPS-synched, lab-quality frequency reference (Part 2)":
Purchase a printed copy of this issue for $10.00. |
Using Cheap Asian Electronic Modules Part 20: by Jim Rowe
Two tiny Electronic
C mpass modules
The Elecrow GY-271 and the GY-511 are two
low-cost electronic compass modules. Both
readily available modules incorporate a 3-axis
magnetometer, with the GY-511 also including
an accelerometer. You can use them with an
Arduino, Micromite or any other microcontroller
which supports I2C communication.
T
he GY-271 is the smaller of the
two modules, measuring only 14.5
x 13.3 x 3.5mm (without the 5-pin
header attached). It’s based on the
Honeywell HMC5883L 3-axis digital
compass (magnetometer) IC, which is
no longer being manufactured but is
nevertheless still available in significant quantities.
The GY-511 is nearly double the size,
measuring 21 x 14.5 x 3.5mm (without
the 8-pin header attached). This module is based on the STMicroelectronics
LSM303DLHC 3D accelerometer/3D
magnetometer IC, which is somewhat
more complex than the HMC5883L.
A functionally identical clone of
the GY-271 is available from Altronics
(Cat Z6391) and Jaycar (Cat XC4496).
This has a six-pin header rather than
five, with the extra pin being a 3.3V
output from the on-board regulator
which you can use to power external
circuitry.
Since that connection is purely for
convenience, the description of the
GY-271 here applies to those modules too.
The GY-511 is also available from
Altronics, Cat Z6391A. Interestingly,
while the GY-511 is a bit more expensive overseas, Altronics charge exactly
the same for it as they do the GY-271
clone. Given the extra functionality,
that seems like the one to get.
72
Silicon Chip
The HMC5883L
The HMC5883L IC used in the GY271 module comes in a tiny 3 x 3 x
0.9mm 16-pin LCC (leadless chip carrier) surface-mount package. A simplified version of its internal block diagram is shown in Fig.1.
There are actually two chips inside
the HMC5883L: the sensing block on
the far left (pink shading) which does
the actual magnetic field sensing and
the measurement and control circuitry which forms the rest of the device.
Presumably, this is necessary because
they use different manufacturing processes.
The sensing block chip has three
magneto-resistive sensor bridges, orientated at right angles to each other.
They are labelled X, Y and Z. This allows it to sense both the direction and
magnitude of very low-intensity magnetic fields, like the one generated by
the Earth.
The sensor bridge outputs are connected to the inputs of an analog multiplexer (MUX) on the measurement
chip, which allows the control circuitry to select them in turn.
The selected bridge output is then
passed via a charge amplifier to the input of a 12-bit ADC (analog-to-digital
converter), which delivers its corresponding digital value to the control
logic section.
When all three measurements have
been made in this way, the control logic makes them available to an external
Fig.1: block diagram for the Honeywell HMC5883L eCompass IC, showing
the magnetic sensing bridges at upper left, which are connected to the charge
amplifier by a multiplexer.
Australia’s electronics magazine
siliconchip.com.au
Fig.2: circuit diagram of the GY-271, which is based around the HMC5883L IC. It has few other components; primarily,
voltage regulator REG1, level shifting Mosfets Q1 & Q2 and some bypass/filtering capacitors and pull-up resistors.
MCU via the standard I2C interface at
far right.
The other two circuit blocks labelled
“Offset Strap Driver” and “Set/Reset
Strap Driver” are used by the chip’s
control logic to perform degaussing,
testing and offset compensation for
the magneto-resistive sensor bridges.
As a result, the device can offer magnetic field resolution down to 200nT
(nanoTesla) or 2mG (milliGauss).
This makes it very suitable for measurements of the Earth’s magnetic field,
which tends to vary between about
22µT and 64µT (microTesla) over the
planet’s surface. And it can make these
measurements at a rate of up to 160Hz.
The supply current of the HMC5883L is very low, varying from
around 2µA in idle mode up to about
100µA when it’s making measurements. This makes it suitable for portable and hand-held applications like
smartphones and tablets.
The circuit diagram of the complete
GY-271 eCompass module is shown in
Fig.2 with the HMC5883L forming the
heart of this module. The only other
active devices are REG1, a 3.3V LDO
(low-dropout) regulator and N-channel Mosfets Q1 and Q2 which perform
level translation on the SCL and SDA
lines of the module’s I2C interface.
This means that the HMC5883L can
operate from a 3.3V supply rail but still
siliconchip.com.au
exchange data with an external micro
running from a 5V supply. In fact, the
I2C pull-up resistors (2.2kW) for CON1
connect to the incoming 5V supply.
The 220nF capacitor between pins
8 and 12 of IC1 determines its Set/Reset timing, while the 4.7µF capacitor
from pin 10 to ground acts as a reservoir for the charge amplifier ahead of
the ADC. Pin 15 provides a data ready
signal at the end of each measurement
cycle. This is brought out to pin 5 of
Australia’s electronics magazine
CON1, for optional use by the MCU to
which it’s connected.
We’ll describe how to use this module a bit later. First, let’s take a look
at the IC used in its larger sibling, the
LCM303DLHC.
The LSM303DLHC IC
Fig.3 shows a simplified block diagram of the LSM303DLHC eCompass
IC, and as you can see it is a little more
complex than the HMC5883L (Fig.1).
November 2018 73
Fig.3: the STMicro LSM303DLHC IC is similar to the HMC5883L shown in
Fig.1 but also incorporates a three-axis MEMS accelerometer along with an
additional multiplexer and amplifier. This allows the compass’ orientation to be
determined, for more accurate results.
Most of the additional complexity is
because this device incorporates a
3-axis linear accelerometer as well as
the 3-axis magnetometer.
The magnetometer’s sensing system
is similar to that in the HMC5883L,
with three magneto-resistive sensor
bridges orientated at right angles to
each other.
The linear accelerometer sensors are
made from very thin micromachined
strips, again orientated in mutually orthogonal directions, which cause capacitance changes when they deflect
in response to any acceleration forces.
They can also be used to sense
gravitational fields, which allows the
Earth’s gravitational field to be used for
calibrating the magnetometer.
Both sensor arrays are shown in the
pink shaded area of Fig.3 and they
each have their own multiplexer and
charge amplifier feeding the in-built
ADC. The only other real differences
from the HMC5883L are the additional
blocks shown at the bottom of Fig.3.
Either of the two sensing arrays can
be enabled or disabled by the control
logic, in response to commands sent
from the host MCU via the I2C interface.
Since the accelerometer array is not
really needed when you want to use
the device as a simple eCompass, it can
therefore be disabled. So when used
as an eCompass, the LSM303DLHC is
quite similar to the HMC5883L.
The LSM303DLHC draws about
110µA in normal measurement mode
and around 1µA in idle/sleep mode.
It has seven magnetic measurement
ranges varying from ±1.3 gauss to
±8.1 gauss (1G = 100µT), a maximum
magnetic resolution of 2mG (0.2µT or
200nT) and the ability to make measurements at eight selectable rates, from
0.75Hz to 220Hz.
So once again, the LSM303DLHC IC
forms the heart of the GY-511 eCompass module, as shown in Fig.4. If you
compare this with Fig.2, you’ll see that
they’re almost identical.
The only differences are the chip
for IC1 and an 8-pin header for CON1
instead of a 5-pin header. When using
the GY-511 module as an eCompass,
the additional pins can be ignored.
Fig.4: the circuit for the GY-511 eCompass module, which is virtually identical to the GY-271 shown in Fig.2, except that a
different IC is used and it has two extra interrupt signal connections which are wired to header CON1This has more pins
(eight, compared to five), along with a 3.3V output from REG1.
74
Silicon Chip
Australia’s electronics magazine
siliconchip.com.au
The GY-511 module shown enlarged above and to its right is the example serial output from the Compass.ino sketch
(James Sleeman's Arduino library) using the GY-271.
Connecting to a micro
As both modules use an I2C serial interface to exchange data with an MCU,
connecting them to an Arduino or a
Micromite is straightforward.
Fig.5 shows how a GY-271 is connected to an Arduino, while Fig.6
shows how it’s connected to a Micromite. Similarly, Fig.7 shows how
a GY-511 module is connected to an
Arduino, while Fig.8 shows how it’s
connected to a Micromite.
Things are not quite so straightforward when it comes to the software.
You would expect that there are already Arduino libraries suitable for
interfacing with these modules, and
indeed they are available. But when I
tried them out, I found most of them
to be too complicated, poorly written
and/or buggy.
The only library I found that was
both easy to use and worked well was
one called HMC5883L_Simple, written by James Sleeman in New Zealand.
This library can be downloaded from
Mr Sleeman’s website, at: http://sparks.
gogo.co.nz/HMC5883L_Simple.zip
The archive file includes a simple
example sketch (Compass.ino), which
I can recommend. A sample grab of the
Arduino IDE’s Serial Monitor output
when running this sketch is shown
above, with the GY-271 module’s Y
axis pointing to magnetic north. The
heading figures are all within the range
of 0.43-2.14°N.
Since the two modules are similar,
we adapted this library to work with
the GY-511 module without any modifications, although the magnitude of
the results may be wrong (this isn't
terribly important when using it as a
compass).
siliconchip.com.au
When it comes to using either of
these modules with a Micromite, I
couldn’t find any existing programs
or libraries.
So I had to analyse the functions
embedded in Arduino libraries (especially Mr Sleeman’s), and then write
MMBasic programs to duplicate the
same functions on the Micromite.
The programs I wrote are called
“GY271 eCompass.bas” and “GY511
eCompass.bas” and both are available
in a zip file from the Silicon Chip website. Note that all of these programs
(Arduino and Micromite) treat the Yaxis of the module as the “needle” of
the eCompass.
These programs do the bare mini-
mum to allow the modules to be used
as electronic compasses. They initialise the main IC, then make measurements twice a second, process the X
and Y data readings to arrive at the
magnetic heading, then convert this
to a true heading by subtracting the
local declination figure.
Both heading figures are then displayed on the Micromite’s LCD screen,
as you can see from the screenshot
below. Note that the current declination is also shown at the bottom of the
screen, as a reminder.
The declination adjustment is necessary because the Earth’s magnetic
North Pole is not at the actual North
Pole; in fact, they are getting further
Our example MMBasic program shows both the magnetic heading (relative to
north magnetic pole) and the true heading (relative to the north celestial pole).
Australia’s electronics magazine
November 2018 75
Fig.5: connecting the GY-271 eCompass module to an Arduino is easy as it only
requires four connections: two for 5V power and two for I2C communications (SDA
[data] and SCL [clock]). The DRDY signal is not mandatory.
▼
Fig.6: connecting the GY-271 to a ►
Micromite (in this case, the LCD
BackPack) is just as easy; the
connections are the same as in
Fig.5 but the Micromite uses pins
17 and 18 for I2C communications.
Fig.7: connecting the GY-511 module to an
Arduino involves similar wiring compared to
the GY-271. As with the DRDY signal, the two
interrupt signals are not absolutely necessary
and so can be left unconnected. ▼
◄ Fig.8: as with the Arduino circuit in
Fig.7, only four pins of the GY-511 need
to be connected to the Micromite (two
are for the power supply and two are
for I2C serial communications).
76
Silicon Chip
Australia’s electronics magazine
siliconchip.com.au
How a compass works
A compass is a portable device used to work out your heading. This is the direction in which you are travelling with respect to the Earth’s axis of rotation, or the
hypothetical meridian lines on the surface of the Earth between the true south and
North Poles. It does this by sensing the weak magnetic field which surrounds the
Earth, due to the magnetisation of the Earth’s metal core.
A traditional compass senses the Earth’s field by means of a small magnetised
iron needle which is able to rotate freely in the horizontal plane about its centre because it’s either floating on a small pool of liquid or mounted at its centre on a very
low friction needle bearing.
As a result, the needle can orientate itself to align with the horizontal component
of the Earth’s field, so the needle always tends to point towards north. A dial around
the circumference of the compass then allows the user to work out the direction of
any desired heading.
That’s the basic idea, anyway. But in practice, things are a little more complicated. That’s because while the compass needle aligns itself with the Earth’s magnetic
field passing from south to north, that field passes between the Earth’s magnetic
poles and these are different from the Earth’s true geographic poles (which correspond to its axis of rotation).
Not only that, but the magnetic field is not uniform with smooth meridian lines
passing between the South and North Magnetic Poles. In fact, the field lines weave
around quite a bit, with an orientation varying significantly according to latitude and
longitude and also according to time, as the field pattern changes from year to year.
So wherever you happen to be, although the needle of a compass nominally
points towards north, that doesn’t mean that it shows the direction of true north. To
work out the direction of true north, you need to know the angle between the horizontal component of the Earth’s magnetic field at that location and a meridian line from the true South Pole to the true north
pole at the same location.
This angle is called the Magnetic Declination and you can find the declination at any particular point on the Earth’s surface
by referring to either maps or websites like www.magnetic-declination.com
The declination varies quite significantly over Australia and New Zealand. For example, in Sydney, it’s around 12.6°E while
in Perth it’s around 1.8°W. The current declinations for a number of locations in Oceania are shown in Table 1.
There’s another aspect of the Earth’s magnetic field that can affect compass operation. That’s the fact that the magnetic
field at any particular location is not aligned parallel to the Earth’s surface (ie, in the horizontal plane) but in many places is at
a significant angle.
This is called the Magnetic Inclination, and broadly speaking (when facing north) it points down into the ground in the Northern Hemisphere and upwards away from the ground in the Southern Hemisphere. This doesn’t have a major effect on compass
operation but it sometimes does need to be taken into account, especially with traditional compasses.
Table 1 also shows the inclination of the Earth’s magnetic field for each location. All the inclinations listed are orientated upwards (because all locations are in the Southern Hemisphere) but they vary with latitude. The locations that are furthest south
have a noticeably higher inclination than those nearer the Equator.
apart each year so you may need to update this value occasionally to maintain accuracy. See the panel above for
more detail on the differences between
magnetic north and true north.
Both programs are written to include
the magnetic declination of Sydney
(12.583°E, as shown in the previous
screenshot). If you’re at a different location, you need to modify the source
code to include the correct declination
value for your location, near the start
of the program:
DIM AS FLOAT Declin! = 12.583
Like Mr Sleeman’s Arduino library
and example sketch, my Micromite
programs make no allowance for the
local inclination (tilt) of the Earth’s
magnetic field.
siliconchip.com.au
In this respect, they are the same as
a traditional compass – both programs
assume that the module’s PCB (and thus
its magnetometer chip) is being held in
the horizontal plane or close to it.
It possible to take the magnetic inclination into account when working
out the absolutely true heading of an
eCompass but you need to combine
the data from the magnetometer with
that an accelerometer or gravity field
detector like that in the LSM303DLHC
chip. So you could not do this with the
GY-271 module unless you also had a
separate accelerometer.
This also requires quite a bit of number crunching to combine the data
from the two sensors. Which raises
the question of whether it would be
worth the effort. Ignoring the inclinaAustralia’s electronics magazine
tion seems to deliver a heading accuracy that is at least as good as a traditional compass and probably better.
I think that the only applications
where it would be necessary to achieve
the highest possible heading accuracy would be for things like aircraft or
ship navigation, or missile guidance
systems. But those are a bit out of my
league.
Handy links
HMC5883L datasheet:
siliconchip.com.au/link/aakz
LSM303DLHC datasheet:
siliconchip.com.au/link/aal0
Magnetic declination:
siliconchip.com.au/link/aal1
Geomagnetic declination:
siliconchip.com.au/link/aal2
SC
November 2018 77
|