Triple Axis Magnetometer HMC5883L Interfacing with Raspberry Pi

Overview of Magnetometer HMC5883L 

HMC5883L Magnetometer Module
HMC5883L Magnetometer Module

 

  • Magnetometer HMC5883L is used for measuring the direction and magnitude of the Earth’s magnetic field. It is used for low cost compassing and magnetometry.
  • It measures the Earth’s magnetic field value along the X, Y, and Z axes from milli-gauss to 8 gauss.
  • It can be used to find the direction of the heading of the device.
  • It uses the I2C protocol for communication with the microcontroller.

For more information about Magnetometer HMC5883L and how to use it, refer the topic HMC5883L Magnetometer Module in the sensors and modules section.

To interface the HMC5883L magnetometer module with Raspberry Pi, we should ensure that the I2C protocol on Raspberry Pi is enabled. So before going for interfacing HMC5883L with Raspberry Pi, we need to make some I2C configurations on Raspberry Pi for which you can refer Raspberry Pi I2C.

 

Connection Diagram of HMC5883L Magnetometer with Raspberry Pi

Magnetometer Interfacing with Raspberry Pi 3
HMC5883L Magnetometer Interfacing with Raspberry Pi 3

 

 

Get the angle in degrees using HMC5883L Magnetometer and Raspberry Pi

Reading magnetic field strength along the x, y, and z axes from the HMC5883L magnetometer module, calculate the heading angle from these three axes and displaying heading angle on Serial Monitor.

Let’s interface magnetometer HMC5883L with Raspberry Pi and calculate its heading angle. HMC5883L uses the I2C protocol for communication. We can interface it with Raspberry by using Python. 

To know about Python based I2C functions used on Raspberry Pi, you can refer Python based I2C functions for Raspberry Pi

 

Programming steps

  • First we need to set configuration register A for average of 8-sample measurement with 15 Hz default data output rate
  • Set Gain using Configuration Register B i.e. here its 0xA0. (or we can choose any other desired gain)
  • Select Continuous measurement mode of operation in Mode Register. Hence Mode Register value will become 0x00.

After initialization, read X, Y and Z-axis registers raw values.

Calculate Heading value by using following formula,

Magnetic Heading value

 

HMC5883L Python Program for Raspberry Pi

'''
    Find Heading by using HMC5883L interface with Raspberry Pi using Python
	http://www.electronicwings.com
'''
import smbus		#import SMBus module of I2C
from time import sleep  #import sleep
import math

#some MPU6050 Registers and their Address
Register_A     = 0              #Address of Configuration register A
Register_B     = 0x01           #Address of configuration register B
Register_mode  = 0x02           #Address of mode register

X_axis_H    = 0x03              #Address of X-axis MSB data register
Z_axis_H    = 0x05              #Address of Z-axis MSB data register
Y_axis_H    = 0x07              #Address of Y-axis MSB data register
declination = -0.00669          #define declination angle of location where measurement going to be done
pi          = 3.14159265359     #define pi value


def Magnetometer_Init():
        #write to Configuration Register A
        bus.write_byte_data(Device_Address, Register_A, 0x70)

        #Write to Configuration Register B for gain
        bus.write_byte_data(Device_Address, Register_B, 0xa0)

        #Write to mode Register for selecting mode
        bus.write_byte_data(Device_Address, Register_mode, 0)
	
	

def read_raw_data(addr):
    
        #Read raw 16-bit value
        high = bus.read_byte_data(Device_Address, addr)
        low = bus.read_byte_data(Device_Address, addr+1)

        #concatenate higher and lower value
        value = ((high << 8) | low)

        #to get signed value from module
        if(value > 32768):
            value = value - 65536
        return value


bus = smbus.SMBus(1) 	# or bus = smbus.SMBus(0) for older version boards
Device_Address = 0x1e   # HMC5883L magnetometer device address

Magnetometer_Init()     # initialize HMC5883L magnetometer 

print (" Reading Heading Angle")

while True:
    
	
        #Read Accelerometer raw value
        x = read_raw_data(X_axis_H)
        z = read_raw_data(Z_axis_H)
        y = read_raw_data(Y_axis_H)

        heading = math.atan2(y, x) + declination
        
        #Due to declination check for >360 degree
        if(heading > 2*pi):
                heading = heading - 2*pi

        #check for sign
        if(heading < 0):
                heading = heading + 2*pi

        #convert into angle
        heading_angle = int(heading * 180/pi)

        print ("Heading Angle = %d°" %heading_angle)
        sleep(1)

 

 

HMC5883L Output

Magnetometer Output

Components Used

HMC5883L Magnetometer Module
Magnetometer HMC5883L is developed by Honeywell. It gives the heading direction.
1
Raspberry Pi 4B
Raspberry Pi 4BRaspberry Pi 4B
1
Raspberry Pi Zero
Raspberry Pi Zero
1

Downloads

HMC5883 Interface with Raspberry using Python Download
Ad