This article is a continuation of “Design and Control of a Quadruped Spider Robot Using ESP32 (Part 1)”
ESP32 Quadruped Spider Robot Circuit Diagram (8 Servos)
This circuit diagram illustrates the integration of 12 servos, allowing the robot to move in various directions.
The robot consists of four legs, with each leg connected to two servos. One servo is responsible for axial rotation (called pivot servo), while the other servo controls the lifting motion of the leg (called lift servo).
To achieve coordinated movement, the servos are connected in a specific sequence or order. The precise sequence and connections depend on the desired gait pattern and the specific control algorithm employed. By synchronously controlling the movements of these servos, the robot can achieve stable and coordinated motion.
ESP32 and Servos:
-
Assign PWM (Pulse Width Modulation) supporting pins on the ESP32 to control the servos. These pins will generate the necessary PWM signals to adjust the position of each servo motor.
-
Connect the signal wire of each servo (usually yellow or white) to a separate PWM-supporting pin on the ESP32.
Servo 1 |
Pin 21 |
Servo 5 |
Pin 27 |
Servo 2 |
Pin 19 |
Servo 6 |
Pin 14 |
Servo 3 |
Pin 33 |
Servo 7 |
Pin 12 |
Servo 4 |
Pin 25 |
Servo 8 |
Pin 13 |
-
Connect the positive terminal of the power supply to the VCC pin of each servo motor.
-
Connect the ground terminal of the power supply to the ground (GND) pins of all servo motors.
-
Connect the ground (GND) pin of the ESP32 to the ground terminal of the power supply (-).
-
Use a switch to connect the Vin pin of the ESP32 to the positive terminal of the power supply (+).
If using a battery with a higher voltage, use a buck circuit (DC-DC buck converter) to reduce it to 5 volts. Connect the output of the buck circuit to the ESP32 and the servos. This ensures safe and efficient power supply.
Note that the specific pin connections and programming code may vary depending on the exact model of the ESP32 development board you are using and the servos you have. Be sure to consult the datasheets and pin diagrams of the components to understand the accurate pin assignments and voltage requirements. Additionally, ensure that you handle the power supply and connections safely, following best practices to avoid damage to the components.
ESP32 Quadruped Spider Robot Code Explanation
The spider robot described in this project is not operated via a remote control. Instead, it utilizes different functions to perform various movements, such as moving forward, backward, right, and left. These functions are based on a crawling gait (also known as a quadrupedal gait). The crawling gait pattern is converted into a program and embedded into the ESP32 microcontroller. This program provides instructions to the servos, indicating where to position the robot’s legs for the desired movement. By executing the programmed crawling gait, the spider robot can achieve coordinated and controlled movement.
It is essential to execute this code #1 before starting the assembly phase. This code is designed to calibrate and adjust the servos to the correct angles. By running this code, you can ensure that the servos operate correctly and are accurately positioned. This will help prevent any potential issues or complications during subsequent stages of the assembly process.
Note: Before uploading the code to the ESP32, always ensure that the switch is turned off and the external power supply is disconnected. This step is crucial as the switch provides electrical isolation between the Vin pin of the ESP32 and the Vcc pin of the servos. This precaution ensures the safety and normal operation of the components during the code upload process. Remember to follow this procedure every time you upload code to the ESP32, maintaining electrical isolation between the ESP32 and the servos to avoid any unnecessary complications or damage.
Code #1#include // include servo library// Define 8 ServosServo myServo1; // Front Left Pivot ServoServo myServo2; // Front Left Lift ServoServo myServo3; // Back Left Pivot ServoServo myServo4; // Back Left Lift ServoServo myServo5; // Back Right Pivot ServoServo myServo6; // Back Right Lift ServoServo myServo7; // Front Right Pivot ServoServo myServo8; // Front Right Lift Servovoid setup() { // Attach servos to Arduino Pins myServo1.attach(21); myServo2.attach(19); myServo3.attach(33); myServo4.attach(25); myServo5.attach(27); myServo6.attach(14); myServo7.attach(12); myServo8.attach(13); myServo1.write(90); myServo2.write(90); myServo3.write(90); myServo4.write(90); myServo5.write(90); myServo6.write(90); myServo7.write(90); myServo8.write(90);}void loop() {}
This code #2 includes the servo movements required to make your robot operate correctly. Without this code, you will not be able to complete the project successfully. It is crucial for the normal operation of the robot.
Additionally, the code comments are well done, providing explanations for each block of code. These comments serve as guidance, making it easy for you to identify which block corresponds to a specific movement or function. If you need to make any modifications or adjustments, the comments will help you understand the purpose and functionality of each block of code.
Code #2#include // include servo libraryThis line includes the ESP32_Servo library, which provides functions for controlling servo motors using the ESP32 microcontroller.// Define the delay between function calls (in milliseconds)const unsigned long FUNCTION_DELAY = 2000; // 2 seconds
This line declares a global constant named FUNCTION_DELAY, which indicates the delay between each function call (in milliseconds).
// Define 8 ServosServo myServo1; // Front Left Pivot ServoServo myServo2; // Front Left Lift ServoServo myServo3; // Back Left Pivot ServoServo myServo4; // Back Left Lift ServoServo myServo5; // Back Right Pivot ServoServo myServo6; // Back Right Lift ServoServo myServo7; // Front Right Pivot ServoServo myServo8; // Front Right Lift Servo
This block declares eight Servo objects named “myServo1” to “myServo8”. Each object represents a servo motor and is associated with a specific function (such as pivot or lift) and a specific leg of the spider robot.
void setup() { // Attach servos to Arduino Pins myServo1.attach(21); myServo2.attach(19); myServo3.attach(33); myServo4.attach(25); myServo5.attach(27); myServo6.attach(14); myServo7.attach(12); myServo8.attach(13); center_servos(); Serial.println("center"); delay(2000);}
The setup function executes once when the microcontroller starts. It uses the attach() method to attach each Servo object to the corresponding Arduino pin. The center_servos() function is called to set all servos to their center position, and a 2-second delay is added for stability.
void loop() { // Move Forward 10 steps for (int i = 0; i < 10; i++) { moveLegServos_Forward(); } center_servos(); delay(FUNCTION_DELAY); // Move Backward 10 steps for (int i = 0; i < 10; i++) { moveLegServos_Backward(); } center_servos(); delay(FUNCTION_DELAY); // Move Right 10 steps for (int i = 0; i < 10; i++) { moveLegServos_Right(); } center_servos(); delay(FUNCTION_DELAY); // Move Left 10 steps for (int i = 0; i < 10; i++) { moveLegServos_Left(); } center_servos(); delay(FUNCTION_DELAY); // dance 10 steps for (int i = 0; i < 10; i++) { dance(); } center_servos(); delay(FUNCTION_DELAY); // pushup 10 steps for (int i = 0; i < 10; i++) { pushup(); } center_servos(); delay(FUNCTION_DELAY);}
This block contains the ‘loop()’ function, which executes repeatedly after the ‘setup()’ function. It consists of several “for” loops, each controlling the number of steps for a specific movement. In each iteration, the corresponding movement function is called (‘moveLegServos_Forward()’, ‘moveLegServos_Backward()’, ‘moveLegServos_Right()’, ‘moveLegServos_Left()’, ‘pushup()’, ‘dance()’). After each movement, the “center_servos()” function is called to reset all servos to their center position. A delay of “FUNCTION_DELAY” milliseconds is introduced between each action.
// Move Spider Robot Forward Functionvoid moveLegServos_Forward() { // Control the servo actions for each leg // Left side leg - Leg 1 moveLeg_Left_Forward(myServo8, myServo7, myServo8); // Left side leg - Leg 4 moveLeg_Left_Forward(myServo2, myServo1, myServo2); // Left side leg - Legs 1 and 4 for (int angle = 0; angle <= 90; angle += 2) { myServo7.write(angle); myServo1.write(angle); delay(10); } // Right side leg - Leg 2 moveLeg_Right_Forward(myServo6, myServo5, myServo6); // Right side leg - Leg 3 moveLeg_Right_Forward(myServo4, myServo3, myServo4); // Right side leg - Legs 2 and 3 for (int angle = 180; angle >= 90; angle -= 2) { myServo5.write(angle); myServo3.write(angle); delay(10); }}
This block defines the “moveLegServos_Forward()” function, which controls the servo actions for moving the spider robot forward. It calls the “moveLeg_Left_Forward()” and “moveLeg_Right_Forward()” functions to control the movements of each leg. The function gradually adjusts the servo angles to achieve the desired leg positions for moving forward.
// Move Spider Robot Backward Functionvoid moveLegServos_Backward() { // Control the servo actions for each leg // Left side leg - Leg 1 moveLeg_Left_Backward(myServo2, myServo1, myServo2); // Left side leg - Leg 4 moveLeg_Left_Backward(myServo8, myServo7, myServo8); // Left side leg - Legs 1 and 4 for (int angle = 180; angle >= 90; angle -= 2) { myServo1.write(angle); myServo7.write(angle); delay(10); } // Right side leg - Leg 2 moveLeg_Right_Backward(myServo4, myServo3, myServo4); // Right side leg - Leg 3 moveLeg_Right_Backward(myServo6, myServo5, myServo6); // Right side leg - Legs 2 and 3 for (int angle = 0; angle <= 90; angle += 2) { myServo3.write(angle); myServo5.write(angle); delay(10); }}
This block defines the “moveLegServos_Backward()” function, which controls the servo actions for moving the spider robot backward. It calls the “moveLeg_Left_Backward()” and “moveLeg_Right_Backward()” functions to control the movements of each leg. The function gradually adjusts the servo angles to achieve the desired leg positions for moving backward.
// Move Spider Robot Right Function void moveLegServos_Right() { // Control the Right turn servo actions for each leg moveLeg_Right(myServo8, myServo7, myServo1); // Leg 4 moveLeg_Right(myServo6, myServo5, myServo7); // Leg 3 moveLeg_Right(myServo4, myServo3, myServo5); // Leg 2 moveLeg_Right(myServo2, myServo1, myServo3); // Leg 1}// Move Spider Robot Left Function void moveLegServos_Left() { // Control the Left turn servo actions for each leg moveLeg_Left(myServo8, myServo7, myServo1); // Leg 4 moveLeg_Left(myServo6, myServo5, myServo7); // Leg 3 moveLeg_Left(myServo4, myServo3, myServo5); // Leg 2 moveLeg_Left(myServo2, myServo1, myServo3); // Leg 1}
In this code block, the “moveLegServos_Right()” and “moveLegServos_Left()” functions control the servo actions for moving the spider robot to the right and left, respectively.
These functions coordinate the movements of the leg servos to achieve the desired motion of the spider robot turning to the right or left. Each function calls the appropriate “moveLeg_Right()” or “moveLeg_Left()” function, passing the corresponding servo objects as parameters.
// Spider Robot dance Functionvoid dance(){ // Move the selected servos from 0 to 180 degrees for (int angle = 0; angle <= 180; angle += 2) { myServo1.write(angle); myServo3.write(angle); myServo5.write(angle); myServo7.write(angle); delay(10); // Delay between each angle change (adjust as needed) }// Move the selected servos from 180 to 0 degrees for (int angle = 180; angle >= 0; angle -= 2) { myServo1.write(angle); myServo3.write(angle); myServo5.write(angle); myServo7.write(angle); delay(10); // Delay between each angle change (adjust as needed) }}// Spider Robot pushup Function void pushup(){ // Move the selected servos from 0 to 180 degrees for (int angle = 0; angle <= 180; angle += 2) { myServo2.write(angle); myServo4.write(angle); myServo6.write(angle); myServo8.write(angle); delay(10); // Delay between each angle change (adjust as needed) } // Move the selected servos from 180 to 0 degrees for (int angle = 180; angle >= 0; angle -= 2) { myServo2.write(angle); myServo4.write(angle); myServo6.write(angle); myServo8.write(angle); delay(10); // Delay between each angle change (adjust as needed) }}
This block defines the pushup() and dance() functions, which control the servo actions of the spider robot to perform push-ups and dance movements. It alternates the servo angles of the selected servos (myServo1, myServo3, myServo5, myServo7) between 0 and 180 degrees for the dance effect and between 0 and 180 degrees for the push-up effect.
// Move Spider Robot Forward logicvoid moveLeg_Left_Forward(Servo& liftServo, Servo& pivotServo , Servo& oppositePivotServo) { // Move the lift servo from 90 to 180 for (int angle = 90; angle <= 180; angle += 2) { liftServo.write(angle); delay(10); } // Move the pivot servo from 90 to 0 for (int angle = 90; angle >= 0; angle -= 2) { pivotServo.write(angle); delay(10); } // Move the lift servo from 180 to 90 for (int angle = 180; angle >= 90; angle -= 2) { liftServo.write(angle); delay(10); }}void moveLeg_Right_Forward(Servo& liftServo, Servo& pivotServo , Servo& oppositePivotServo) { // Move the lift servo from 90 to 180 for (int angle = 90; angle <= 180; angle += 2) { liftServo.write(angle); delay(10); } // Move the pivot servo from 90 to 180 for (int angle = 90; angle <= 180; angle += 2) { pivotServo.write(angle); delay(10); } // Move the lift servo from 180 to 90 for (int angle = 180; angle >= 90; angle -= 2) { liftServo.write(angle); delay(10); }} // Move Spider Robot Backward logicvoid moveLeg_Left_Backward(Servo& liftServo, Servo& pivotServo, Servo& oppositePivotServo) { // Move the lift servo from 90 to 180 for (int angle = 90; angle <= 180; angle += 2) { liftServo.write(angle); delay(10); } // Move the pivot servo from 90 to 180 for (int angle = 90; angle <= 180; angle += 2) { pivotServo.write(angle); delay(10); } // Move the lift servo from 180 to 90 for (int angle = 180; angle >= 90; angle -= 2) { liftServo.write(angle); delay(10); }}void moveLeg_Right_Backward(Servo& liftServo, Servo& pivotServo, Servo& oppositePivotServo) { // Move the lift servo from 90 to 180 for (int angle = 90; angle <= 180; angle += 2) { liftServo.write(angle); delay(10); } // Move the pivot servo from 90 to 0 for (int angle = 90; angle >= 0; angle -= 2) { pivotServo.write(angle); delay(10); } // Move the lift servo from 180 to 90 for (int angle = 180; angle >= 90; angle -= 2) { liftServo.write(angle); delay(10); }}
This code block includes four functions: “moveLeg_Left_Forward()”, “moveLeg_Right_Forward()”, “moveLeg_Left_Backward()”, and “moveLeg_Right_Backward()”.
The moveLeg_Left_Forward(): This function is responsible for moving the left leg of the spider robot forward. It takes three servo objects as parameters: liftServo, pivotServo, and oppositePivotServo. The function first moves the liftServo from 90 degrees to 180 degrees in increments of 2 degrees. Then, it moves the pivotServo from 90 degrees to 0 degrees in the same increments. Finally, it moves the lift servo back from 180 degrees to 90 degrees. Each movement is accompanied by a 10-millisecond delay.
The moveLeg_Right_Forward(): This function is responsible for moving the right leg of the spider robot forward. Its structure is similar to that of moveLeg_Left_Forward(), but it includes an additional movement. After moving liftServo and pivotServo as described above, it moves the opposite PivotServo from 0 degrees to 90 degrees. This additional movement aims to coordinate the leg movements for forward motion.
The moveLeg_Left_Backward(): This function is responsible for moving the left leg of the spider robot backward. It follows a similar structure to moveLeg_Left_Forward(), but with some variations in angle increments and direction. The pivot servo moves from 90 degrees to 180 degrees, and the lift servo moves from 180 degrees to 90 degrees. These changes result in the leg moving in the opposite direction.
The moveLeg_Right_Backward(): This function is responsible for moving the right leg of the spider robot backward. Its structure is similar to that of moveLeg_Left_Backward(), but it includes an additional movement. After moving liftServo and pivotServo, it moves the opposite PivotServo from 90 degrees to 0 degrees.
// Move Spider Robot Right logicvoid moveLeg_Right(Servo& liftServo, Servo& pivotServo , Servo& oppositePivotServo) { // Move the lift servo from 90 to 180 for (int angle = 90; angle <= 180; angle += 2) { liftServo.write(angle); delay(10); } // Move the pivot servo from 90 to 0 for (int angle = 90; angle >= 0; angle -= 2) { pivotServo.write(angle); delay(10); } // Move the opposite pivot servo from 0 to 90 for (int angle = 0; angle <= 90; angle += 2) { oppositePivotServo.write(angle); delay(10); } // Move the lift servo from 90 to 180 for (int angle = 180; angle >= 90; angle -= 2) { liftServo.write(angle); delay(10); }}// Move Spider Robot Left logicvoid moveLeg_Left(Servo& liftServo, Servo& pivotServo , Servo& oppositePivotServo) { // Move the lift servo from 90 to 180 for (int angle = 90; angle <= 180; angle += 2) { liftServo.write(angle); delay(10); } // Move the pivot servo from 90 to 180 for (int angle = 90; angle <= 180; angle += 2) { pivotServo.write(angle); delay(10); } // Move the opposite pivot servo from 180 to 90 for (int angle = 180; angle >= 90; angle -= 2) { oppositePivotServo.write(angle); delay(10); } // Move the lift servo from 180 to 90 for (int angle = 180; angle >= 90; angle -= 2) { liftServo.write(angle); delay(10); }}
This code block includes two functions: moveLeg_Right() and moveLeg_Left(). Below is a description of each function:
The moveLeg_Right(): This function is responsible for moving the right leg of the spider robot to the right or turning. It takes three servo objects as parameters: liftServo, pivotServo, and oppositePivotServo. The function first moves the liftServo from 90 degrees to 180 degrees in increments of 2 degrees. It then moves the pivotServo from 90 degrees to 0 degrees in the same increments. Next, it moves the opposite PivotServo from 0 degrees to 90 degrees. Finally, it moves the lift servo back from 180 degrees to 90 degrees. Each movement is accompanied by a 10-millisecond delay.
The moveLeg_Left(): This function is responsible for moving the left leg of the spider robot to the left or turning. It follows a similar structure to moveLeg_Right(), but with some variations in angle increments and direction. The pivot servo moves from 90 degrees to 180 degrees, and the opposite pivot servo moves from 180 degrees to 90 degrees. These changes result in the leg moving in the opposite direction.
// All Servos Center function void center_servos() { myServo1.write(90); myServo2.write(90); myServo3.write(90); myServo4.write(90); myServo5.write(90); myServo6.write(90); myServo7.write(90); myServo8.write(90);}
This block defines the center_servos() function, which sets all servos to their center position by writing a 90-degree angle to each servo motor.
Overall, this code provides a basic framework for controlling a spider robot using the ESP32 microcontroller and servos, enabling it to perform various movements and actions.
This article is sourced from
https://circuitdigest.com/microcontroller-projects/esp32-controlled-quadruped-spider-robot
Leave a Comment
Your email address will not be published. Required fields are marked *