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

37 reviews
Write a review
143%
(53)
8%
(3)
3%
(1)
5%
(2)
8%
(3)
21
35
F
CodeCell C6
Francisco Estivallet

Amazing hardware, my go to for compact projects.

User picture
M
CodeCell C6 Drive
Mohamed Karkouchia

Very god product to buyand test

A
CodeCell C6 Drive
Anonymous

Module looks absoluty perfect and cute.So such a small device has so many funtions. Dont have time to test full pontencial but definitely will order again

N
CoilPad
NileshRahate

How can I order this to India ..
Or can I get ready gerber to load on pcb way...
Or if have to purchase .. can purchase , pls send the link how to purchase .

A
MotorCell
Anonymous

Integrated Driver in such small size, unbeatable.

123