Using HMC5883L and QMC5883L Magnetometer Modules

Using HMC5883L and QMC5883L Magnetometer Modules

The HMC5883L reads Earth's own magnetic field to work out which way is north — solving a completely different problem than GPS, which tells you where you are rather than which direction you're facing.

Many boards sold under the HMC5883L name contain QMC5883L, which uses a different address and library. Heading also needs offset/scale calibration and tilt compensation for non-level use.

How it's different from GPS

We've covered the NEO-6M GPS module before for determining position — latitude and longitude coordinates from satellite signals. The HMC5883L does something else entirely: it's a magnetometer, sensing the direction and strength of the ambient magnetic field to calculate a heading, the same basic principle as a traditional compass needle, with no satellite signal involved at all.

How it works

The sensor measures magnetic field strength independently along three axes (X, Y, Z), across a range of roughly ±1.3 to 8 Gauss. To turn those raw axis readings into a compass heading, the sketch calculates the arctangent of the Y and X values, then adds a declination angle — a correction specific to your geographic location, since magnetic north and true north aren't quite the same everywhere — before converting the result from radians into a 0–360° heading.

Components needed

An Arduino Uno, an HMC5883L digital compass module, and jumper wires.

Wiring it up

The module communicates over I2C: SDA to A4, SCL to A5, plus 5V and ground — the same wiring pattern as our other I2C sensors.

Programming it

Using the Wire.h library alongside an HMC5883L-specific library, the sketch reads the raw axis values, applies the heading calculation and declination correction, and prints the resulting direction to the Serial Monitor — 0° for north, 90° for east, 180° for south, and 270° for west.

Where this is used

Heading sensing like this fits robot and drone navigation, handheld direction-finding tools, and any project needing to know which way it's pointed rather than where it physically is — often paired with a GPS module like the NEO-6M for projects that need both position and orientation.

You can find HMC5883L digital compass modules at Juthour Tech.

Leave your comment

*
*
Only registered users can leave comments.