Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,42 @@ void setup()

void loop()
{
unsigned int currentX = 0;
unsigned int currentY = 0;
unsigned int currentZ = 0;
uint32_t currentX = 0;
uint32_t currentY = 0;
uint32_t currentZ = 0;
double normalizedX = 0;
double normalizedY = 0;
double normalizedZ = 0;

// This reads the X, Y and Z channels consecutively
// (Useful if you have one or more channels disabled)
currentX = myMag.getMeasurementX();
currentY = myMag.getMeasurementY();
currentZ = myMag.getMeasurementZ();

// Or, we could read all three simultaneously
//myMag.getMeasurementXYZ(&currentX, &currentY, &currentZ);

Serial.print("X axis raw value: ");
Serial.print(currentX);
Serial.print("\tY axis raw value: ");
Serial.print(currentY);
Serial.print("\tZ axis raw value: ");
Serial.println(currentZ);

// The magnetic field values are 18-bit unsigned. The zero (mid) point is 2^17 (131072).
// Normalize each field to +/- 1.0
normalizedX = (double)currentX - 131072.0;
normalizedX /= 131072.0;
normalizedY = (double)currentY - 131072.0;
normalizedY /= 131072.0;
normalizedZ = (double)currentZ - 131072.0;
normalizedZ /= 131072.0;

// The magnetometer full scale is +/- 8 Gauss
// Multiply the normalized values by 8 to convert to Gauss
Serial.print("X axis field (Gauss): ");
Serial.print(normalizedX * 8, 5);
Serial.print(normalizedX * 8, 5); // Print with 5 decimal places

Serial.print("\tY axis field (Gauss): ");
Serial.print(normalizedY * 8, 5);
Expand All @@ -88,4 +97,4 @@ void loop()

Serial.println();
delay(100);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,52 +43,39 @@ void setup()

void loop()
{
unsigned int rawValue = 0;
double heading = 0;
uint32_t rawValueX = 0;
uint32_t rawValueY = 0;
uint32_t rawValueZ = 0;
double normalizedX = 0;
double normalizedY = 0;
double normalizedZ = 0;
double heading = 0;

// Read all three channels simultaneously
myMag.getMeasurementXYZ(&rawValueX, &rawValueY, &rawValueZ);

rawValue = myMag.getMeasurementX();
normalizedX = (double)rawValue - 131072.0;
// The magnetic field values are 18-bit unsigned. The zero (mid) point is 2^17 (131072).
// Normalize each field to +/- 1.0
normalizedX = (double)rawValueX - 131072.0;
normalizedX /= 131072.0;

rawValue = myMag.getMeasurementY();
normalizedY = (double)rawValue - 131072.0;
normalizedY = (double)rawValueY - 131072.0;
normalizedY /= 131072.0;

rawValue = myMag.getMeasurementZ();
normalizedZ = (double)rawValue - 131072.0;
normalizedZ = (double)rawValueZ - 131072.0;
normalizedZ /= 131072.0;

// Magnetic north is oriented with the Y axis
if (normalizedY != 0)
{
if (normalizedX < 0)
{
if (normalizedY > 0)
heading = 57.2958 * atan(-normalizedX / normalizedY); // Quadrant 1
else
heading = 57.2958 * atan(-normalizedX / normalizedY) + 180; // Quadrant 2
}
else
{
if (normalizedY < 0)
heading = 57.2958 * atan(-normalizedX / normalizedY) + 180; // Quadrant 3
else
heading = 360 - (57.2958 * atan(normalizedX / normalizedY)); // Quadrant 4
}
}
else
{
// atan of an infinite number is 90 or 270 degrees depending on X value
if (normalizedX > 0)
heading = 270;
else
heading = 90;
}
// Convert the X and Y fields into heading using atan2 (Arc Tangent 2)
heading = atan2(normalizedX, 0 - normalizedY);

// atan2 returns a value between +PI and -PI
// Convert to degrees
heading /= PI;
heading *= 180;
heading += 180;

Serial.print("Heading: ");
Serial.println(heading, 1);
delay(10);
}
delay(100);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ SFE_MMC5983MA myMag;

int interruptPin = 2;

volatile bool newDataAvailable = false;
unsigned int rawValue = 0;
double heading = 0;
volatile bool newDataAvailable = true;
uint32_t rawValueX = 0;
uint32_t rawValueY = 0;
uint32_t rawValueZ = 0;
double normalizedX = 0;
double normalizedY = 0;
double normalizedZ = 0;
double heading = 0;

void setup()
{
Expand All @@ -39,8 +41,9 @@ void setup()

Wire.begin();

// Configure the interrupt pin for the "Measurement Done" interrupt
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), interruptRoutine, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin), interruptRoutine, RISING);

