Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » Interfacing HMC5883L Magnetometer with Raspberry Pi Pico
Raspberry Pi Raspberry Pi Pico Projects

Interfacing HMC5883L Magnetometer with Raspberry Pi Pico

Mamtaz AlamBy Mamtaz AlamUpdated:July 8, 20232 Comments6 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
HMC5883L Raspberry Pi Pico MicroPython
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

Overview

In this guide, we will be interfacing the HMC5883L Triple Axis Digital Magnetometer with a Raspberry Pi Pico & MicroPython Code to create our very own digital compass. The HMC5883L magnetometer serves as a feasible option for a digital compass because of its three-axis magnetometer functionality, which is often used to measure the strength and direction of magnetic fields. It can also be utilized to discern the Earth’s magnetic north.

The HMC5883L magnetometer will provide compass readings and measure the magnetic field of the X, Y, and Z axes in microteslas (µT). With these measurements, we can also calculate the heading in degrees. All these operations will be implemented using MicroPython on our Raspberry Pi Pico.


Bill of Materials

We need following components to work on this project.

S.N.ComponentsQuantityPurchase Link
1Raspberry Pi Pico1Amazon | AliExpress | SunFounder
2HMC5883L Magnetometer1Amazon | AliExpress | SunFounder
3Jumper Wires10Amazon | AliExpress | SunFounder
4Breadboard1Amazon | AliExpress | SunFounder



What is a Magnetic Compass & Magnetometer?

A magnetic compass is a tool for navigation that is employed to ascertain direction in relation to Earth’s magnetic poles. Its functionality is based on the concept of magnetism and incorporates a small, lightweight magnet which is delicately balanced on an almost friction-free pivot point, commonly known as the compass needle.

Magnetic Compass

The compass needle, having the ability to swivel without restraint, synchronizes itself with the Earth’s magnetic field, with its extremities pointing towards the magnetic north and south. This basic feature enables individuals to determine their bearings in reference to the four primary directions—north, south, east, and west.

A magnetometer is a non-invasive device that records variations in Earth’s magnetic field, delivering significant data for marine exploration. This includes the inspection of cultural heritage sites like ship and aircraft debris, as well as the description of geological attributes located on the ocean floor.

Magnetometer

The functioning of a magnetometer is rooted in the Earth’s core, which consists of an external liquid layer of magnetic iron and nickel. As the Earth rotates, the electric currents produced by these metals give rise to a magnetic field. The strength of this field is not uniform across the Earth’s surface and can be identified and measured with the aid of a magnetometer.


HMC5883L 3-Axis Compass/Magnetometer

The HMC5883L is a digital compass conceived by Honeywell. It is a surface-mount, multi-chip module engineered specifically for the detection of low magnetic fields. It is particularly adept at supplying directional data, making it a common choice for navigation and positioning systems.

HMC5883L Magnetometer Module

The design of the HMC5883L revolves around anisotropic magnetoresistive (AMR) technology, which facilitates the precise measurement of magnetic fields. It includes three-axis sensors, with field capacities spanning from ±1.3 to ±8.1 Gauss. This denotes its capability to measure magnetic fields across three distinct axes (X, Y, and Z), allowing it to sense the direction of the Earth’s magnetic field in a three-dimensional context.

The HMC5883L is equipped with numerous inherent features that simplify its incorporation into a variety of systems. Among these are an integrated ADC (Analog-to-Digital Converter) offering 12-bit data output, and an I2C serial bus interface. This interface is utilized for device communication, enabling it to transfer data to a microcontroller or another processing unit.

The HMC5883L Magnetometer Module is comprised of an HMC5883L Magnetometer IC, a Voltage Regulator IC, resistors, and capacitors, all consolidated within an integrated circuit. It uses a voltage regulator IC XC6206P332MR (662K).

Refer to HMC5883L datsheets to learn more: Download HMC5883L Datasheet




Specifications of HMC5883L

  • Operating Voltage: 3V to 6V DC
  • Operating Current: 100-130 μA
  • I2C interface
  • 1-2 degree heading accuracy
  • Integrated 12-bit ADC
  • 160Hz max data rate
  • Range of -8 to +8 Gauss
  • Data Output Rate: 0.75, 1.5, 3, 7.5, 15 Hz

