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.
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).
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:
The higher the value, the faster the rotation.
Now, let's detect when CodeCell rotates left or right and change the RGB LED color accordingly.
This program:
#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.
Try changing ROTATION_THRESHOLD
to increase or decrease the sensitivity.
Now that you can track rotation, try:
Join our Community ~ Be the first to know about new products and get exciting deals!
© 2025 Microbots.