ESP32

Control Servo Motor using ESP32 Wifi and Python Widgets

Introduction:

Controlling servo motors wirelessly using a Python GUI and web server can greatly enhance the flexibility and convenience of various applications. In this article, we will explore the exciting realm of controlling a servo motor through a Python Graphical User Interface (GUI) and a web server using an ESP32 Wi-Fi module. The ESP32, acting as a bridge between the GUI, server, and the servo motor, enables wireless communication, allowing for seamless and remote control of the motor’s position. By following this guide, you will gain the knowledge and skills needed to implement this wireless servo motor control system in your own projects, opening up a world of possibilities for automation, robotics, and remote-controlled applications.

This article delves into the exciting realm of controlling servo motors using Python GUI widgets, such as TextBox, Buttons, Dials, and Sliders. Furthermore, we explore the integration of these widgets with the ESP32 microcontroller, leveraging its built-in Wi-Fi capabilities to enable wireless servo motor control.

In my previous article titled “SG90 Servo Motor with ESP32 Interfacing and Programming,” I delved into the fascinating world of controlling SG90 servo motors using the ESP32 microcontroller.




Amazon links:

ESP32

Servo Motors

Jumper wires

Breadboard

Power supply

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

About SG90 Servo Motor:

Image depicting the control of an SG90 servo motor using an ESP32 microcontroller, WiFi connectivity, and Python programming. The image shows the SG90 servo motor connected to the ESP32 board, with wires and cables indicating the electrical connections. The Python code running on the ESP32 is responsible for controlling the servo motor via WiFi

The SG90 servo motor is a popular and widely used micro servo motor known for its compact size, lightweight design, and affordability. This article provides an overview of the SG90 servo motor, including its specifications, working principle, and various applications in different fields.

Specifications:

Operating Voltage: 4.8V – 6V

Stall Torque: 1.8 kg/cm

Operating Speed: 0.12 sec/60°

Weight: 9 grams

Dimensions: 23mm x 12.2mm x 29mm

Gear Type: Plastic



Working Principle:

Three-dimensional representation of a servo motor construction with a control circuit board, ESP32 microcontroller, and Python WiFi controlling. The image showcases a visual representation of a servo motor, highlighting its internal components and construction in a 3D format. Additionally, it portrays the presence of a control circuit board connected to an ESP32 microcontroller, which enables wireless control of the servo motor using Python programming and WiFi connectivity

The SG90 servo motor is based on a closed-loop control system. It consists of a DC motor, a set of gears, a control circuit, and a potentiometer feedback system. The potentiometer provides position feedback to the control circuit, allowing for accurate control of the motor’s position.

The control circuit uses a pulse width modulation (PWM) signal to control the position of the motor shaft. By varying the pulse width of the signal, the motor can be positioned to different angles within its range of motion.

Features and Advantages:

Compact Size: The SG90 servo motor’s small size makes it ideal for applications where space is limited.

Lightweight: Weighing only 9 grams, the SG90 servo motor is suitable for projects that require lightweight components.

Cost-Effective: The SG90 servo motor is an affordable option compared to larger and more powerful servo motors.

Ease of Use: It can be easily controlled with microcontrollers like Arduino, Raspberry Pi, or ESP32, making it accessible for hobbyists and beginners.

Applications:

The SG90 servo motor finds applications in various fields, including:

Robotics: The SG90 servo motor is commonly used in small-scale robotic projects to control the movement of robot arms, grippers, and other mechanical components.

RC (Remote Control) Vehicles: It is widely employed in RC cars, planes, boats, and drones to control steering mechanisms and throttle.

Camera Stabilization: The compact size and lightweight nature of the SG90 servo motor make it suitable for camera stabilization systems, such as gimbals.

Animatronics: It is utilized in animatronic projects to control movements of characters or robotic creatures.

Home Automation: The SG90 servo motor can be integrated into home automation systems for tasks like opening and closing curtains, controlling door locks, or adjusting light fixtures.