Pinout of HMC5883L

The HMC5883L module has five pins as shown in this image below:

HMC5883L Pinout

Here is a basic description of the typical pinout for an HMC5883L module.

  1. VCC: This is the power supply pin. It requires a DC voltage, typically 3V -6V.
  2. GND: This is the ground pin.
  3. SCL: This is the clock line for the I2C interface. It’s connected to the microcontroller’s I2C clock line.
  4. SDA: This is the data line for the I2C interface. It’s connected to the microcontroller’s I2C data line.
  5. DRDY: Data Ready. This pin goes low when new data is available. It’s optional and isn’t always used in basic applications.

Register Mapping & I2C Communication

Register Address Name Access
00 Configuration Register A Read/Write
01 Configuration Register B Read/Write
02 Mode Register Read/Write
03 Data Output X MSB Register Read
04 Data Output X LSB Register Read
05 Data Output Z MSB Register Read
06 Data Output Z LSB Register Read
07 Data Output Y MSB Register Read
08 Data Output Y LSB Register Read
09 Status Register Read
10 Identification Register A Read
11 Identification Register B Read
12 Identification Register C Read

The HMC5883L magnetometer uses various internal registers to configure and receive data:

  1. Configuration Register A (0x00): Configures device operation, including output data rate and measurement configuration.
  2. Configuration Register B (0x01): Sets the device gain and sensitivity.
  3. Mode Register (0x02): Selects the operating mode, such as continuous measurement, single measurement, or idle mode.
  4. Data Output X Registers (0x03 and 0x04): Hold X-axis sensor readings.
  5. Data Output Y Registers (0x05 and 0x06): Hold Y-axis sensor readings.
  6. Data Output Z Registers (0x07 and 0x08): Hold Z-axis sensor readings.
  7. Status Register (0x09): Contains status flags about data output.
  8. Identification Registers (0x0A, 0x0B, 0x0C): Used to identify the device.

The HMC5883L employs the I2C communication protocol, functioning as a slave device. Its designated I2C device address is 0x1E. The corresponding addresses for its read and write operations are as follows:

  • For write operations (SLA+W), the address is 0x3C.
  • For read operations (SLA+R), the address is 0x3D.

Applications of HMC5883L

Some of the applications of HMC5883L Magnetometer in various fields are:

  • Smart Devices: Auto-rotate and orientation features.
  • GPS: Enhanced directional accuracy.
  • Robotics: Improved navigation.
  • Drones: Guidance and control.
  • Gaming: Advanced motion tracking.
  • AR/VR: Real-time orientation.
  • Industrial Use: Satellite positioning, antenna tracking.



Interfacing HMC5883L Magnetometer with Raspberry Pi Pico

Let us interface the HMC5883L Magnetometer with Raspberry Pi Pico using MicroPython Code and read the magnetometer data for X, Y, Z axis along with the heading information.


Wiring Diagram

The connection diagram is very simple.

Interfacing HMC5883L Magnetometer with Raspberry Pi Pico

Connect the VCC & GND pin of HMC5883L magnetometer to 3.3V & GND Pin of Raspberry Pi Pico. Similarly, connect the SDA & SCL pin of Magnetometer to GP20 & A4 GP21 of Pico respectively.

HMC5883L Digital Compass Raspberry Pi Pico


Source Code/Program

Here is a MicroPython Code to interface HC5883L Magnetometer with Raspberry Pi Pico. Copy the following code and save it to your Raspberry Pi Pico Board with name main.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from machine import I2C, Pin
import time
import math
 
# HMC5883L address and register addresses
HMC5883L_ADDR = 0x1E
CONFIG_REG_A = 0x00
CONFIG_REG_B = 0x01
MODE_REG = 0x02
DATA_REG = 0x03
 
# Conversion factor from raw value to uT
# With default gain 1 Ga = 1090 LSb
# And 1 Ga = 100 uT, therefore 1 LSb = 100 uT / 1090 LSb
LSB_TO_UT = 100.0 / 1090.0
 
