Example Code for Arduino-Mega
Last revision 2026/01/15
Learn how to set up and utilize the Herkulex library with Arduino IDE for Mega projects, featuring a detailed wiring diagram and sample code to control servo motors effectively.
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
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.beginSerial1(115200); //open serial port 1
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?
