Build it step by step
0 of 7 doneWire the compass
Connect I2C and print raw X and Y on the serial monitor.
Check the module type
If nothing reads, try the HMC5883L library.
Add the ring
Wire the data line to D0 and test with a colour fill.
Calibrate
After power-up, turn the board slowly through a full circle for 10 seconds.
Test north
Check the lit LED against a phone compass.
Turn the offset
Shift the LED index if north is at the wrong place.
Fit it
Mount in a bracelet with the compass level on the wrist.
How do you build a compass bracelet?
A ring of LEDs on your wrist lights the north point. You need Seeed XIAO RP2040, HMC5883L / QMC5883L compass module, WS2812B ring 16 LEDs and USB A to micro-USB cable (data). It takes 7 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 4
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Compass VCC | XIAO 3V3 | Power |
| Compass GND | XIAO GND | Ground |
| Compass SDA | XIAO D4 | I2C data |
| Compass SCL | XIAO D5 | I2C clock |
| LED ring VCC | XIAO 5V | Power |
| LED ring GND | XIAO GND | Ground |
| LED ring DIN | XIAO D0 | Data line |
Code
The compass chip senses the Earth's magnetic field in two axes; the code subtracts the middle of the measured range and evens out the scale so that a full turn gives a circle, which is the usual quick calibration. Calibrate away from iron, magnets and speakers, and hold the board level, because this simple version does not correct for tilt. The LED that is lit points away from the front of the board, so mount the ring and compass in a known orientation and turn the code's led offset if north appears at a different place. The XIAO RP2040 has no battery charger, so power it from a USB power bank while developing; a wearable version would need a protected Li-po with a charger and boost module, which this guide does not cover. Keep the ring brightness low to save power and avoid glare.
Runs on a virtual Seeed XIAO RP2040 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Libraries: QMC5883LCompass (by MPrograms), Adafruit NeoPixel. Many modules sold as HMC5883L are really QMC5883L.
#include <Wire.h>
#include <QMC5883LCompass.h>
#include <Adafruit_NeoPixel.h>
QMC5883LCompass compass;
Adafruit_NeoPixel ring(16, D0, NEO_GRB + NEO_KHZ800);
int xmin = 32767, xmax = -32768, ymin = 32767, ymax = -32768;
void setup() {
Serial.begin(115200);
Wire.begin();
compass.init();
ring.begin(); ring.setBrightness(25);
// Calibration: for 10 seconds turn the board slowly through all directions, flat
ring.fill(ring.Color(0, 0, 40)); ring.show();
unsigned long t = millis();
while (millis() - t < 10000) {
compass.read();
int x = compass.getX(), y = compass.getY();
xmin = min(xmin, x); xmax = max(xmax, x);
ymin = min(ymin, y); ymax = max(ymax, y);
delay(20);
}
ring.clear(); ring.show();
}
void loop() {
compass.read();
float x = compass.getX() - (xmax + xmin) / 2.0; // remove the fixed offset
float y = compass.getY() - (ymax + ymin) / 2.0;
x /= (xmax - xmin) / 2.0; y /= (ymax - ymin) / 2.0; // even out the two axes
float heading = atan2(y, x) * 180.0 / PI;
if (heading < 0) heading += 360;
int led = ((int)((heading + 11.25) / 22.5)) % 16; // 16 LEDs, 22.5 degrees each
ring.clear();
ring.setPixelColor(led, ring.Color(120, 0, 0)); // north in red
ring.setPixelColor((led + 8) % 16, ring.Color(0, 0, 40)); // south in dim blue
ring.show();
delay(100);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Facing north | Red LED at the north position |
| Turning clockwise | Red LED moves round smoothly |
| Near a magnet | Direction wrong |
| After recalibrating | Reading matches the phone compass within about 10 degrees |
If it does not work
All zeros
The module may be a different chip; check the library.
North is wrong
Check the orientation and the calibration turn.
LED jumps
Keep metal and cables away from the module.
No LEDs
Check the data line and the 5 V supply.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the compass bracelet?
Seeed XIAO RP2040, HMC5883L / QMC5883L compass module, WS2812B ring 16 LEDs and USB A to micro-USB cable (data).
How long does the compass bracelet take to build?
About 120 minutes for a beginner maker, in 7 steps.
How do I know the compass bracelet works?
Facing north: Red LED at the north position. Turning clockwise: Red LED moves round smoothly. Near a magnet: Direction wrong.
All zeros: what should I check?
The module may be a different chip; check the library.
North is wrong: what should I check?
Check the orientation and the calibration turn.
LED jumps: what should I check?
Keep metal and cables away from the module.
No LEDs: what should I check?
Check the data line and the 5 V supply.
