How to Make Magnetic Compass with Arduino and HMC5883 Digital Compass

Introduction

Welcome to the realm of Arduino and navigation! In this guide, we’ll explore How to Make Magnetic Compass with Arduino and HMC5883 Digital Compass module. The HMC5883 offers a convenient and accurate way to detect magnetic fields, allowing us to determine the orientation of our compass. Whether you’re navigating the wilderness or simply experimenting with sensors, building a magnetic compass with Arduino can be both educational and practical. Let’s dive into the details of the HMC5883 digital compass and learn how to harness its capabilities with Arduino.

Hardware Required

You will require the following Hardware Components for How to Interface HMC5883L Magnetometer Module with Arduino.

Components#Buy From Amazon
Arduino UNO1Buy Link
HMC5883L Magnetometer Module1Buy Link
9v DC Adapter (Optional)1Buy Link
Jumper Wires4Buy Link
Breadboard1Buy Link

What is an HMC5883 Digital Compass?

The HMC5883 is a digital compass module manufactured by Honeywell. It utilizes magnetoresistive sensors to detect magnetic fields and determine the orientation of the module relative to the Earth’s magnetic field. The HMC5883 communicates with Arduino via the I2C (Inter-Integrated Circuit) interface, making it easy to integrate into Arduino projects. With its compact size, high precision, and simple interface, the HMC5883 digital compass is ideal for applications requiring accurate orientation sensing.

Pinout

HMC5883L-Triple-Axis-Magnetometer-Module-Pinout

Pin Configuration

Pin NumberPin NameDescription
1VccPower supply voltage input (3.3V or 5V)
2GNDGround
3SCLSerial Clock (I2C)
4SDASerial Data (I2C)
5DRDYData Ready (Output)

Features

  1. Three-Axis Magnetoresistive Sensors: Detects magnetic fields along the X, Y, and Z axes simultaneously, providing accurate orientation sensing.
  2. Digital Output: Provides digital output data for easy integration with microcontrollers like Arduino.
  3. Low Power Consumption: Designed for low power operation, making it suitable for battery-powered applications.
  4. High Precision: Offers high-resolution measurements for accurate orientation detection.
  5. Calibration Support: Includes built-in calibration routines to compensate for magnetic interference and ensure accurate readings.

Specifications

  1. Measurement Range:
    • ±1.3 to ±8.1 Gauss.
  2. Resolution:
    • Up to 0.2 milliGauss.
  3. Communication Interface:
    • I2C (Inter-Integrated Circuit) interface for communication with Arduino.
    • Operating voltage: 3.3V to 5V.
  4. Sampling Rate:
    • Programmable sampling rates up to 160Hz.
  5. Temperature Range:
    • Operating Temperature: -40°C to +85°C.

Circuit Diagram

The following circuit shows you the connection of the How to Make Magnetic Compass with Arduino and HMC5883 Digital Compass Please make the connection carefully

Magnetic-compass-with-Arduino-and-digital-compass-HMC5883-Circuit

Circuit Connections

ArduinoMagnetometer Module
+5V+V Pin
GNDGND Pin
A4SDA
A5SCL

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Installing Libraries

Now when you are Ready to upload the code, to the Arduino Board you will need first to add the Following Libraries in Arduino, If you Learn How to add the library in the Arduino step-by-step guide click on how to install the library Button given Blow

Code

The library includes code examples that we recommend reviewing. The following examples are variations of those provided in the library.

//For more Projects: www.arduinocircuit.com

//GND ​​- GND
//VCC - VCC
//SDA - Pin A4
//SCL - Pin A5

#include "Wire.h"
#include "I2Cdev.h"
#include "HMC5883L.h"

HMC5883L compass;

int16_t mx, my, mz;

void setup()
{
    
    Serial.begin(9600);
    Wire.begin();
    compass.initialize();
}

void loop()
{
    //Get components of the magnetic field
    compass.getHeading(&mx, &my, &mz);
    
    Serial.print("mx:");
    Serial.print(mx);
    Serial.print("tmy:");
    Serial.print(my);
    Serial.print("tmz:");
    Serial.println(mz);
    delay(100);
}

Obtain orientation with respect to north

In this example, we use magnetic field measurements to determine the sensor’s orientation relative to magnetic north. However, it’s essential to consider that geographic north isn’t perfectly aligned with magnetic north due to declination, which varies based on your location on the globe. To obtain orientation relative to geographic north, you’ll need to input the declination for your specific location.

//For more Projects: www.arduinocircuit.com

//GND ​​- GND
//VCC - VCC
//SDA - Pin A4
//SCL - Pin A5

#include "Wire.h"
#include "I2Cdev.h"
#include "HMC5883L.h"

HMC5883L compass;

int16_t mx, my, mz;

//declination in degrees in your position
const float declination = 0.12;

void setup()
{
    Serial.begin(9600);
    Wire.begin();
    compass .initialize();
}

void loop() {
    //Get components of the magnetic field
    compass .getHeading(&mx, &my, &mz);

    //Calculate angle the angle of the X axis with respect to north
    float angle = atan2(my, mx);
    angle = angle * RAD_TO_DEG;
    angle = angle - declination;
    
    if(angle < 0) angle = angle + 360;
    
    Serial.print("N:");
    Serial.println(angle,0);
}

Applications

  1. Navigation Systems: Use the HMC5883 digital compass in navigation systems for determining heading, direction, and orientation in applications such as GPS navigation, robotics, and autonomous vehicles.
  2. Geographic Information Systems (GIS): Integrate the HMC5883 into GIS applications for mapping, surveying, and geolocation services, providing accurate compass readings for spatial data analysis.
  3. Outdoor Adventure Gear: Incorporate the HMC5883 digital compass into outdoor adventure gear such as hiking compasses, camping equipment, and survival kits, helping users navigate and orient themselves in the wilderness.
  4. Vehicle Navigation: Utilize the HMC5883 in vehicle navigation systems for automotive applications such as compass displays, heading sensors, and vehicle tracking, enabling accurate direction sensing and navigation assistance.
  5. Educational Demonstrations: Use the HMC5883 digital compass in educational settings to demonstrate principles of magnetism, navigation, and sensor technology, fostering hands-on learning experiences for students and enthusiasts.

Conclusion

With its advanced features and versatile applications, the HMC5883 digital compass offers endless possibilities for navigation and orientation sensing in Arduino projects. Whether you’re building navigation systems, GIS applications, outdoor adventure gear, vehicle navigation systems, or educational demonstrations, the HMC5883 provides precise and reliable compass readings for enhanced functionality and performance. Let’s embark on the journey of creating a magnetic compass with Arduino and the HMC5883 digital compass module, and explore the wonders of navigation technology!

Leave a Comment


error: