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 Acceleration & Shaking Detection

Inside your CodeCell is a BNO085 3-axis accelerometer, a tiny sensor that measures movement. With just a few lines of code, you can detect shaking and even trigger an LED light when movement is detected! Let's break it down step by step.

What is an Accelerometer?

An accelerometer measures acceleration - how fast something is speeding up or slowing down. It detects:

  • Left & Right (X-axis)
  • Forward & Backward (Y-axis)
  • Up & Down (Z-axis)

It even feels gravity! When CodeCell is still, the Z-axis will show a value close to 9.81 m/s², which is Earth's gravity.

Step 1: Read Motion from CodeCell

First, let's start by reading real-time movement values from CodeCell's onboard accelerometer. This code will show how much CodeCell is moving in each direction:

#include <CodeCell.h>

CodeCell myCodeCell;

float x = 0.0;  // X-axis acceleration
float y = 0.0;  // Y-axis acceleration
float z = 0.0;  // Z-axis acceleration

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

void loop() {
  if (myCodeCell.Run(10)) {  // Run at 10 times per second (10Hz)
    myCodeCell.Motion_AccelerometerRead(x, y, z); // Read motion values
    Serial.print("X: "); Serial.print(x);
    Serial.print("  Y: "); Serial.print(y);
    Serial.print("  Z: "); Serial.println(z);
  }
}

What You’ll Notice:

  • When CodeCell is still, facing up, X and Y should be close to 0, and Z should be around 9.81 (gravity).
  • When you move CodeCell left or right, X changes.
  • When you move it forward or backward, Y changes.
  • When you shake it, all values jump around!

Step 2: Detect a Shaking 

Now, let’s detect when CodeCell is shaken and use the onboard RGB LED to signal it.

This program will turn the LED red if a shake is detected.

#include <CodeCell.h>
#include <math.h>  // Needed for square root calculations

CodeCell myCodeCell;

float x = 0.0, y = 0.0, z = 0.0;
const float SHAKE_THRESHOLD = 15.0;  // Adjust this value for sensitivity

void setup() {
    Serial.begin(115200);
    myCodeCell.Init(MOTION_ACCELEROMETER);  // Initialize accelerometer
}

void loop() {
    if (myCodeCell.Run(10)) { // Check every 100ms (10Hz)
        myCodeCell.Motion_AccelerometerRead(x, y, z); // Read accelerometer data

        // Calculate total movement strength (vector magnitude)
        float totalAcceleration = sqrt(x * x + y * y + z * z);

        Serial.print("Total Acceleration: ");
        Serial.println(totalAcceleration); // Show acceleration strength

        // If the acceleration is stronger than the threshold, it's a shake!
        if (totalAcceleration > SHAKE_THRESHOLD) {
            Serial.println("Shake detected!");
            myCodeCell.LED(255, 0, 0); // Turn LED red
            delay(500); // Keep LED on for 0.5 seconds
        } else {
            //Not shaking
        }
    }
}

Instead of checking X, Y, and Z separately, we combine them into a single value:

This tells us how much CodeCell is moving overall, no matter the direction. If totalAcceleration jumps above 15.0, we assume CodeCell is being shaken. The onboard RGB LED turns red for 0.5 seconds to signal the shake.

Experiment with Your Own Ideas!

Now that you can detect movement, here are some fun challenges:

  • Change LED colors based on movement (e.g., blue for tilting, red for shaking).
  • Use the accelerometer to detect taps or knocks.
  • Learn how CodeCell moves when attached to a robot or vehicle!

By understanding motion sensing, you're learning the same technology used in smartphones, drones, and rockets! 🚀


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