I’m hoping to build a small portable compass ideally with at least an OLED showing the heading or even something exotic like this:https://www.instructables.com/Arduino-Analog-Digital-Compass-With-HMC5883L-Senso/ (yes I know its for a different sensor but the one I bought from eB*y was a fake so I thought I’d ‘treat myself’ to a proper one from Pimoroni.
Using the example code ‘compass’ from the Adafruit_MMC56x3.h library it compiles and uploads OK. But I get the following regardless of how I wave the sensor around (in X,Y & Z).
Which leaves me puzzled… Ultimately as its using I2C I’d distance limited (its going up an aerial mast) so a serial link down to another Nano with an OLED serial display of the numbers would work for me. Links to any functioning compass code gratefully received…
Gareth
#include <Adafruit_MMC56x3.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_MMC5603 mag = Adafruit_MMC5603(12345);
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit_MMC5603 Magnetometer Compass");
Serial.println("");
/* Initialise the sensor */
if (!mag.begin(MMC56X3_DEFAULT_ADDRESS, &Wire)) { // I2C mode
/* There was a problem detecting the MMC5603 ... check your connections */
Serial.println("Ooops, no MMC5603 detected ... Check your wiring!");
while (1) delay(10);
}
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
float Pi = 3.14159;
// Calculate the angle of the vector y,x
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
Serial.print("Compass Heading: ");
Serial.println(heading);
delay(500);
}
I can't see it should do anything iother than oputput 0-359 into the serial monitor....
Gareth
I’d add some debug to print out the x/y (and possibly z) values, which might clarify if there’s a problem with the data or if the sensor is in an unexpected orientation :-)