Arduino HMC5883L Magnetometer interfacing

arduino magnetic compass

 

Do you need to Identify the Magnetic North for your project and confused what to do? and how to select magneto meter sensor? Here this article helps you take note.




This project uses HMC5883L triple axis magnetometer from Honeywell, it is a surface mount (Better to buy as breakout board) multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as low-cost compassing and magnetometry.

It utilize I²C (inter integrated circuit) interface technique to communicate with Microcontrollers and consumes 2.16-3.6VDC at low current draw and it gives 5 milli-guass resolution.

Breakout Board

10530-03  hmc5883l pcb

 

 

 

 

 

 

 

https://www.sparkfun.com/products/10530

Arduino Hookup

arduino hmc5883l hookup

Connect the Vcc of breakout board to Arduino’s 3.3V power source and Gnd to Gnd of Arduino. Connect SDA (serial data line) to A4 and SCL (serial clock line) to A5 of Arduino, that’s all the hookup is over. Now turning to Arduino code before that get HMC5883L Library here. There you can find from simple example to read sensor data serially to Graphical Magnetic compass using processing code.

Arduino Code

Simple Example to Read HMC5883L Sensor

 

#include <Wire.h> //I2C Arduino Library

#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup(){
  //Initialize Serial and I2C communications
  Serial.begin(9600);
  Wire.begin();
  
  //Put the HMC5883 IC into the correct operating mode
  Wire.beginTransmission(address); //open communication with HMC5883
  Wire.send(0x02); //select mode register
  Wire.send(0x00); //continuous measurement mode
  Wire.endTransmission();
}
  void loop(){
  
  int x,y,z; //triple axis data

  //Tell the HMC5883L where to begin reading data
  Wire.beginTransmission(address);
  Wire.send(0x03); //select register 3, X MSB register
  Wire.endTransmission();
  
 
 //Read data from each axis, 2 registers per axis
  Wire.requestFrom(address, 6);
  if(6<=Wire.available()){
    x = Wire.receive()<<8; //X msb
    x |= Wire.receive(); //X lsb
    z = Wire.receive()<<8; //Z msb
    z |= Wire.receive(); //Z lsb
    y = Wire.receive()<<8; //Y msb
    y |= Wire.receive(); //Y lsb
  }
  
  //Print out values of each axis
  Serial.print("x: ");
  Serial.print(x);
  Serial.print("  y: ");
  Serial.print(y);
  Serial.print("  z: ");
  Serial.println(z);
  
  delay(250);
}



Serial Port Data Sample

serial print magnetometer

 

Resources

HMC5883L Arduino Library

pdf-icon HMC5883L-FDS Datasheet
 



2 Comments

  1. parisa January 25, 2020 Reply
    • QA Team January 20, 2022 Reply

Add a Comment

Your email address will not be published. Required fields are marked *