Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Component Documentation

How to Use HMC5883L: Examples, Pinouts, and Specs

Image of HMC5883L
Cirkit Designer LogoDesign with HMC5883L in Cirkit Designer

Introduction

The HMC5883L is a digital compass sensor equipped with a three-axis magnetometer for measuring magnetic fields. It is widely used in navigation, orientation, and motion-tracking applications. The sensor communicates with microcontrollers via the I2C protocol, making it easy to integrate into various projects. Its compact size and low power consumption make it ideal for portable devices and embedded systems.

Explore Projects Built with HMC5883L

Use Cirkit Designer to design, explore, and prototype these projects online. Some projects support real-time simulation. Click "Open Project" to start designing instantly!
Raspberry Pi Pico-Based Navigation Assistant with Bluetooth and GPS
Image of sat_dish: compass example: A project utilizing HMC5883L in a practical application
This circuit features a Raspberry Pi Pico microcontroller interfaced with an HC-05 Bluetooth module for wireless communication, an HMC5883L compass module for magnetic field measurement, and a GPS NEO 6M module for location tracking. The Pico is configured to communicate with the HC-05 via serial connection (TX/RX), with the compass module via I2C (SCL/SDA), and with the GPS module via serial (TX/RX). Common power (VCC) and ground (GND) lines are shared among all modules, indicating a unified power system.
Cirkit Designer LogoOpen Project in Cirkit Designer
Arduino Uno R3 and HMC5883L Compass Interface
Image of Magnometer: A project utilizing HMC5883L in a practical application
This circuit connects an HMC5883L compass module to an Arduino Uno R3 for the purpose of reading magnetic field data. The Arduino is programmed to initialize the compass module, continuously read its X, Y, and Z magnetometer data, and output the readings to the Serial Monitor. The compass module is interfaced with the Arduino via I2C communication, using the SDA and SCL lines, and powered through the Arduino's VIN pin.
Cirkit Designer LogoOpen Project in Cirkit Designer
Raspberry Pi Pico-Based Bluetooth Compass
Image of sat_dish: compass-intro: A project utilizing HMC5883L in a practical application
This circuit features a Raspberry Pi Pico microcontroller interfaced with an HC-05 Bluetooth module and an HMC5883L digital compass. The Pico's GPIO pins are configured for serial communication with the HC-05 (TX/RX) and I2C communication with the HMC5883L (SCL/SDA). The circuit is likely designed for wireless data transmission of compass readings.
Cirkit Designer LogoOpen Project in Cirkit Designer
ESP32C3-Based Multi-Sensor Data Logger with Wi-Fi and SD Card Storage
Image of Petora_v1: A project utilizing HMC5883L in a practical application
This circuit is a data logging and transmission system that collects environmental and positional data from various sensors (BME/BMP280, HMC5883L, MAX30102, GPS NEO 6M) and records it to an SD card. The data is also sent over WiFi to a specified server using an ESP32 microcontroller.
Cirkit Designer LogoOpen Project in Cirkit Designer

Explore Projects Built with HMC5883L

Use Cirkit Designer to design, explore, and prototype these projects online. Some projects support real-time simulation. Click "Open Project" to start designing instantly!
Image of sat_dish: compass example: A project utilizing HMC5883L in a practical application
Raspberry Pi Pico-Based Navigation Assistant with Bluetooth and GPS
This circuit features a Raspberry Pi Pico microcontroller interfaced with an HC-05 Bluetooth module for wireless communication, an HMC5883L compass module for magnetic field measurement, and a GPS NEO 6M module for location tracking. The Pico is configured to communicate with the HC-05 via serial connection (TX/RX), with the compass module via I2C (SCL/SDA), and with the GPS module via serial (TX/RX). Common power (VCC) and ground (GND) lines are shared among all modules, indicating a unified power system.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of Magnometer: A project utilizing HMC5883L in a practical application
Arduino Uno R3 and HMC5883L Compass Interface
This circuit connects an HMC5883L compass module to an Arduino Uno R3 for the purpose of reading magnetic field data. The Arduino is programmed to initialize the compass module, continuously read its X, Y, and Z magnetometer data, and output the readings to the Serial Monitor. The compass module is interfaced with the Arduino via I2C communication, using the SDA and SCL lines, and powered through the Arduino's VIN pin.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of sat_dish: compass-intro: A project utilizing HMC5883L in a practical application
Raspberry Pi Pico-Based Bluetooth Compass
This circuit features a Raspberry Pi Pico microcontroller interfaced with an HC-05 Bluetooth module and an HMC5883L digital compass. The Pico's GPIO pins are configured for serial communication with the HC-05 (TX/RX) and I2C communication with the HMC5883L (SCL/SDA). The circuit is likely designed for wireless data transmission of compass readings.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of Petora_v1: A project utilizing HMC5883L in a practical application
ESP32C3-Based Multi-Sensor Data Logger with Wi-Fi and SD Card Storage
This circuit is a data logging and transmission system that collects environmental and positional data from various sensors (BME/BMP280, HMC5883L, MAX30102, GPS NEO 6M) and records it to an SD card. The data is also sent over WiFi to a specified server using an ESP32 microcontroller.
Cirkit Designer LogoOpen Project in Cirkit Designer

