Close
0%
0%

Digital Compass using Arduino and HMC5883L Magneto

Human brain is built of complex layer of structures which helps us to be a dominant species on earth.

Similar projects worth following
Human brain is built of complex layer of structures which helps us to be a dominant species on earth. For example the entorhinal cortex in your brain can give you sense of direction helping you to navigate easily through places that you are not familiar with. But unlike us, Robots and unmanned Ariel vehicles need something to get this sense of direction so they could manoeuvre autonomously in new terrains and landscapes. Different robots use different types of sensors to accomplish this, but the commonly used one is a magnetometer, which could inform the robot in which geo-graphic direction it is currently facing at. This will not only help the robot to sense direction but also to take turns in a pre-defined direction and angel.

Human brain is built of complex layer of structures which helps us to be a dominant species on earth. For example the entorhinal cortex in your brain can give you sense of direction helping you to navigate easily through places that you are not familiar with. But unlike us, Robots and unmanned Ariel vehicles need something to get this sense of direction so they could manoeuvre autonomously in new terrains and landscapes. Different robots use different types of sensors to accomplish this, but the commonly used one is a magnetometer, which could inform the robot in which geo-graphic direction it is currently facing at. This will not only help the robot to sense direction but also to take turns in a pre-defined direction and angel.

Since the sensor could indicate the geo-graphic North, South, East and West, we humans could also use it at times when required. So in this article let us try to understand how Magnetometer sensor works and how to interface it with a microcontroller like Arduino. Here we will build a cool Digital Compass which will help us in finding the directions by glowing an LED pointing North Direction. This Digital Compass is neatly fabricated on PCB from PCBGOGO, so that I can carry it next time when I go out in the wild and wish that I would get lost just to use this thing for finding my way back home. Let’s get started.

What is a Magnetometer and How does it Work?

Before we dive into the circuit, let’s understand a bit about magnetometer and how they work. As the name suggests the term Magneto does not refer to that crazy mutant in marvel who could control metals by just playing piano in the air. Ohh! But I like that guy he is cool.

Magnetometer is actually a piece of equipment that could sense the magnetic poles of the earth and point the direction according to that. We all know that Earth is huge piece of spherical magnet with North Pole and South Pole. And there is magnetic field because of it. A Magnetometer senses this magnetic field and based on the direction of the magnetic field it can detect the direction we are facing.

How the HMC5883L Sensor Module Works

The HMC5883L being a magnetometer sensor does the same thing. It has the HMC5883L IC on it which is from Honeywell. This IC has 3 magneto-resistive materials inside which are arranged in the axes x, y and z. The amount of current flowing through these materials is sensitive to the earth’s magnetic field. So by measuring the change in current flowing through these materials we can detect the change in Earth’s magnetic field. Once the change is magnetic field is absorbed the values can then be sent to any embedded controller like a microcontroller or processor through the I2C protocol.

Since the sensor works by sensing the magnetic field, the output values will be greatly affected if a metal is placed nearby. This behavior can be leveraged to use these sensors as metal detectors also. Care should be taken not to bring magnets near this sensor since the strong magnetic field from a magnet might trigger false values on the sensor.

Difference between HMC5883L and QMC5883L

There is a common confusion revolving around these sensors for many beginners. This is because some vendors (actually most) sell the QMC5883L sensors instead of the original HMC5883L from Honeywell. It is mostly because the QMC5883L is way cheaper than the HMC5883L module. The sad part is that the working of these two sensors is slightly different and the same code cannot be used for both. This is because the I2C address of both the sensors is not the same. The code give in this tutorial will work only for QMC5883L the commonly available sensor module.

To know which model of sensor you are having, you just have to look up closely at the IC itself to read what is written on top of it. If it is written something like L883 then it is the HMC58836L and if it is written something like DA5883 then...

Read more »

Portable Network Graphics (PNG) - 58.62 kB - 05/26/2020 at 01:57

Preview
Download

  • 1 × Arduino Pro Mini
  • 1 × HMC5883L Magnetometer sensor
  • 1 × LED lights - 8Nos
  • 1 × 470Ohm Resistor – 8Nos
  • 1 × Barrel Jack

View all 7 components

  • 1
    Step 1
    /*
     * Program for Arduino Digital Compass using QMC5883
     * Project by: Aswinth Raj
     * Dated: 1-11-2018
     * Website: www.circuitdigest.com
     * Lib. from https://github.com/keepworking/Mecha_QMC5883L
     * WARNING: This code works only for QMC5883 Sensor which is commonly being sold as HMC5883 read article to find the actual name of the sensor you have.
     */
    
    #include <Wire.h> //Wire Librarey for I2C communication 
    #include <MechaQMC5883.h> //QMC5883 Librarey is added since mine is QMC583 and not HMC5883
    
    MechaQMC5883 qmc; //Create an object name for the snsor, I have named it as qmc
    
    int ledPins[] = {2,3,4,5,6,7,8,9}; //Array of output pin to which the LED is connected to
    char led_count = 7; //Total number of LED pins 
    
      
    void setup() {
      Wire.begin(); //Begin I2C communication 
      Serial.begin(9600); //Begin Serial Communication 
      qmc.init(); //Initialise the QMC5883 Sensor 
    
      for (int thisPin=0; thisPin <= led_count; thisPin++){ //Navigate through all the pins in array 
        pinMode(ledPins[thisPin],OUTPUT); //Declare them as output 
      }
    
    }
    
    void loop() { //Infinite Loop
      int x,y,z;
      qmc.read(&x,&y,&z); //Get the values of X,Y and Z from sensor 
      
      int heading=atan2(x, y)/0.0174532925; //Calculate the degree using X and Y parameters with this formulae 
    
     //Convert result into 0 to 360
      if(heading < 0) 
      heading+=360;
      heading = 360-heading;
      
      Serial.println(heading); //Print the value of heading in degree for debugging 
    
    //Based on the value of heading print the result for debugging and glow the respective LED.
      if (heading > 338 || heading < 22)
      {
        Serial.println("NORTH");
        digitalWrite(ledPins[0],HIGH);
      }
      if (heading > 22 && heading < 68)
      {
        Serial.println("NORTH-EAST");
        digitalWrite(ledPins[7],HIGH);
      }
      if (heading > 68 && heading < 113)
      {
        Serial.println("EAST");
        digitalWrite(ledPins[6],HIGH);
      }
      if (heading > 113 && heading < 158)
      {
        Serial.println("SOUTH-EAST");
        digitalWrite(ledPins[5],HIGH);
      }
      if (heading > 158 && heading < 203)
      {
        Serial.println("SOUTH");
        digitalWrite(ledPins[4],HIGH);
      }
      if (heading > 203 && heading < 248)
      {
        Serial.println("SOTUH-WEST");
        digitalWrite(ledPins[3],HIGH);
      }
      if (heading > 248 && heading < 293)
      {
        Serial.println("WEST");
        digitalWrite(ledPins[2],HIGH);
      }
      if (heading > 293 && heading < 338)
      {
        Serial.println("NORTH-WEST");
        digitalWrite(ledPins[1],HIGH);
      }
    
      delay(500); // update position of LED for every alf seconds 
    //Turn off the all the LED 
        for (int thisPin=0; thisPin <= led_count; thisPin++){
         digitalWrite(ledPins[thisPin],LOW);
      }
    }

View all instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates