Menu
Microbots
0
  • Make
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • Maker-Modules
    • Maker-Packs
    • Tools & Gears
    • Robots & Displays
  • About
    • Our Story
    • Reach Out
    • FAQs
  • English
  • Your Cart is Empty
Microbots
  • Make
    • Getting Started
    • Maker Builds
    • Education
  • Shop
    • Maker-Modules
    • Maker-Packs
    • Tools & Gears
    • Robots & Displays
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Language

  • 0 0

CodeCell: Reading the Gyroscope

The CodeCell comes with a built-in BNO085 3-axis gyroscope, which can measure angular velocity - how fast CodeCell is rotating around each axis - making it easier to detect directional changes.

What is a Gyroscope?

Unlike an accelerometer that measures movement in a straight line, a gyroscope detects rotational motion. It tells you how fast CodeCell is spinning in degrees per second (°/s).

  • X-axis → Rotation around the left-right axis (pitch)
  • Y-axis → Rotation around the front-back axis (roll)
  • Z-axis → Rotation around the top-bottom axis (yaw)

 

Step 1: Read Gyroscope Data from CodeCell

Before we can use the gyroscope, we need to initialize it and start reading data.

This code reads the X, Y, and Z angular velocity values and prints them every 100ms.

#include <CodeCell.h>

CodeCell myCodeCell;

float x = 0.0;  // Angular velocity around X-axis
float y = 0.0;  // Angular velocity around Y-axis
float z = 0.0;  // Angular velocity around Z-axis

void setup() {
  Serial.begin(115200); 
  myCodeCell.Init(MOTION_GYRO); // Initialize the gyroscope
}

void loop() {
  if (myCodeCell.Run(10)) { // Read at 10Hz (every 100ms)
    myCodeCell.Motion_GyroRead(x, y, z); // Get gyro readings

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

What You'll Notice:

  • When CodeCell is still, all values should be close to 0.
  • If you tilt it forward or backward, X-axis values change.
  • If you twist it left or right, Y-axis values change.
  • If you rotate it like a steering wheel, Z-axis values change.

The higher the value, the faster the rotation.

Step 2: Detect Rotation & Change LED Color

Now, let's detect when CodeCell rotates left or right and change the RGB LED color accordingly.

This program:

  • Turns the LED green when rotated to the right (positive Z).
  • Turns the LED red when rotated to the left (negative Z).
#include <CodeCell.h>

CodeCell myCodeCell;

float x = 0.0, y = 0.0, z = 0.0;
const float ROTATION_THRESHOLD = 3.0; // Rotation speed threshold (°/s)

void setup() {
    Serial.begin(115200);
    myCodeCell.Init(MOTION_GYRO);  // Initialize gyroscope
}

void loop() {
    if (myCodeCell.Run(10)) { // Read at 10Hz
        myCodeCell.Motion_GyroRead(x, y, z); // Get gyroscope values

        Serial.print("Gyro Z: ");
        Serial.println(z); // Print rotation speed

        if (z > ROTATION_THRESHOLD) { 
            myCodeCell.LED(0, 255, 0); // Turn LED green(rotating right)
        } 
        else if (z < -ROTATION_THRESHOLD) {
            myCodeCell.LED(255, 0, 0); // Turn LED red (rotating left)
        } 
        else {
            myCodeCell.LED(0, 0, 0); // Turn LED off when not rotating
        }
    }
}

The gyroscope measures angular velocity in degrees per second (°/s). If the value of Z rotation (yaw) exceeds the threshold, we assume CodeCell is being rotated.

  • Z > 3 → Rotating right, LED turns green.
  • Z < -3 → Rotating left, LED turns red.

Try changing ROTATION_THRESHOLD to increase or decrease the sensitivity.

  • Higher value = Detects only fast rotations
  • Lower value = Detects even small rotations

Experiment with Your Own Ideas!

Now that you can track rotation, try:

  • Using different LED colors for different axes
  • Detecting quick flicks or gestures
  • Learn and Tracking rotation over time 

 

  • 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!

© 2025 Microbots.