Common Applications

  • Electronic compasses
  • GPS navigation systems
  • Robotics and drones for orientation and heading
  • Motion tracking in augmented reality (AR) and virtual reality (VR) systems
  • Magnetic field detection and mapping

Technical Specifications

Below are the key technical details of the HMC5883L:

Parameter Value
Operating Voltage 2.16V to 3.6V
Communication Protocol I2C
Measurement Range ±1.3 to ±8 Gauss
Resolution 12-bit ADC
Operating Temperature -40°C to +85°C
Power Consumption 100 µA (typical)
Dimensions 3mm x 3mm x 0.9mm

Pin Configuration

The HMC5883L has 8 pins, but only 4 are typically used for operation. Below is the pinout description:

Pin Name Description
1 VCC Power supply input (2.16V to 3.6V)
2 GND Ground connection
3 SCL I2C clock line
4 SDA I2C data line
5-8 NC Not connected (leave unconnected in typical usage)

Usage Instructions

Connecting the HMC5883L to an Arduino UNO

To use the HMC5883L with an Arduino UNO, follow these steps:

  1. Connect the VCC pin of the HMC5883L to the 3.3V pin on the Arduino.
  2. Connect the GND pin of the HMC5883L to the GND pin on the Arduino.
  3. Connect the SCL pin of the HMC5883L to the A5 pin on the Arduino (I2C clock line).
  4. Connect the SDA pin of the HMC5883L to the A4 pin on the Arduino (I2C data line).

Sample Arduino Code

Below is an example Arduino sketch to read data from the HMC5883L:

#include <Wire.h>

// HMC5883L I2C address
#define HMC5883L_ADDRESS 0x1E

void setup() {
  Wire.begin(); // Initialize I2C communication
  Serial.begin(9600); // Start serial communication for debugging

  // Initialize HMC5883L
  Wire.beginTransmission(HMC5883L_ADDRESS);
  Wire.write(0x00); // Select configuration register A
  Wire.write(0x70); // Set 8-average, 15 Hz default, normal measurement
  Wire.endTransmission();

  Wire.beginTransmission(HMC5883L_ADDRESS);
  Wire.write(0x01); // Select configuration register B
  Wire.write(0xA0); // Set gain to 5
  Wire.endTransmission();

  Wire.beginTransmission(HMC5883L_ADDRESS);
  Wire.write(0x02); // Select mode register
  Wire.write(0x00); // Continuous measurement mode
  Wire.endTransmission();
}

void loop() {
  int16_t x, y, z;

  // Request 6 bytes of data from the HMC5883L
  Wire.beginTransmission(HMC5883L_ADDRESS);
  Wire.write(0x03); // Start reading from data output register
  Wire.endTransmission();
  Wire.requestFrom(HMC5883L_ADDRESS, 6);

  if (Wire.available() == 6) {
    // Read data and combine high and low bytes
    x = (Wire.read() << 8) | Wire.read();
    z = (Wire.read() << 8) | Wire.read();
    y = (Wire.read() << 8) | Wire.read();

    // Print the raw magnetometer data
    Serial.print("X: ");
    Serial.print(x);
    Serial.print(" Y: ");
    Serial.print(y);
    Serial.print(" Z: ");
    Serial.println(z);
  }

  delay(500); // Wait for 500ms before the next reading
}

Important Considerations

  • The HMC5883L operates at 3.3V. If your microcontroller uses 5V logic, use a level shifter to avoid damaging the sensor.
  • Ensure proper pull-up resistors (typically 4.7kΩ) are connected to the SCL and SDA lines if they are not already present on your breakout board.
  • Avoid placing the sensor near strong magnetic fields or ferromagnetic materials, as they can interfere with measurements.

Troubleshooting and FAQs

Common Issues

  1. No data or incorrect readings from the sensor.

    • Solution: Verify the wiring connections and ensure the I2C address (0x1E) matches the sensor's default address.
    • Tip: Use an I2C scanner sketch to confirm the sensor is detected on the I2C bus.
  2. Readings are inconsistent or fluctuate significantly.

    • Solution: Ensure the sensor is not near any magnetic interference sources, such as motors or power supplies.
    • Tip: Calibrate the sensor by rotating it in all directions to account for hard and soft iron distortions.
  3. Arduino cannot communicate with the sensor.

    • Solution: Check that the pull-up resistors are properly connected to the SCL and SDA lines.
    • Tip: Confirm that the Arduino's I2C pins (A4 and A5) are correctly connected to the sensor.

FAQs

Q: Can the HMC5883L measure the Earth's magnetic field?
A: Yes, the HMC5883L is designed to measure the Earth's magnetic field and is commonly used in electronic compasses.

Q: How do I calibrate the HMC5883L?
A: Calibration involves rotating the sensor in all directions to collect data and then applying algorithms to correct for distortions. Libraries like "Adafruit HMC5883L" include built-in calibration functions.

Q: Can I use the HMC5883L with a 5V microcontroller?
A: Yes, but you must use a level shifter to step down the 5V logic to 3.3V to avoid damaging the sensor.

Q: What is the maximum range of the HMC5883L?
A: The sensor can measure magnetic fields in the range of ±8 Gauss, which is sufficient for most navigation and orientation applications.