In this guide, we'll explore how to use the CodeCell to control a servo motor. We will also see how we can control the servo's angular position using the CodeCell's angular position, which is especially useful for robotic applications like gimbals or angular stability control.
A servo motor is a rotary actuator that allows for precise control of angular position. It consists of a motor coupled with gears and a sensor for position feedback, and it is commonly used in robotics, automation, and stabilization applications.
CodeCell is equipped with a BNO085 motion sensor that fuses data from an accelerometer, gyroscope, and magnetometer to compute rotation vectors. These vectors are processed by CodeCell to obtain roll, pitch, and yaw angles based on the device's orientation in space.
In this example, our CodeCell continuously monitors the pitch angle to control the position of a servo motor, allowing it to respond dynamically to changes in orientation. The pitch value is used to set the servo motor's position, enabling it to rotate based on how you tilt the device.
Below is an example code snippet to get you started. Ensure your CodeCell is properly connected via USB-C. Also, verify that your servo motor can be driven via USB-C power (CodeCell can deliver up to 500mA via USB-C and up to 1500mA when powered by a battery). Adjust the angular limits in the code accordingly.
For this example, you need to install the 'ESP32Servo' library to 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. Enable Tools/USB_CDC_On_Boot 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(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;
}
else{
//Skip
}
Serial.println(servo_angle); // Print the servo angle for debugging
myservo.write(servo_angle); // Set the servo position
}
}
This basic functionality can be expanded to create more complex interactions, such as:
servo_angle
in the code. In this example, we use a 60-degree range micro-servo. Some servo motors are not mechanically linear, so you might need to compensate for angular-mechanical errors.This project demonstrates how to control a servo motor with CodeCell using its motion-sensing capabilities. Experiment with the code, build your own projects, and check out the CodeCell GitHub Repository for more code examples and technical documentation!
Join our Community ~ Be the first to know about new products and get exciting deals!
© 2025 Microbots.