In this guide, we'll explore how to use the CodeCell to measure its rotation directly by reading the roll, pitch, and yaw angles. Understanding these angles is crucial in applications like robotics, drones, and motion tracking, where precise orientation data is needed.
CodeCell is equipped with a BNO085 motion sensor, which fuses data from an accelerometer, gyroscope, and magnetometer to compute rotation vectors. These vectors help determine the device's orientation in space in terms of roll, pitch, and yaw:
By continuously monitoring these values, CodeCell provides real-time orientation feedback, making it useful for a variety of motion-based applications.
To read the rotation angles, follow this simple example. This code initializes CodeCell's motion sensor and continuously prints the roll, pitch, and yaw values to the serial monitor.
#include <CodeCell.h>
CodeCell myCodeCell;
float Roll = 0.0;
float Pitch = 0.0;
float Yaw = 0.0;
void setup() {
Serial.begin(115200); // Set Serial baud rate to 115200. Ensure Tools/USB_CDC_On_Boot is enabled if using Serial.
myCodeCell.Init(MOTION_ROTATION); // Initializes Rotation Sensing
}
void loop() {
if (myCodeCell.Run(10)) { // Run every 10Hz
myCodeCell.Motion_RotationRead(Roll, Pitch, Yaw);
Serial.printf("Roll: %.2f°, Pitch: %.2f°, Yaw: %.2f°\n", Roll, Pitch, Yaw);
}
}
Explanation of the Code:
Initialize the CodeCell motion sensor to read rotation values.
Run the sensor at 10Hz (every 100ms) to get fresh data.
Print the roll, pitch, and yaw angles in degrees to the serial monitor.
This provides an accurate way to measure the orientation of CodeCell in real time.
One practical application of reading roll, pitch, and yaw is controlling a servo motor based on CodeCell's orientation. The following example maps the pitch angle to the servo’s position.
#include <CodeCell.h>
#include <ESP32Servo.h>
CodeCell myCodeCell;
Servo myservo;
float Roll = 0.0;
float Pitch = 0.0;
float Yaw = 0.0;
int servo_angle = 0;
void setup() {
Serial.begin(115200); // Set Serial baud rate to 115200
myCodeCell.Init(MOTION_ROTATION); // Initializes rotation sensing
myservo.attach(1); // Attaches the servo on pin 1 to the servo object
}
void loop() {
if (myCodeCell.Run(10)) {
// Read rotation angles from the BNO085 sensor
myCodeCell.Motion_RotationRead(Roll, Pitch, Yaw);
// Convert the pitch angle to a servo angle
servo_angle = abs((int)Pitch);
servo_angle = (180 - servo_angle);
// Limit the servo angle to the range 0-60 degrees
if (servo_angle > 60) {
servo_angle = 60;
} else if (servo_angle < 0) {
servo_angle = 0;
}
Serial.println(servo_angle); // Print the servo angle for debugging
myservo.write(servo_angle); // Set the servo position
}
}
Explanation:
Maps the pitch angle to control the servo position.
Limits the servo angle to a safe range (0-60 degrees) to avoid excessive motion.
Uses 10Hz updates to keep the response smooth.
By directly reading roll, pitch, and yaw, you can unlock advanced motion-based applications with CodeCell. Try modifying the examples and integrating them into your own projects. Check out the CodeCell GitHub Repository for more examples and technical documentation!
Join our Community ~ Be the first to know about new products and get exciting deals!
© 2025 Microbots.