Compass (Magnetometer)
Measures Earth's magnetic field to determine heading and bearing for navigation and orientation projects.
Overview
A digital compass (magnetometer) measures the strength and direction of magnetic fields around it. By detecting Earth's magnetic field, it calculates the heading (0-360 degrees) and cardinal bearing (N, NE, E, SE, S, SW, W, NW). ArduinoStudio supports the HMC5883L — the most widely used magnetometer for Arduino projects.
The component emits sensor-data events with heading (degrees) and bearing (cardinal direction string).
Specifications
| Parameter | Value |
|---|---|
| Sensor | HMC5883L |
| Field Range | +-1.3 to +-8 Gauss |
| Heading Accuracy | 1 - 2 degrees |
| Interface | I2C |
| I2C Address | 0x1E |
| Operating Voltage | 3.3V only |
Wiring Guide
| HMC5883L Pin | Arduino Pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| SDA | A4 (Uno) / 20 (Mega) |
| SCL | A5 (Uno) / 21 (Mega) |
| DRDY | Optional — data-ready interrupt |
Important: The HMC5883L is a 3.3V sensor. Use a level shifter on the I2C lines when connecting to a 5V Arduino. Many breakout boards include this level shifting on-board.
Placement Tips
- Mount the sensor away from motors, speakers, and other magnetic sources
- Keep it level for accurate heading readings
- Calibrate by rotating the sensor 360 degrees to account for local magnetic distortion (hard/soft iron compensation)
- Magnetic declination varies by location — adjust if precise heading is needed
Usage in ArduinoStudio
Drag the Compass component onto the canvas. It connects via I2C automatically. Heading and bearing data stream continuously. Use cases include:
- Robot navigation — maintain a heading or turn to a specific bearing
- Weather vane — display wind direction
- Combine with GPS for complete navigation
- Augmented reality compass displays