Built-in project · Arduino Uno R3

GY-271 compass on an Arduino Uno

A QMC5883L compass on a GY-271 board reads the Earth’s field, and the serial monitor prints the azimuth and the compass point four times a second. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $30. On Mokxi they're free.

GY-271 compass · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Sensors
  • School
  • Using the Compass
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

A QMC5883L compass on a GY-271 board reads the Earth’s field, and the serial monitor prints the azimuth and the compass point four times a second.

Arduino Uno R3 runs the sketch below. The circuit around it: Compass, QMC5883L (GY-271). delay waits 250 milliseconds. Serial.begin opens the serial port at 9600 baud.

Parts list

3 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Compass, QMC5883L (GY-271) pin SCLArduino Uno R3 pin SCL
Compass, QMC5883L (GY-271) pin SDAArduino Uno R3 pin SDA
Compass, QMC5883L (GY-271) pin GNDArduino Uno R3 pin GND
Compass, QMC5883L (GY-271) pin VCC5 V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A GY-271 compass (QMC5883L) on the Uno's I2C bus.
//
// The QMC5883L measures the magnetic field along X, Y and Z. Flat on a desk,
// the Earth's field points north (and down), so the angle of the two flat
// readings, atan2(y, x), is the way the board's X arrow faces: its azimuth.
//
//   GY-271  VCC -> 5V, GND -> GND, SCL -> SCL (A5), SDA -> SDA (A4)
//
// Four times a second the sketch prints the azimuth in degrees and the
// compass point it falls in. Drag the heading slider while it runs.
//
// THINGS TO TRY. Tip the board with the pitch slider: the heading drifts,
// because the flat readings now carry part of the field's downward pull.
// That is why phones use an accelerometer to correct their compass.

#include <Wire.h>
#include <QMC5883LCompass.h>

QMC5883LCompass compass;

void setup() {
  Serial.begin(9600);
  compass.init();
  Serial.println("GY-271 compass at 0x0D");
}

void loop() {
  compass.read();
  int azimuth = compass.getAzimuth();
  char dir[3];
  compass.getDirection(dir, azimuth);

  Serial.print("azimuth ");
  Serial.print(azimuth);
  Serial.print("  ");
  for (int i = 0; i < 3; i++) {
    if (dir[i] != ' ') Serial.print(dir[i]);
  }
  Serial.println();
  delay(250);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once when the board starts. It gets the pins and libraries ready.

void setup() {
  Serial.begin(9600);
  compass.init();
  Serial.println("GY-271 compass at 0x0D");
}

loop sketch.ino

Runs over and over for as long as the board has power. This is where the project does its work.

void loop() {
  compass.read();
  int azimuth = compass.getAzimuth();
  char dir[3];
  compass.getDirection(dir, azimuth);

  Serial.print("azimuth ");
  Serial.print(azimuth);
  Serial.print("  ");
  for (int i = 0; i < 3; i++) {
    if (dir[i] != ' ') Serial.print(dir[i]);
  }
  Serial.println();
  delay(250);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.