4 mm x 4 mm x 0.95 mm
4 mm x 4 mm x 0.95 mm
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.
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.