Example Code for Arduino-UNO

Last revision 2026/01/15

The article offers a comprehensive guide on using Arduino UNO to control servo motor angles, including software setup, wiring instructions, and sample code utilizing the Herkulex library.

Software Preparation

Development tools: Arduino IDE (download from Arduino official website). Library: Herkulex library (download from Arduino library). Installation: Follow Arduino library installation instructions to add the Herkulex library to Arduino IDE.

Wiring Diagram


Servo Motor Pinout

Black :GND
Red :VDD(7.4V)
Blue :TXD
Yellow :RXD

Other Preparation Work

There is only one Hardware Serial port on UNO, so that Software Serial has to be used. However, the default baud rate of HerkuleX is up to 115200, which may be unstable when using the Software Serial. Thus it's recommended to change the baud rate of HerkuleX to 57600 first.**

Sample Code

#include <Herkulex.h>

int n=0xfe; //motor ID - verify your ID !!!!

void setup()
{
  delay(2000);  //a delay to have time for serial monitor opening
  Serial.begin(115200);    // Open serial communications
  Serial.println("Begin");
  Herkulex.begin(115200,10,11); //open serial with rx=10 and tx=11
  Herkulex.reboot(n); //reboot first motor
  delay(500);
  Herkulex.initialize(); //initialize motors
  delay(200);
}

void loop(){
  Serial.println("Move Angle: -100 degrees");
  Herkulex.moveOneAngle(n, -100, 1000, LED_BLUE); //move motor with 300 speed
  delay(1200);
  Serial.print("Get servo Angle:");
  Serial.println(Herkulex.getAngle(n));
  Serial.println("Move Angle: 100 degrees");
  Herkulex.moveOneAngle(n, 100, 1000, LED_BLUE); //move motor with 300 speed
  delay(1200);
  Serial.print("Get servo Angle:");
  Serial.println(Herkulex.getAngle(n));
}

Was this article helpful?

TOP