Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Matha Goram
Published © GPL3+

What Is the Heading?

This note introduces the use of magnetometers to establish headings. Extensions to the solution for navigation purposes is food for thought.

BeginnerProtip30 minutes4,138
What Is the Heading?

Things used in this project

Hardware components

Elegoo Arduino UNO Rev 3
×1
Seeed Studio GY-271 QMC5883L Electronic Compass
×1
Elegoo DuPont connection wires
×4
Elegoo Breadb
×1
Baseplate
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Basic assembly

Logical connection diagram

Schematics

Digital compass test schematic

Pin connections between Arduino UNO and GY-271 that servers as the PCB interface for the QMC5883L digital compass module.

Code

QMC5883L-14.ino

C/C++
Simple test for GY-271 QMC5883L Electronic Compass
#include <Wire.h>
#include "QMC5883.h"

const float fullCircle = 360.0;
const float halfCircle = fullCircle / 2.0;
const float rad2deg = 180.0 / PI;
const float magdec = 3.0833; // Plano, TX
QMC5883 qmc;

void setup() {
  Wire.begin();
  Serial.begin(115200);
  qmc.init(); // qmc.setMode(Mode_Continuous, ODR_200Hz, RNG_2G, OSR_256);
}

void loop() {
  int xRoll, yPitch, zYaw, heading;
  qmc.read(&xRoll, &yPitch, &zYaw);

  heading = atan2((double)yPitch, (double)xRoll) * rad2deg;
  heading += magdec;
  
  if (heading > fullCircle)
  {
    heading -= fullCircle;
  }
  if (heading < 0.0)
  {
    heading += fullCircle;
  }
  

  //Serial.print("Heading=");
  Serial.println(heading);
  //Serial.println("\u00B0");
  delay(100);
}

Credits

Matha Goram
27 projects • 22 followers
Working with discrete electronic components for a very long time but still suffering from the occasional dry soldering results.
Thanks to DFRobot and Core.

Comments