Arduino Uno Servo Motor Control Code

Servos are fast and strong motors with a limited movement range. You can control SG90, MG996R and other small servo motors with your Ozeki software by providing a desired angle. Each servo has a built in potmeter. It will try to position the motor axis to reach the desired angle. You can discover the minimum and maximum movement range by reading the datasheet. You can simultaneously control as many servo motors as you wish. It also supports the waterproof SM-S8330M with 30 kg torque.Servos are fast and strong motors with a limited movement range. You can control SG90, MG996R and other small servo motors with your Ozeki software by providing a desired angle. Each servo has a built in potmeter. It will try to position the motor axis to reach the desired angle. You can discover the minimum and maximum movement range by reading the datasheet. You can simultaneously control as many servo motors as you wish. It also supports the waterproof SM-S8330M with 30 kg torque.

arduino uno servo motor control
Figure 1 - Arduino Uno Servo Motor Control

Required hardware

  • Arduino Uno
  • Servo Motor(s)

Source code to install on controller


Before you upload this code to your Arduino, please format the EEPROM...
#include <OzIDManager.h>
#include <OzServoController.h>

OzIDManager* manager;
OzServoController* servocontroller;

void setup()
{
  Serial.begin(115200);

  manager = new OzIDManager;
  manager->_sendACK = true;
  manager->_checksum = true;

  OzCommunication::setIDManager(manager);

  servocontroller = new OzServoController;
  //pin 6, MG996R servo (motor min -60, motor max +60), starting angle +5
  servocontroller->AddMotorconfig(6, ServoMotors::SERVO_MG996R, 5);

  int x=1; 
  manager->sendLinkSetup();
  manager->PrintWelcomeLine(servocontroller, x++, "MyServoMotor");
  servocontroller->showMotorDetails();
}

void loop()
{
    OzCommunication::communicate();
}

Good to know

You can use the following enums in the constructor to use the SG90, MG996R or SM-S8330M servos:

servocontroller->AddMotorconfig(5, ServoMotors::SERVO_SG90);
servocontroller->AddMotorconfig(6, ServoMotors::SERVO_MG996R);
servocontroller->AddMotorconfig(7, ServoMotors::SERVO_SM_S8330M);

More information