Embedded SystemsBeginnerPRJ-139

Digital Compass System Using Arduino

Shows real-time heading and direction on an OLED using an HMC5883L magnetometer.

Built and tested by Anju.A · Industrial Robotics Institute

Circuit diagram of the Digital Compass System using Arduino Uno
Circuit diagram · Arduino Uno
LevelBeginner
Build time1–2 hours
BoardArduino Uno
Parts5 components

What is the Digital Compass System?

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.

How it works

  1. The HMC5883L measures magnetic field values along the X and Y axes.
  2. The Arduino computes the heading using atan2(Y, X) and converts it from radians to degrees.
  3. Negative angles are corrected by adding 360° to give a 0–360° heading.
  4. The angle is mapped to North (315–45°), East (45–135°), South (135–225°) or West.
  5. Heading and direction are refreshed on the OLED every 0.5 seconds.

Parts list

ComponentQty
Arduino Uno1
HMC5883L Magnetometer Sensor1
OLED Display (SSD1306, 128×64)1
Connecting WiresAs needed
Power Supply1

Software

  • Arduino IDE
  • Wire library
  • Adafruit GFX library
  • Adafruit SSD1306 library
  • HMC5883L library

Wiring and connections

FromConnect to
HMC5883L VCC3.3V / 5V
HMC5883L GNDGND
HMC5883L SDAA4 (SDA)
HMC5883L SCLA5 (SCL)
OLED VCC3.3V / 5V
OLED GNDGND
OLED SDAA4 (SDA)
OLED SCLA5 (SCL)

How to build the Digital Compass System

  1. Connect the HMC5883L to the Arduino I2C bus: SDA to A4, SCL to A5, plus VCC and GND.
  2. Connect the SSD1306 OLED to the same I2C bus (SDA, SCL) and to VCC and GND.
  3. Install the Adafruit GFX, Adafruit SSD1306 and HMC5883L libraries in the Arduino IDE.
  4. Upload the code to the Arduino Uno.
  5. Hold the sensor level and rotate it to see the heading angle and direction change on the OLED.

Digital Compass System code

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);
}

Real-world uses

  • Navigation
  • Robotics orientation
  • Real-time direction finding

Frequently asked questions

What is a Digital Compass System?

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°.

What components are needed for the Digital Compass System?

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.

Is the Digital Compass System project good for beginners?

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.

Where can I get the code for the Digital Compass System?

The complete, tested Arduino C++ code is free on this page. Copy it into Arduino IDE and upload it to the board.

Can I buy a kit for the Digital Compass System?

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.

Industrial Robotics Institute
Logo