Example Code for Arduino-UNO

Last revision 2026/01/16

This blog post guides users on controlling motor angles with Arduino UNO using the Herkulex library, featuring software preparation, wiring diagrams, and practical sample code for effective setup and control.

Software Preparation

Wiring Diagram

Other Preparation Work

  • There is only one Hardware Serial port on UNO, so Software Serial has to be used
  • Default Herkulex baud rate (115200) may be unstable with Software Serial - recommended to change to 57600 first
  • Verify your motor ID (default: 0xfe)

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