Example Code for Arduino-Hexapod Walking

Last revision 2025/12/30

The code demonstrates how to make the hexapod robot stand up and start walking. It controls the servos to move the legs to 170 for standing up, hips to 480 for standing up, axis group A to 130, and axis group B to 70 to prepare for walking, then enters a walking loop.

Hardware Preparation

  • Arduino Mega 1280 microcontroller board * 1.
  • Mega IO Expansion Shield for Arduino Mega * 1.
  • Hexapod robot frame kit * 1.
  • Uptech CDS5516 Servos * 18.
  • Servo driver board * 2.
  • 7.4v battery * 1.
  • Servo debugger board.

Software Preparation

Development tools: Arduino IDE.

You must import the below three libraries first:

  • CDS5500_2Serials.h
  • Hexapod_Servo.h
  • ServoConsole.h

Download links for libraries:
libraries.zip

Wiring Diagram

Wiring diagram for servos

Other Preparation Work

You must set the IDs of the servo according to the documents :

  • IDs of the servos of the left front leg: 0, 1, 2 (inner, middle, outer)
  • IDs of the servos of the left middle leg: 10, 11, 12(inner, middle, outer)
  • IDs of the servos of the left back leg: 20, 21, 22 (inner, middle, outer)
  • IDs of the servos of the right back leg: 30, 31, 32(inner, middle, outer)
  • IDs of the servos of the right middle leg: 40, 41, 42 (inner, middle, outer)
  • IDs of the servos of the right front leg: 50, 51, 52(inner, middle, outer)

Ensure the Arduino Mega is connected correctly. The code uses Serial1 (1000000 baud) for right side servos and Serial2 (1000000 baud) for left side servos.

Sample Code

/*
*Sample code for the hexapod robot.
*Author: Danfei Xu
*Created on Aug 9th, 2011
*You must import the below three libraries first.
*/


#include <CDS5500_2Serials.h>
#include <Hexapod_Servo.h>
#include <ServoConsole.h>


// Below are the ID arrays of servos. You must set the IDs of the servo according
//to the documents :
//IDs of the servos of the left front leg: 0, 1, 2 (inner, middle, outer)
//IDs of the servos of the left middle leg: 10, 11, 12(inner, middle, outer)
//IDs of the servos of the left back leg: 20, 21, 22 (inner, middle, outer)
//IDs of the servos of the right back leg: 30, 31, 32(inner, middle, outer)
//IDs of the servos of the right middle leg: 40, 41, 42 (inner, middle, outer)
//IDs of the servos of the right front leg: 50, 51, 52(inner, middle, outer)
int axisA[] = {0, 20, 40};
int axisB[] = {10, 30, 50};
int hipA[] = {1, 21, 41};
int hipB[] = {11, 31, 51};
int legA[] = {2, 22, 32};
int legB[] = {12, 32, 52};
int axis[] = {0, 10, 20, 30, 40, 50};
int hips[] = {1, 11, 21, 31, 41, 51};
int legs[] = {2, 12, 22, 32 ,42, 52};
// servo controller
ServoConsole console;

void setup(){
  Serial.begin(115200); //For printing out data, or debugging.
  Serial2.begin(1000000); // Used for control servos on left side.
  Serial1.begin(1000000);// Used for control servos on right side.
}

void loop(){
   console.Move(legs, 6, 170, 511); //Legs move to 170 for standing up
   console.Move(hips,6, 480, 511); // hips move to 480 for standing up
   console.Move(axisA, 3, 130, 511); // axis group A move to 130. Prepare for walking
   console.Move(axisB, 3, 70, 511); // axis group B move 70. Prepare for walking
   delay(1000); // waits for their moving

   while(1){
     walk();
     //walking loop
   }
}

void walk(){
  delay(500);
  console.MoveUp(hipB, 3, 100, 500); //hips group B move up 100
  console.MoveUp(axisB, 3, 60, 300);// axis group B move forward 60
  console.MoveDown(axisA, 3, 60, 300);// axis group A move backward 60
  delay(500);// waits for movement
  console.MoveDown(hipB, 3, 100, 500);//hips group B move down 100
  delay(500);// waits for movement
  console.MoveUp(hipA, 3, 100, 500);//hips group A move up 100
  console.MoveDown(axisB, 3, 60, 300);// axis group B move backward 60
  console.MoveUp(axisA, 3, 60, 300);// axis group A move forward 60
  delay(500);// waits for movement
  console.MoveDown(hipA,3, 100, 500);//hips group A move down 100
}

Result

The hexapod will first move the legs to 170 and hips to 480 to stand up. Then it moves axis group A to 130 and axis group B to 70 to prepare for walking. After a 1-second delay, it enters a walking loop where it moves the hips and axes to walk forward. The expected phenomena are the hexapod standing up and then walking continuously.

Was this article helpful?

TOP