Menu
Microbots
0
  • Learn
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • Modules & Parts
    • Tools & Gears
    • Robots & Displays
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Sign in
  • English
  • Your Cart is Empty
Microbots
  • Learn
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • Modules & Parts
    • Tools & Gears
    • Robots & Displays
  • 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

52 reviews
Write a review
85%
(44)
4%
(2)
2%
(1)
4%
(2)
6%
(3)
21
48
L
MotorCell
Lennart Lange

Nice packaging, good follow up on the delivery, need to look up some online resources now for my first actual project wiht the cell

B
CodeCell C6 Drive
Brandon

Awesome product with great tutorials and example code

G
MotorCell
Gerald Kendrick

Very happy with my MotorCell. I'm incorporating it into a prototype project that will hopefully result in me needing a few more!

User picture
A
CodeCell C6 Drive
Anonymous

Great product! Having the IMU, motor driver, and battery management directly on the board is incredibly handy for quick prototyping. Love it!

Improvement ideas:
- using an ESP32 other than the C6 to get more cores. On a single-core chip, WiFi tasks often interfere with real-time applications.
- adding two more motor drivers (with a slightly higher current rating) would be awesome for drone projects!
- I know the compactness of the board is a huge selling point and really optimised, but exposing a few more pins would be great. With the motor drivers already occupying 4 pins, having only 4 GPIOs left can be tight for complex projects (though I’m nitpicking, I’m really pushing this board to its limits!).

A
CodeCell C6
Anonymous

I use CodeCell C6, like all the Features, and compactness. The remote Link to the iPhone with some Controls for 2 Motors is just perfect for the job.

123