Educational Projects: Due to its affordability and ease of use, the SG90 servo motor is a popular choice in educational settings for teaching robotics and electronics concepts.



SG90 Servo Motor Wires:

Image illustrating the setup for controlling a servo motor using wires, an ESP32 microcontroller, WiFi connectivity, and Python programming. The image shows the servo motor connected to the ESP32 board via wires, with clear indications of the electrical connections. The Python code running on the ESP32 enables the control of the servo motor through WiFi

The SG90 servo motor typically has three wires, each serving a different purpose. Here’s an explanation of what each wire is used for:

Red Wire:

Power Supply (VCC) The red wire is connected to the positive terminal of the power supply. It typically requires a voltage between 4.8V and 6V to operate. Make sure not to exceed the maximum voltage specified by the manufacturer to prevent damage to the servo motor.

Brown or Black Wire:

Ground (GND) The brown or black wire is connected to the ground or negative terminal of the power supply. It completes the electrical circuit and provides the reference point for the motor’s operation.

Orange or Yellow Wire:

Signal Input (Control Signal) The orange or yellow wire is responsible for receiving the control signal from a microcontroller or other control devices. This wire is connected to a digital GPIO pin of the microcontroller, and it carries the pulse width modulation (PWM) signal that determines the position of the servo motor.

It’s important to note that some servo motors may have different color coding for the wires. However, the function of each wire remains the same: power supply, ground, and control signal.

When connecting the servo motor to a microcontroller or any control device, make sure to double-check the pin assignments and connect the wires accordingly. Incorrect wiring may result in unexpected behavior or damage to the servo motor. Always consult the servo motor’s datasheet or manufacturer’s documentation for accurate wiring instructions specific to your servo model.



SG90 Servo Motor Connection with ESP32:

Circuit diagram illustrating the control of an SG90 servo motor using an ESP32, WiFi, and Python programming. The diagram shows the various components and connections involved in the setup, including the ESP32 microcontroller, the SG90 servo motor, and the wireless WiFi connection

To connect an SG90 servo motor with an ESP32 microcontroller, you’ll need to establish the necessary electrical connections. Here’s a step-by-step explanation of how you can connect them:

First, identify the three wires of the SG90 servo motor: red, brown (or black), and orange (or yellow). The red wire is the power supply (VCC), the brown or black wire is the ground (GND), and the orange or yellow wire is the signal input (control signal).

To provide power to the servo motor, connect the red wire to a 5V pin on the ESP32. Ensure that the voltage does not exceed the maximum supported by the servo motor. This connection allows the servo motor to receive the necessary power for its operation.

Next, establish the ground connection. Connect the brown or black wire of the servo motor to a GND pin on the ESP32. This connection completes the electrical circuit and provides a common reference point for the servo motor and the microcontroller.

Finally, make the signal connection. Connect the orange or yellow wire of the servo motor to any available GPIO pin on the ESP32 as you can see I connected it to GPIO 13. It’s essential to choose a pin that supports PWM (pulse width modulation) functionality since servo motors are controlled using PWM signals. This connection enables the ESP32 to send the control signal to the servo motor, determining its position based on the received PWM signal.

If the servo motor requires more current than the ESP32 can provide, you may need to power it from a separate power supply. In that case, connect the ground of the external power supply to the GND pin on the ESP32 to ensure a common ground reference.




Esp32 code for controlling servo motor using Python GUI:

Code explanation:

This code includes the WiFi library and the ESP32Servo library, which provides functions to control a servo motor using an ESP32 board.

Here, a Servo object called servo is declared to control the servo motor. The ssid and password variables store the Wi-Fi network credentials to connect to. The port variable specifies the port number on which the server will listen for incoming connections. The server object represents the Wi-Fi server, and the client object is used to handle the client connections.

In the setup function, the serial communication is initialized at a baud rate of 9600. The ESP32 board connects to the specified Wi-Fi network using the provided credentials. The code then waits until the ESP32 is successfully connected to the Wi-Fi network. Once connected, the Wi-Fi server is started on the specified port.

