Menu
Microbots
0
  • Robots
    • ProtoBot
    • CubeBot
    • Robot Parts
  • Modules & Parts
    • CodeCell ESP32 Modules
    • Motors & Actuators
    • LiPo Batteries
  • Gear
    • Tools
    • Merch
  • Learn
    • User Guide & Tutorials
    • DIY Projects & Files
    • Education Discounts
  • About
    • Our Story
    • Reach Out
    • FAQs
  • Sign in
  • Your Cart is Empty
Microbots
  • Robots
    • ProtoBot
    • CubeBot
    • Robot Parts
  • Modules & Parts
    • CodeCell ESP32 Modules
    • Motors & Actuators
    • LiPo Batteries
  • Gear
    • Tools
    • Merch
  • Learn
    • User Guide & Tutorials
    • DIY Projects & Files
    • Education Discounts
  • About
    • Our Story
    • Reach Out
    • FAQs
  • 0 0

CodeCell: Reading Acceleration & Shaking Detection

CodeCell Accelerometer

CodeCell includes a BNO085 3-axis accelerometer that measures movement and orientation in space. In this guide, you’ll learn how to read acceleration values and detect shaking — triggering the onboard LED whenever motion is detected.

How It Works

An accelerometer measures changes in speed and direction (acceleration) across three axes:

  • X-axis → Left and right
  • Y-axis → Forward and backward
  • Z-axis → Up and down (gravity)

Even when still, CodeCell feels gravity, the Z-axis will show about 9.81 m/s², Earth’s gravitational acceleration.

To start reading motion data, initialize and call:

myCodeCell.Init(MOTION_ACCELEROMETER);          // Enable accelerometer
myCodeCell.Motion_AccelerometerRead(x, y, z);   // Read acceleration values

Example 1 – Read Real-Time Motion

This example prints live acceleration data for each axis. Try moving or tilting CodeCell to see how values change.


#include <CodeCell.h>

CodeCell myCodeCell;
float x = 0.0, y = 0.0, z = 0.0;

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

void loop() {
  if (myCodeCell.Run(10)) {               // Run loop at 10 Hz
    myCodeCell.Motion_AccelerometerRead(x, y, z);

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

Example 2 – Detect Shaking

This example calculates total acceleration and turns the LED red when CodeCell is shaken above a set threshold.


#include <CodeCell.h>
#include <math.h>  // For sqrt()

CodeCell myCodeCell;
float x = 0.0, y = 0.0, z = 0.0;

const float SHAKE_THRESHOLD = 15.0;  // Adjust for sensitivity

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

void loop() {
  if (myCodeCell.Run(10)) {               // Run at 10 Hz
    myCodeCell.Motion_AccelerometerRead(x, y, z);

    // Calculate total acceleration magnitude
    float totalAcceleration = sqrt(x * x + y * y + z * z);
    Serial.print("Total Acceleration: ");
    Serial.println(totalAcceleration);

    if (totalAcceleration > SHAKE_THRESHOLD) {
      Serial.println("Shake detected!");
      myCodeCell.LED(255, 0, 0);          // Red LED for shake
      delay(500);
    }
  }
}

By combining all three axes into a single totalAcceleration value, you can easily detect shakes in any direction. If it exceeds 15.0 m/s², the onboard LED lights up red for 0.5 seconds.

Customization Tips

  • Change LED behavior: Use color or brightness to indicate motion strength.
  • Detect tilts or knocks: Use X/Y/Z thresholds to create gesture-based actions.
  • Track movement: Attach CodeCell to a wearable to measure motion patterns.

 

  • 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

87 reviews
Write a review
87%
(76)
5%
(4)
2%
(2)
2%
(2)
3%
(3)
81
21
A
CodeCell C3
Anonymous

Perfect product!

A
ProtoBot
Anonymous

This is a great product for beginners who want hands-on experience with Arduino and 3D printing. The Protobot has a sleek, solid design, and the software is just as polished. I’ve bought several educational robotics kits, and this is the best one so far. Looking forward to more awesome products!

Thank you so much for your kind review! 😊

F
ProtoBot
Francis van Uyten

Really love this robot. I like that is simple to assemble and I like that you guys prepares it easy to start programming with examples and libraries. My opinion on what to improve is more like a add-on, can you guys develop a localisation sensor or function so it will know where the bot is. Expecting only great things coming from you guys. Succes

Thank you so much for your thoughtful review! Glad you’re enjoying ProtoBot 😊 A localisation sensor is a great suggestion! The I²C connection lets you add compatible sensors, so there’s room to explore this. Thanks for your support!

G
Experiment in Progress Mug
Gerald Kendrick

I am enjoying my Experiment in Progress coffee mug. It fits in well in the lab.

Hi Gerald, we’re so glad you’re enjoying it! Thanks for sharing your kind review.

K
12-in-1 Screwdriver (Pen-Size)
Klaas

Very useful fun little screwdriver for the all tiny stuff

Hi Klaas, thanks for the great review! We’re glad you’re enjoying your screwdriver!