Shows real-time heading and direction on an OLED using an HMC5883L magnetometer.
Built and tested by Anju.A · Industrial Robotics Institute

A Digital Compass System determines direction using the Earth’s magnetic field. The HMC5883L magnetometer measures the magnetic field and an Arduino Uno calculates the heading angle from 0° to 360°. The heading and direction (North, East, South, West) are shown in real time on an SSD1306 OLED display.
| Component | Qty |
|---|---|
| Arduino Uno | 1 |
| HMC5883L Magnetometer Sensor | 1 |
| OLED Display (SSD1306, 128×64) | 1 |
| Connecting Wires | As needed |
| Power Supply | 1 |
| From | Connect to |
|---|---|
| HMC5883L VCC | 3.3V / 5V |
| HMC5883L GND | GND |
| HMC5883L SDA | A4 (SDA) |
| HMC5883L SCL | A5 (SCL) |
| OLED VCC | 3.3V / 5V |
| OLED GND | GND |
| OLED SDA | A4 (SDA) |
| OLED SCL | A5 (SCL) |
Complete, tested Arduino C++ code for this project. Copy it into Arduino IDE and upload it to the Arduino Uno.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HMC5883L.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
HMC5883L compass;
float heading;
void setup() {
Wire.begin();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
while(1);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
compass.initialize();
}
void loop() {
Vector norm = compass.readNormalize();
heading = atan2(norm.YAxis, norm.XAxis);
heading = heading * 180 / 3.14159265;
if(heading < 0) heading += 360;
display.clearDisplay();
display.setCursor(0,10);
display.setTextSize(1);
display.print("Heading: ");
display.print(heading);
display.print((char)247);
display.setCursor(0,30);
display.setTextSize(2);
if (heading >= 315 || heading < 45)
display.print("NORTH");
else if (heading >= 45 && heading < 135)
display.print("EAST");
else if (heading >= 135 && heading < 225)
display.print("SOUTH");
else
display.print("WEST");
display.display();
delay(500);
}
A Digital Compass System determines direction using the Earth’s magnetic field. The HMC5883L magnetometer measures the magnetic field and an Arduino Uno calculates the heading angle from 0° to 360°.
You need: Arduino Uno (1), HMC5883L Magnetometer Sensor (1), OLED Display (SSD1306, 128×64) (1), Connecting Wires, Power Supply (1). Software: Arduino IDE, Wire library, Adafruit GFX library, Adafruit SSD1306 library, HMC5883L library.
Yes. It is rated beginner and takes about 1–2 hours to build, using the Arduino Uno. The guide on this page explains every connection and step.
The complete, tested Arduino C++ code is free on this page. Copy it into Arduino IDE and upload it to the board.
Yes. IRI sells a kit with the parts for this project (PRJ-139). Message IRI on WhatsApp at +91 86060 82634 for the kit price and delivery details.