The servo motor is attached to GPIO pin 13 using the servo.attach(13) function call. The initial angle of the servo motor is set to 0 degrees using servo.write(0). Serial messages are printed to indicate the status of the setup process, including the IP address of the ESP32.

The loop function is executed repeatedly after the setup function. It first checks if a client is available by calling server.available(). If a client is connected, it checks if the client is still connected using client.connected(). If the client is connected, the code reads data sent by the client.



Python Controlling GUI Examples:

Control Servo Motor using ESP32 Wifi and Python PyQt5 buttons:

Controlling servo motors is a fundamental aspect of robotics and automation. In this example, we will explore how to control a servo motor using an ESP32 microcontroller, Wi-Fi connectivity, and Python buttons. The ESP32’s built-in Wi-Fi capabilities allow us to control the servo motor remotely, adding an extra layer of convenience and flexibility to our projects.

Output:

Upon running the program, a window titled “Servo Control” will appear with the buttons and a label centered in the window. The buttons are styled with different background colors and sizes. The label at the top of the window displays the text “Servo Motor Controlling” in Arial font with a font size of 15.

Clicking the “0” button will send a command to the ESP32 microcontroller with an angle of 0 degrees. Similarly, clicking the “90” button will send a command with an angle of 90 degrees, and clicking the “180” button will send a command with an angle of 180 degrees. Each time a button is clicked, the program will attempt to establish a connection with the ESP32 using a socket connection over the specified IP address and port number. If the connection is successful, the program will send the command to the ESP32, and the command will be printed to the console.

If there is a “ConnectionRefusedError” while trying to connect to the ESP32, the program will print “Failed to connect to the ESP32” to the console.



Control Servo Motor using Esp32 Wifi and PyQt5 QDial:

To remotely control a servo motor using the esp32 and python PyQt5 QDial widget, you can create a GUI application that allows the user to adjust the servo motor’s position by rotating the dial. Here’s a step-by-step guide on how to implement this:

Step 1: Set up the PyQt5 Environment Make sure you have PyQt5 installed. You can install it using pip:

Step 2: Create a PyQt5 Application Create a new Python file, e.g., wifi.py, and open it in a text editor. paste the below Python PyQt5 Controlling GUI Program:

Output:

Upon running the program, a window titled “Servo Control” will appear. Inside the window, there will be a QDial widget and a QLabel. The QDial is a circular dial that allows the user to select an angle for the servo motor. It is styled to have a range from 0 to 180 degrees, with a single step increment of 1. The size of the QDial is increased to 200×200 pixels, and there are dots spaced around the dial.

The QLabel is positioned below the QDial and is initially empty. It is centered and has a minimum size of 200×50 pixels. The font size of the label is set to 20 pixels.

When the user changes the value of the QDial by rotating it, the program will attempt to establish a connection with the ESP32 using a socket connection over the specified IP address and port number. If the connection is successful, the program will send the selected angle as a command to the ESP32, and the command will be printed to the console. If there is a “ConnectionRefusedError” while trying to connect to the ESP32, the program will print “Failed to connect to the ESP32” to the console.

Regardless of the success or failure of the connection, the selected angle will be displayed in the QLabel below the QDial. The label will update dynamically as the user changes the angle using the QDial.



Control Servo Motor using ESP32 Wifi and PyQt5 textbox:

To remotely control a servo motor using PyQt5 and an ESP32 microcontroller, you can create a GUI application that allows the user to enter the desired position of the servo motor in a textbox. The ESP32 will receive this position value and control the servo motor accordingly.

Here’s a step-by-step guide on how to implement this:

Step 1: Set up the PyQt5 Environment Make sure you have PyQt5 installed. You can install it using pip:

Step 2: Create a PyQt5 Application Create a new Python file, e.g., wifi.py, and open it in a text editor. Paste the below python PyQt5 program to control the servo motor using esp32 wifi:

Output:

Upon running the program, a window titled “Servo Control” will appear. The window is positioned at coordinates (100, 100) on the screen and has dimensions of 300×300 pixels. Inside the window, there is a QLineEdit and a QPushButton.

The QLineEdit is a single-line text input field where the user can enter the desired angle for the servo motor. It is styled with a white background, black text, and rounded corners. It has a placeholder text “Enter angle (0-180)” to provide instructions to the user. When the QLineEdit is in focus, a green border is displayed around it. The font size of the text in the QLineEdit is set to 18 pixels.

The QPushButton is labeled “Send” and is styled with a dark green background, white text, rounded corners, and padding. When the mouse hovers over the button, it becomes slightly lighter in color, and when the button is pressed, it becomes slightly darker. The font size of the button text is set to 18 pixels.

The user can enter an angle between 0 and 180 in the QLineEdit. When the QPushButton is clicked, the program attempts to establish a connection with the ESP32 using a socket connection over the specified IP address and port number. If the connection is successful, the program converts the entered angle to an integer, sends it as a command to the ESP32, and prints the sent command to the console.

If the entered angle is not a valid integer or is outside the range of 0 to 180, an appropriate error message is printed to the console. If there is a “ConnectionRefusedError” while trying to connect to the ESP32, the program will print “Failed to connect to the ESP32” to the console.




Remotely Control Servo Motor using ESP32 wifi and PyQt5 slider:

Controlling a servo motor remotely is a common requirement in various projects, ranging from robotics to home automation. In this example, we will explore how to control a servo motor using a PyQt5 slider and an ESP32 connected via Wi-Fi. This combination allows for intuitive servo control through a graphical user interface (GUI) powered by PyQt5 on a computer or mobile device, while the ESP32 serves as a bridge between the GUI and the physical servo motor.

The ESP32, a powerful microcontroller with built-in Wi-Fi capabilities, acts as the intermediary device between the GUI and the servo motor. It connects to the local Wi-Fi network and creates an HTTP server to receive commands from the PyQt5 application. The PyQt5 application, developed using the Python programming language, presents a user-friendly interface with a slider widget that adjusts the servo motor’s position. When the slider is moved, the PyQt5 application sends the corresponding value to the ESP32 over Wi-Fi.

Output:

Upon running the program, a window titled “Servo Control” will appear. The window is positioned at coordinates (100, 100) on the screen and has dimensions of 300×300 pixels. Inside the window, there is a QLabel at the top, a QSlider in the middle, and another QLabel at the bottom.

The top QLabel displays the text “Servo Motor Controlling” and is centered. The font size of the label is set to 20 pixels using the Arial font.

The QSlider is a horizontal slider that allows the user to select a value for the servo motor. It has a range from 0 to 180, with a default value of 0. Tick marks are displayed below the slider, indicating values at intervals of 10. The appearance of the slider is styled using custom CSS-like code, with different colors for the groove, handle, and progress bar.

The bottom QLabel is initially empty but will display the current value of the slider. It is centered and uses a font size of 18 pixels.

When the user changes the value of the slider, the program updates the bottom QLabel to display the new value. Additionally, the program attempts to establish a connection with the ESP32 using a socket connection over the specified IP address and port number. If the connection is successful, the program converts the slider value to a string, sends it as a command to the ESP32, and prints the sent command to the console.

If there is a “ConnectionRefusedError” while trying to connect to the ESP32, the program will print “Failed to connect to the ESP32” to the console.



Conclusion:

In this article, we discussed how to control a servo motor through a Python GUI using an ESP32 Wi-Fi module. By establishing a wireless connection between the GUI and the ESP32, we were able to send commands to control the servo motor’s position remotely. This opens up possibilities for creating interactive and user-friendly interfaces for servo motor control, enabling automation and remote operation in various applications. With this knowledge, you can now explore and expand upon this project to suit your specific needs and integrate it into your own projects.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button