if (myMag.begin() == false)
{
Expand All @@ -53,14 +56,6 @@ void setup()

Serial.println("MMC5983MA connected");

Serial.print("Die temperature: ");
int celsius = myMag.getTemperature();
Serial.print(celsius);
Serial.print("°C or ");
float fahrenheit = (celsius * 9.0f / 5.0f) + 32.0f;
Serial.print((int)fahrenheit);
Serial.println("°F");

Serial.println("Setting filter bandwith to 100 Hz for continuous operation...");
myMag.setFilterBandwidth(100);
Serial.print("Reading back filter bandwith: ");
Expand All @@ -81,61 +76,54 @@ void setup()
Serial.print("Reading back continuous mode: ");
Serial.println(myMag.isContinuousModeEnabled() ? "enabled" : "disabled");

Serial.println("Enabling interrupt...");
myMag.enableInterrupt();
Serial.print("Reading back interrupt status: ");
Serial.println(myMag.isInterruptEnabled() ? "enabled" : "disabled");

// Set our interrupt flag, just in case we missed the rising edge
newDataAvailable = true;
}

void loop()
{
if (newDataAvailable == true)
{
rawValue = myMag.getMeasurementX();
normalizedX = (double)rawValue - 131072.0;
newDataAvailable = false; // Clear our interrupt flag
myMag.clearMeasDoneInterrupt(); // Clear the MMC5983 interrupt

// Read all three channels simultaneously
// Note: we are calling readFieldsXYZ to read the fields, not getMeasurementXYZ
// The measurement is already complete, we do not need to start a new one
myMag.readFieldsXYZ(&rawValueX, &rawValueY, &rawValueZ);

// The magnetic field values are 18-bit unsigned. The zero (mid) point is 2^17 (131072).
// Normalize each field to +/- 1.0
normalizedX = (double)rawValueX - 131072.0;
normalizedX /= 131072.0;

rawValue = myMag.getMeasurementY();
normalizedY = (double)rawValue - 131072.0;

normalizedY = (double)rawValueY - 131072.0;
normalizedY /= 131072.0;

rawValue = myMag.getMeasurementZ();
normalizedZ = (double)rawValue - 131072.0;

normalizedZ = (double)rawValueZ - 131072.0;
normalizedZ /= 131072.0;

// Magnetic north is oriented with the Y axis
if (normalizedY != 0)
{
if (normalizedX < 0)
{
if (normalizedY > 0)
heading = 57.2958 * atan(-normalizedX / normalizedY); // Quadrant 1
else
heading = 57.2958 * atan(-normalizedX / normalizedY) + 180; // Quadrant 2
}
else
{
if (normalizedY < 0)
heading = 57.2958 * atan(-normalizedX / normalizedY) + 180; // Quadrant 3
else
heading = 360 - (57.2958 * atan(normalizedX / normalizedY)); // Quadrant 4
}
}
else
{
// atan of an infinite number is 90 or 270 degrees depending on X value
if (normalizedX > 0)
heading = 270;
else
heading = 90;
}

// Convert the X and Y fields into heading using atan2 (Arc Tangent 2)
heading = atan2(normalizedX, 0 - normalizedY);

// atan2 returns a value between +PI and -PI
// Convert to degrees
heading /= PI;
heading *= 180;
heading += 180;

Serial.print("Heading: ");
Serial.println(heading, 1);

newDataAvailable = false;
myMag.enableInterrupt();
}
}

