Stufin
Home Quick Cart Profile

HMC5883L Triple Axis Compass

Buy Now

Package size

4 mm x 4 mm x 0.95 mm

Pin pitch

0.5 mm

Recommended Usage and Precautions

The HMC5883L should be used in a well-ventilated area, away from magnetic interference sources.

The module should be handled with care to avoid mechanical stress or damage.

Proper grounding and shielding techniques should be used to minimize electromagnetic interference.

The module's operating temperature range should be strictly adhered to avoid performance degradation or damage.

By providing accurate and reliable magnetic field measurements, the HMC5883L Triple Axis Compass enables devices to determine their orientation and direction in three-dimensional space, making it an ideal component for a wide range of applications.

Pin Configuration

  • HMC5883L Triple Axis Compass Documentation
  • Pin Description:
  • The HMC5883L Triple Axis Compass has 16 pins, which are explained below:
  • 1. VCC (Pin 1)
  • Function: Power supply voltage (3.3V to 5V)
  • Description: Connect to the power source (e.g., Arduino board's 3.3V or 5V pin)
  • 2. GND (Pin 2)
  • Function: Ground
  • Description: Connect to the ground (e.g., Arduino board's GND pin)
  • 3. SCL (Pin 3)
  • Function: IC clock signal
  • Description: Connect to the IC clock signal of the microcontroller (e.g., Arduino board's SCL pin)
  • 4. SDA (Pin 4)
  • Function: IC data signal
  • Description: Connect to the IC data signal of the microcontroller (e.g., Arduino board's SDA pin)
  • 5. DRDY (Pin 5)
  • Function: Data ready signal (active low)
  • Description: Connect to an interrupt pin of the microcontroller to detect when new data is available
  • 6. INT (Pin 6)
  • Function: Interrupt signal (active low)
  • Description: Connect to an interrupt pin of the microcontroller to detect interrupts (e.g., overflow)
  • 7. NO CONN (Pin 7)
  • Function: No connection
  • Description: Leave unconnected
  • 8. VDDIO (Pin 8)
  • Function: I/O voltage (1.7V to VCC)
  • Description: Connect to the voltage reference for the IC interface (e.g., Arduino board's VCC pin)
  • 9. XOUT (Pin 9)
  • Function: X-axis magnetometer output
  • Description: Connect to an analog-to-digital converter (ADC) input of the microcontroller
  • 10. YOUT (Pin 10)
  • Function: Y-axis magnetometer output
  • Description: Connect to an ADC input of the microcontroller
  • 11. ZOUT (Pin 11)
  • Function: Z-axis magnetometer output
  • Description: Connect to an ADC input of the microcontroller
  • 12. NO CONN (Pin 12)
  • Function: No connection
  • Description: Leave unconnected
  • 13. NO CONN (Pin 13)
  • Function: No connection
  • Description: Leave unconnected
  • 14. NO CONN (Pin 14)
  • Function: No connection
  • Description: Leave unconnected
  • 15. NO CONN (Pin 15)
  • Function: No connection
  • Description: Leave unconnected
  • 16. NO CONN (Pin 16)
  • Function: No connection
  • Description: Leave unconnected
  • Connection Structure:
  • To connect the HMC5883L Triple Axis Compass to an Arduino board, follow this structure:
  • VCC (Pin 1) -> Arduino's 3.3V or 5V pin
  • GND (Pin 2) -> Arduino's GND pin
  • SCL (Pin 3) -> Arduino's SCL pin
  • SDA (Pin 4) -> Arduino's SDA pin
  • DRDY (Pin 5) -> Arduino's Interrupt pin (e.g., D2)
  • INT (Pin 6) -> Arduino's Interrupt pin (e.g., D3)
  • VDDIO (Pin 8) -> Arduino's VCC pin
  • XOUT (Pin 9) -> Arduino's ADC input pin (e.g., A0)
  • YOUT (Pin 10) -> Arduino's ADC input pin (e.g., A1)
  • ZOUT (Pin 11) -> Arduino's ADC input pin (e.g., A2)
  • Note: Make sure to use a suitable voltage level shifter if the microcontroller operates at a different voltage level than the HMC5883L.
  • Remember to consult the datasheet and the microcontroller's documentation for specific connection requirements and programming details.

Code Examples

HMC5883L Triple Axis Compass Component Documentation
Overview
The HMC5883L is a triple-axis magnetometer designed for low-field magnetic sensing applications such as compassing and magnetometer applications. The device provides accurate and reliable magnetic field measurements, making it suitable for use in various IoT applications, including robotics, navigation, and gesture recognition.
Pinout and Hardware Connection
The HMC5883L is a 3.3V device with a 16-pin LFCSP (Lead-Free Chip Scale Package) package. The pinout is as follows:
VCC: 3.3V power supply
 GND: Ground
 SCL: I2C clock line
 SDA: I2C data line
 DRDY: Data ready output
To connect the HMC5883L to a microcontroller or single-board computer, use the following hardware configuration:
VCC to 3.3V power supply
 GND to ground
 SCL to I2C clock line (e.g., GPIO 5 on Raspberry Pi)
 SDA to I2C data line (e.g., GPIO 6 on Raspberry Pi)
Software Interface
The HMC5883L communicates with the host device using the I2C protocol. The device address is 0x1E or 0x1F, depending on the state of the address pin (ADD).
Code Examples
### Example 1: Basic Compass Reading using Arduino
This example demonstrates how to read the magnetic field data from the HMC5883L using Arduino.
```c
#include <Wire.h>
#define HMC5883L_ADDRESS 0x1E // or 0x1F depending on the ADD pin state
void setup() {
  Wire.begin();
  Serial.begin(9600);
}
void loop() {
  int x, y, z;
  
  // Read the magnetic field data
  Wire.beginTransmission(HMC5883L_ADDRESS);
  Wire.write(0x00); // Register address for X-axis
  Wire.endTransmission();
  Wire.requestFrom(HMC5883L_ADDRESS, 6);
  
  x = Wire.read() << 8 | Wire.read();
  z = Wire.read() << 8 | Wire.read();
  y = Wire.read() << 8 | Wire.read();
  
  // Calculate the heading (azimuth)
  float heading = atan2(y, x);
  
  // Convert the heading to degrees
  float degrees = heading  180 / 3.14159;
  
  Serial.print("Heading: ");
  Serial.print(degrees);
  Serial.println(" degrees");
  
  delay(500);
}
```
### Example 2: Compass Reading with Calibration using Python on Raspberry Pi
This example demonstrates how to read and calibrate the magnetic field data from the HMC5883L using Python on Raspberry Pi.
```python
import smbus
import math
# I2C bus and HMC5883L address
bus = smbus.SMBus(1)
address = 0x1E  # or 0x1F depending on the ADD pin state
# Function to read the magnetic field data
def read_compass():
  x = bus.read_word_data(address, 0x00)
  y = bus.read_word_data(address, 0x02)
  z = bus.read_word_data(address, 0x04)
  return x, y, z
# Function to calculate the heading (azimuth)
def calculate_heading(x, y):
  heading = math.atan2(y, x)
  return heading  180 / math.pi
# Calibrate the compass
min_x, max_x, min_y, max_y = float('inf'), float('-inf'), float('inf'), float('-inf')
for _ in range(500):
  x, y, _ = read_compass()
  min_x, max_x = min(min_x, x), max(max_x, x)
  min_y, max_y = min(min_y, y), max(max_y, y)
x_offset = (max_x + min_x) / 2
y_offset = (max_y + min_y) / 2
x_scale = (max_x - min_x) / 2
y_scale = (max_y - min_y) / 2
# Read and calculate the calibrated heading
while True:
  x, y, _ = read_compass()
  x_cal = (x - x_offset) / x_scale
  y_cal = (y - y_offset) / y_scale
  heading = calculate_heading(x_cal, y_cal)
  print("Heading: {:.2f} degrees".format(heading))
```
These examples demonstrate the basic functionality of the HMC5883L Triple Axis Compass component. For more advanced applications, refer to the device datasheet and the documentation of the host device's I2C library.