Build Your Own Smart Robot with ESP32 and Servos

Build Your Own Smart Robot with ESP32 and Servos!

Build Your Own Smart Robot with ESP32 and Servos

Introduction

Dear friends, have you ever thought about how your room could be filled with mystery and surprises just like a castle in a fairy tale? Today, let’s continue to use the ESP32 to control a 9G servo to add a unique charm to your room!

Materials Preparation

First, let’s understand this mysterious device.

  • Servo: This is a special motor that can accurately control angles, commonly used in robotics, drones, and other fields. It controls its rotation angle by receiving signals from a microcontroller. The servo, also known as a servo motor, can rotate at specific angles, typically 90°, 180°, and 360° (360° can rotate continuously). Many servos are used in robots to perform actions like raising hands or shaking heads. Therefore, the more servos a robot has, the more flexible its movements will be.

  • Build Your Own Smart Robot with ESP32 and Servos

  • Build Your Own Smart Robot with ESP32 and Servos

Control Principle

To use the ESP32 to control a 9G servo, we need to follow these steps:

  1. Connect Hardware: Connect the GPIO pins of the ESP32 to the data input of the 9G servo, and then connect the power supply to the positive and negative terminals of the 9G servo.

  2. Write Program: Write a program that allows the ESP32 to send data to the 9G servo through the GPIO pins to control its rotation angle.

  3. Upload Program: Upload the written program to the ESP32, then press the reset button to let the ESP32 start running the program.

  4. Observe Effects: Now, you can observe the rotation angle of the 9G servo. When the ESP32 sends a signal, it will accurately control its rotation angle.

Using MicroPython to Write Control Program

Next, we will use MicroPython to write the control program. MicroPython is a lightweight Python interpreter that is very suitable for beginners.

The control of a 180° servo generally requires a pulse of about 20ms, where the high level part of the pulse is usually within the range of 0.5ms-2.5ms for angle control, with a total interval of 2ms. For a 180-degree servo, the corresponding control relationship in MicroPython programming is from -90° to 90°.

Build Your Own Smart Robot with ESP32 and Servos

For a 360° continuous rotation servo, the above pulse table corresponds to the process of rotating from maximum speed in the forward direction to maximum speed in the reverse direction.

First, we need to install the MicroPython firmware on the ESP32 development board. Then, we can write a simple program to control the 9G servo. We can directly write a PWM function to drive it. Here is a simple example code:

#step1 Import libraries
from machine import Pin, PWM
import time

#step2 Create servo object
S1 = PWM(Pin(4), freq=50, duty=0) # The pin for Servo1 is 4

#step3 Implement function

def Servo(servo, angle):
    S1.duty(int(((angle + 90) * 2 / 180 + 0.5) / 20 * 1023))

#step4 Call
    #-90 degrees
Servo(S1, -90)
time.sleep(1)
#-45 degrees
Servo(S1, -45)
time.sleep(1)
#0 degrees
Servo(S1, 0)
time.sleep(1)
#45 degrees
Servo(S1, 45)
time.sleep(1)
#90 degrees
Servo(S1, 90)
time.sleep(1)

Running the Program

The program still follows the four-step structure that I have been mentioning in previous articles. Running the program, you can see that the servo can easily be controlled to rotate to different angles sequentially.

Conclusion

We have just achieved the angle control of the 180° servo. Now let’s conduct an experiment with the 360° continuous rotation servo. The 360° continuous rotation servo can function as a DC gear motor, which can be used in cars or model aircraft. Now that you have learned how to use the ESP32 to control a 9G servo, it’s time to unleash your creativity and create your own smart robot! Here are some suggestions:

  1. Design Appearance: You can design the image of your ideal little robot using materials like cardboard and colored pens to make it look cuter and more interesting.

  2. Assemble Structure: Assemble the various parts according to the design drawings, such as the base, wheels, arms, etc. Make sure to keep the structure stable and firm.

  3. Install Servo: Install the 9G servo on the robot’s arm, ensuring it can rotate freely. Also, connect the power line to the ESP32 and the 9G servo.

  4. Write Program: Write a program that allows the ESP32 to send data to the 9G servo through the GPIO pins to control its rotation angle. For example, you can write a program that makes the robot’s arm move a certain angle horizontally.

Through this section, we have learned to use different types of servos, and by combining multiple motors, we can conduct experiments with models, model aircraft, cars, and robots.

Wish you a wonderful journey in creativity! If you are interested in other creative projects, please bookmark our public account to get more interesting content and tutorials. Don’t forget to share it with friends who may need it!

If you like this little experiment, please give it a thumbs up and let us know! Thank you!

Leave a Comment

×