# Initialize I2C
i2c = I2C(0, scl=Pin(21), sda=Pin(20), freq=100000)
 
# Check if HMC5883L is connected
if HMC5883L_ADDR not in i2c.scan():
    raise ValueError('HMC5883L not found')
 
# Write configuration to HMC5883L
# Continuous measurement mode, 15Hz data output rate
i2c.writeto_mem(HMC5883L_ADDR, CONFIG_REG_A, bytes([0x70]))
i2c.writeto_mem(HMC5883L_ADDR, CONFIG_REG_B, bytes([0x20]))
i2c.writeto_mem(HMC5883L_ADDR, MODE_REG, bytes([0x00]))
 
while True:
    data = i2c.readfrom_mem(HMC5883L_ADDR, DATA_REG, 6)
    x = ((data[0] << 8) | data[1])
    z = ((data[2] << 8) | data[3])
    y = ((data[4] << 8) | data[5])
    
    if x > 32767:
        x -= 65536
    if y > 32767:
        y -= 65536
    if z > 32767:
        z -= 65536
 
    # Convert to uT
    x *= LSB_TO_UT
    y *= LSB_TO_UT
    z *= LSB_TO_UT
 
    # Calculate heading in degrees
    heading = math.atan2(y, x)
    
    # Convert radian to degree
    heading = math.degrees(heading)
    
    # Due to declination, the heading may need to be corrected
    # Declination is the error between magnetic north and true north, depending on your geographical location.
    # Here, let's assume we have a declination of 0 degrees. If you know the declination in your area, put it instead.
    declination_angle = 0.0
    heading += declination_angle
    
    # Correct negative values
    if heading < 0:
        heading += 360
    
    print("Magnetic field in X: %.2f uT, Y: %.2f uT, Z: %.2f uT, Heading: %.2f°" % (x, y, z, heading))
    time.sleep(0.1)

Once you run the code, the Thonny Shell will display the following readings.

The Shell display the Magnetometer reading in X, Y, Z axis in micro-Tesla (uT) and also the heading angle in degrees. You can rotate the magnetometer to observe the change in the readings.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleDigital Compass using HMC5883L Magnetometer & Arduino
Next Article How to use INA219 DC Current Sensor Module with Arduino

Related Posts

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

Updated:July 24, 2025
Interface BMI160 with Raspberry Pi Pico & MicroPython

Interface BMI160 with Raspberry Pi Pico & MicroPython

Updated:February 2, 20253K
Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Updated:February 2, 202513K
Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Updated:February 2, 20253K
Modbus RTU with Raspberry Pi Pico & Micropython

Modbus RTU with Raspberry Pi Pico & MicroPython

Updated:February 2, 20258K
Fever Detector with MLX90640 & OpenCV Raspberry Pi

Thermal Fever Detector with MLX90640 & OpenCV Raspberry Pi

Updated:February 2, 20256K
View 2 Comments

2 Comments

  1. Bent Jensen on December 3, 2023 1:49 PM

    I tried this code on my pico w with micropython, but I cannot set HMC5883L_ADDR = 0x1E. i2c.scan() returns 0x0d, but then it returns all 0 data. what am I doing wrong?

    Reply
  2. Bent Jensen on December 27, 2023 7:36 PM

    I got it working to the point where it prints out data, but the heading changes all the time and too much to be just ‘noise’.

    Reply

CommentsCancel reply

Latest Posts
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

May 31, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

February 1, 2026
Top Posts & Pages
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • IoT Based Drinking Water Quality Monitoring with ESP32
    IoT Based Drinking Water Quality Monitoring with ESP32
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • DIY IoT Water pH Meter using pH Sensor & ESP32
    DIY IoT Water pH Meter using pH Sensor & ESP32
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
  • How to use Modbus RTU with ESP32 to read Sensor Data
    How to use Modbus RTU with ESP32 to read Sensor Data
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (204)
    • ESP32 MicroPython (7)
    • ESP32 Projects (81)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.