Ever wondered how your phone knows which direction you’re facing? It’s all thanks to a magnetometer! Your CodeCell comes with a BNO085 3-axis magnetometer, which detects magnetic fields in three directions. This allows you to measure heading (direction), detect magnetic interference, and even create a simple digital compass!
A magnetometer measures the Earth's magnetic field along three axes:
By using these values, we can calculate the compass heading (0° to 360°) and determine which direction CodeCell is pointing.
Let’s start by initializing the magnetometer and reading raw magnetic field values.
#include <CodeCell.h>
CodeCell myCodeCell;
float x = 0.0; // Magnetic field on X-axis
float y = 0.0; // Magnetic field on Y-axis
float z = 0.0; // Magnetic field on Z-axis
void setup() {
Serial.begin(115200);
myCodeCell.Init(MOTION_MAGNETOMETER); // Initialize the magnetometer
}
void loop() {
if (myCodeCell.Run(10)) { // Read at 10Hz (every 100ms)
myCodeCell.Motion_MagnetometerRead(x, y, z); // Get magnetometer readings
Serial.print("Mag X: "); Serial.print(x);
Serial.print(" Y: "); Serial.print(y);
Serial.print(" Z: "); Serial.println(z);
}
}
Once you upload the code and open the Serial Monitor, you’ll see values like:
Mag X: 12.4 Y: -7.8 Z: 35.2
What Do These Values Mean?
To build a digital compass, we need to convert X and Y values into an angle using the atan2() function.
#include <CodeCell.h>
#include <math.h> // Needed for atan2 function
CodeCell myCodeCell;
float x = 0.0, y = 0.0, z = 0.0;
void setup() {
Serial.begin(115200);
myCodeCell.Init(MOTION_MAGNETOMETER); // Initialize the magnetometer
}
void loop() {
if (myCodeCell.Run(10)) { // Read at 10Hz
myCodeCell.Motion_MagnetometerRead(x, y, z); // Read magnetometer values
// Calculate heading (angle in degrees)
float heading = atan2(y, x) * (180.0 / M_PI);
// Ensure heading is in the range 0° - 360°
if (heading < 0) {
heading += 360;
}
Serial.print("Compass Heading: ");
Serial.println(heading); // Print heading in degrees
}
}
The heading value (0° to 360°) tells us which direction CodeCell is pointing:
Heading (°) | Direction |
---|---|
0° (or 360°) | North ↑ |
90° | East → |
180° | South ↓ |
270° | West ← |
Try rotating CodeCell and watch the heading change!
We can now light up the LED in different colors based on direction:
#include <CodeCell.h>
#include <math.h>
CodeCell myCodeCell;
float x = 0.0, y = 0.0, z = 0.0;
void setup() {
Serial.begin(115200);
myCodeCell.Init(MOTION_MAGNETOMETER); // Initialize the magnetometer
}
void loop() {
if (myCodeCell.Run(10)) { // Read at 10Hz
myCodeCell.Motion_MagnetometerRead(x, y, z); // Read magnetometer values
// Calculate heading
float heading = atan2(y, x) * (180.0 / M_PI);
if (heading < 0) heading += 360;
Serial.print("Heading: ");
Serial.println(heading);
// Change LED color based on heading
if (heading >= 315 || heading < 45) {
myCodeCell.LED(0, 0, 255); // Blue for North
} else if (heading >= 45 && heading < 135) {
myCodeCell.LED(0, 255, 0); // Green for East
} else if (heading >= 135 && heading < 225) {
myCodeCell.LED(255, 0, 0); // Red for South
} else {
myCodeCell.LED(255, 255, 0); // Yellow for West
}
}
}
Now that you can read magnetometer data and calculate direction, try:
Join our Community ~ Be the first to know about new products and get exciting deals!
© 2025 Microbots.