Menu
Microbots
0
  • Learn
  • Shop
    • Maker-Modules
    • Maker-Packs
    • Tools & Gears
    • Robots & Displays
    • All Products
  • Community
    • Education
    • Software
  • About
    • Our Story
    • Reach Out
    • FAQs
  • English
  • Your Cart is Empty
Microbots
  • Learn
  • Shop
    • Maker-Modules
    • Maker-Packs
    • Tools & Gears
    • Robots & Displays
    • All Products
  • Community
    • Education
    • Software
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Language

  • 0 0

CodeCell: Using the Magnetometer to Build a Digital Compass

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! 

What is a Magnetometer?

A magnetometer measures the Earth's magnetic field along three axes:

  • X-axis → Measures left-right magnetic field strength.
  • Y-axis → Measures forward-backward magnetic field strength.
  • Z-axis → Measures up-down magnetic field strength.

By using these values, we can calculate the compass heading (0° to 360°) and determine which direction CodeCell is pointing.

Step 1: Read Magnetometer Data from CodeCell

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?

  • If you rotate CodeCell, X and Y values will change based on direction.
  • If you tilt CodeCell, the Z value will change.
  • If you place CodeCell near a magnet, all values will fluctuate dramatically.

Step 2: Convert Magnetometer Data into a Compass Heading

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!

Step 3: Show Direction with the Onboard LED

We can now light up the LED in different colors based on direction:

  • 🔵 North (0° - 45° & 315° - 360°) → Blue LED
  • 🟢 East (45° - 135°) → Green LED
  • 🔴 South (135° - 225°) → Red LED
  • 🟡 West (225° - 315°) → Yellow LED
#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
        }
    }
}

Experiment with Your Own Ideas!

Now that you can read magnetometer data and calculate direction, try:

  • Displaying the direction (N, E, S, W) on an OLED screen.
  • Detecting magnetic field when near coils or magnets.
  • Combining with other sensors

 

  • Share:


Also in CodeCell

Linking Your CodeCell to the MicroLink App
Linking Your CodeCell to the MicroLink App

Read More

Debugging CodeCell
Debugging CodeCell

Read More

CodeCell: Connecting with BLE
CodeCell: Connecting with BLE

Read More

Follow

Github

  • About
  • Software
  • Education
  • Contact
  • FAQs
  • Terms
  • Refund Policy
  • Privacy Policy

Join our Community ~ Be the first to know about new products and get exciting deals!

© 2025 Microbots.