void interruptRoutine()
{
newDataAvailable = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,42 @@ void setup()

void loop()
{
unsigned int currentX = 0;
unsigned int currentY = 0;
unsigned int currentZ = 0;
uint32_t currentX = 0;
uint32_t currentY = 0;
uint32_t currentZ = 0;
double normalizedX = 0;
double normalizedY = 0;
double normalizedZ = 0;

// This reads the X, Y and Z channels consecutively
// (Useful if you have one or more channels disabled)
currentX = myMag.getMeasurementX();
currentY = myMag.getMeasurementY();
currentZ = myMag.getMeasurementZ();

// Or, we could read all three simultaneously
//myMag.getMeasurementXYZ(&currentX, &currentY, &currentZ);

Serial.print("X axis raw value: ");
Serial.print(currentX);
Serial.print("\tY axis raw value: ");
Serial.print(currentY);
Serial.print("\tZ axis raw value: ");
Serial.println(currentZ);

// The magnetic field values are 18-bit unsigned. The zero (mid) point is 2^17 (131072).
// Normalize each field to +/- 1.0
normalizedX = (double)currentX - 131072.0;
normalizedX /= 131072.0;
normalizedY = (double)currentY - 131072.0;
normalizedY /= 131072.0;
normalizedZ = (double)currentZ - 131072.0;
normalizedZ /= 131072.0;

// The magnetometer full scale is +/- 8 Gauss
// Multiply the normalized values by 8 to convert to Gauss
Serial.print("X axis field (Gauss): ");
Serial.print(normalizedX * 8, 5);
Serial.print(normalizedX * 8, 5); // Print with 5 decimal places

Serial.print("\tY axis field (Gauss): ");
Serial.print(normalizedY * 8, 5);
Expand All @@ -89,4 +98,4 @@ void loop()

Serial.println();
delay(100);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,39 @@ void setup()

void loop()
{
unsigned int rawValue = 0;
double heading = 0;
uint32_t rawValueX = 0;
uint32_t rawValueY = 0;
uint32_t rawValueZ = 0;
double normalizedX = 0;
double normalizedY = 0;
double normalizedZ = 0;
double heading = 0;

// Read all three channels simultaneously
myMag.getMeasurementXYZ(&rawValueX, &rawValueY, &rawValueZ);

rawValue = myMag.getMeasurementX();
normalizedX = (double)rawValue - 131072.0;
// The magnetic field values are 18-bit unsigned. The zero (mid) point is 2^17 (131072).
// Normalize each field to +/- 1.0
normalizedX = (double)rawValueX - 131072.0;
normalizedX /= 131072.0;

rawValue = myMag.getMeasurementY();
normalizedY = (double)rawValue - 131072.0;
normalizedY = (double)rawValueY - 131072.0;
normalizedY /= 131072.0;

rawValue = myMag.getMeasurementZ();
normalizedZ = (double)rawValue - 131072.0;
normalizedZ = (double)rawValueZ - 131072.0;
normalizedZ /= 131072.0;

// Magnetic north is oriented with the Y axis
if (normalizedY != 0)
{
if (normalizedX < 0)
{
if (normalizedY > 0)
heading = 57.2958 * atan(-normalizedX / normalizedY); // Quadrant 1
else
heading = 57.2958 * atan(-normalizedX / normalizedY) + 180; // Quadrant 2
}
else
{
if (normalizedY < 0)
heading = 57.2958 * atan(-normalizedX / normalizedY) + 180; // Quadrant 3
else
heading = 360 - (57.2958 * atan(normalizedX / normalizedY)); // Quadrant 4
}
}
else
{
// atan of an infinite number is 90 or 270 degrees depending on X value
if (normalizedX > 0)
heading = 270;
else
heading = 90;
}
// Convert the X and Y fields into heading using atan2 (Arc Tangent 2)
heading = atan2(normalizedX, 0 - normalizedY);

// atan2 returns a value between +PI and -PI
// Convert to degrees
heading /= PI;
heading *= 180;
heading += 180;

Serial.print("Heading: ");
Serial.println(heading, 1);
delay(100);
}
}
Loading