Menu
Microbots
0
  • Learn
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • ProtoBot
    • Modules & Parts
    • Tools & Gears
    • Coming Soon
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Sign in
  • English
  • Your Cart is Empty
Microbots
  • Learn
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • ProtoBot
    • Modules & Parts
    • Tools & Gears
    • Coming Soon
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Language

  • 0 0

CodeCell: Using the Magnetometer to Build a Digital Compass

CodeCell includes a built-in BNO085 3-axis magnetometer, allowing it to sense magnetic fields and determine direction, just like a digital compass. 

How It Works

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

  • X-axis → Left/right magnetic strength
  • Y-axis → Forward/backward magnetic strength
  • Z-axis → Up/down magnetic strength

These values can be read directly from CodeCell using:

myCodeCell.Motion_MagnetometerRead(x, y, z);

To enable the sensor, initialize magnetometer mode with:

myCodeCell.Init(MOTION_MAGNETOMETER);

Once initialized, CodeCell continuously outputs raw magnetic data that can be used to calculate heading or detect magnetic interference.

Example 1 – Read Magnetometer Data

This example prints live magnetic field readings (X, Y, Z) to the Serial Monitor.


#include <CodeCell.h>

CodeCell myCodeCell;

float x = 0.0, y = 0.0, z = 0.0;

void setup() {
  Serial.begin(115200);
  myCodeCell.Init(MOTION_MAGNETOMETER);  // Initialize magnetometer
}

void loop() {
  if (myCodeCell.Run(10)) {              // Run loop at 10 Hz
    myCodeCell.Motion_MagnetometerRead(x, y, z);

    Serial.print("Mag X: "); Serial.print(x);
    Serial.print("  Y: "); Serial.print(y);
    Serial.print("  Z: "); Serial.println(z);
  }
}

As you rotate or tilt the CodeCell, the X, Y, and Z values will change — helping you visualize magnetic direction and strength.

Example 2 – Digital Compass (Calculate Heading)

This example converts magnetometer readings into a compass heading (0°–360°) using the atan2() function.


#include <CodeCell.h>
#include <math.h>  // Required for atan2()

CodeCell myCodeCell;
float x = 0.0, y = 0.0, z = 0.0;

void setup() {
  Serial.begin(115200);
  myCodeCell.Init(MOTION_MAGNETOMETER);
}

void loop() {
  if (myCodeCell.Run(10)) {
    myCodeCell.Motion_MagnetometerRead(x, y, z);

    // Calculate heading in degrees
    float heading = atan2(y, x) * (180.0 / M_PI);
    if (heading < 0) heading += 360;

    Serial.print("Compass Heading: ");
    Serial.println(heading);
  }
}

The heading represents direction in degrees:

Heading (°) Direction
0° (or 360°) North ↑
90° East →
180° South ↓
270° West ←

Example 3 – Visualize Direction with LED

Here, the onboard LED changes color depending on which direction CodeCell is facing:


#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);
}

void loop() {
  if (myCodeCell.Run(10)) {
    myCodeCell.Motion_MagnetometerRead(x, y, z);

    float heading = atan2(y, x) * (180.0 / M_PI);
    if (heading < 0) heading += 360;

    // LED color by direction
    if (heading >= 315 || heading < 45) {
      myCodeCell.LED(0, 0, 255);    // Blue → North
    } else if (heading >= 45 && heading < 135) {
      myCodeCell.LED(0, 255, 0);    // Green → East
    } else if (heading >= 135 && heading < 225) {
      myCodeCell.LED(255, 0, 0);    // Red → South
    } else {
      myCodeCell.LED(255, 255, 0);  // Yellow → West
    }

    Serial.printf("Heading: %.1f°\n", heading);
  }
}

Customization Tips

  • OLED Display: Show live direction text (N, E, S, W) or angle readings.
  • Magnetic Field Detection: Use raw values to sense nearby magnets or coils.
  • Combine with Motion: Fuse magnetometer and gyroscope data for smooth orientation tracking.
  • Share:

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!

© 2026 Microbots.

★ Reviews

Let customers speak for us

74 reviews
Write a review
85%
(63)
5%
(4)
3%
(2)
3%
(2)
4%
(3)
68
21
J
ProtoBot
Justin Tzitzis

greatest flexible pcb robot out there

G
ProtoBot
Gennadii

Got a Pro version, it's very easy to put together, and it works great.
Would be great to have brushless motors and more GPIO access through the plastic shell in ProtoBot v2

A
ProtoBot
Armand

It is a compact, very well designed RC-like robotic car with endless possibilities for those with active imaginations. Inspirational and creative in a small form factor. Innovate and enjoy!

User picture
J
ProtoBot
Javier

I Recieved yesterday my ProtoBot and the quality and details are awesome!

User picture
C
CodeCell C3
Cloke74

Great piece of kit, had just what i needed to complete the project i had in mind. Shame shipping to the UK is so expensive, but appreciate this isn’t necessarily in the hands of MicroBots

123