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

68 reviews
Write a review
84%
(57)
6%
(4)
3%
(2)
3%
(2)
4%
(3)
63
21
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

A
CodeCell C6
Anonymous

I had an issue, got a red light, I used too much flux. Support said clean it, then the one sensor worked fine. I got the help and answer same day I provided a foto.

A
CodeCell C6 Drive
Anonymous

I think this is the best of the ESP offered, most versatile.

User picture
P
CodeCell C6
Prudhvi tej Chinimilli

Been testing the Microbots CodeCell C6 and honestly impressed with how much functionality they packed into such a tiny module. Great form factor for rapid prototyping wearable/embedded sensing applications. ESP32-C6 + IMU integration makes development much easier compared to building everything from scratch.

Still exploring battery optimization and compact LiPo options for our use case, but overall the platform is promising for low-cost real-time sensing systems. Excited to keep building with it.

F
CodeCell C6
Francisco Estivallet

Amazing hardware, my go to for compact projects.

User picture
123