🎉 Black Friday is Here! Enjoy Up to 35% Off ~ Offer Ends November 30th!
In this build, we'll explore how to use the CodeCell's onboard 9-axis motion sensor to read roll, pitch, and yaw angles, and use these angles to control a servo motor. This project demonstrates how to create interactive motion-based controls, perfect for robotics, gimbals, or any project that requires responsive rotation control.
The CodeCell is equipped with a BNO085 motion sensor, which provides precise motion sensing capabilities, including roll, pitch, and yaw angles. By reading these angles, you can create interactive motion controls for various applications, such as stabilizing platforms or create a response to device orientation.
The BNO085 motion sensor read the acceleromter, gyroscope and magnetometer reading and compute the rotation vectors. These vectors are sent to the CodeCell which it then transforms into angular data to obtain the roll, pitch, and yaw based on the device's orientation in space. These angles represent the device's rotation along three axes. In this example, we'll use the pitch angle to control the position of a servo motor, allowing it to respond dynamically to changes in orientation.
In this example, the CodeCell continuously monitors the pitch angle. The pitch value is used to set the servo motor's position, allowing it to rotate based on how you tilt the device. This basic functionality can be expanded to create more complex interactions, such as controlling multiple servos, stabilizing a platform, or adding responsive movements to a robot.
Below is the example code to get you started. Make sure your CodeCell is properly connected via USB-C. Also, make sure that your servo-motor can be power via USB-C and add its angular limits to the code.
For this example you have to download the ESp32Servo libraryto control the servo-motor with your CodeCell. Follow the comments in the code to understand each step.
#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. Ensure Tools/USB_CDC_On_Boot is enabled if using Serial
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()) {
// 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
}
}
servo_angle
in the code. In this example we are using a 60deg range micro-servo. Note that some servo-motors are not mechanically linear so you might also have to compensate for its angular-mechanical error.This project introduces the basics of using rotation sensing with CodeCell to control a servo, opening up numerous possibilities for motion-responsive projects. Experiment with the code, tweak the settings, and create your own dynamic builds!
Be the first to know about new projects and get exciting deals!
© 2024 Microbots.