ESP32

SG90 Servo Motor with ESP32 Interfacing and Programming

Introduction

The ESP32 microcontroller has revolutionized the field of embedded systems with its powerful features and versatility. One of the exciting applications of the ESP32 is its ability to interface and control servo motors effectively. SG90 Servo motor is widely used in robotics, automation, and other projects that require precise control over angular motion. In this article, we will delve into the world of SG90 servo motor with ESP32 interfacing and programming, exploring the fundamental concepts, wiring connections, and code examples to get you started on your servo motor projects.

Throughout this guide, we will cover the essential aspects of working with servo motors using the ESP32. Firstly, we will provide an overview of servo motors, explaining their construction, operating principles, and the different types available in the market. Understanding the basics of servo motors is crucial for grasping the subsequent sections where we explore how to interface them with the ESP32.




Amazon link:

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!

Overview of SG90 Servo Motor:

An image depicting a servo motor wire. The wire is black and consists of multiple thin, flexible strands. It is connected to a small, rectangular plug on one end, which is designed to fit into a corresponding socket. The wire extends outward from the plug, showing its length and flexibility. The other end of the wire is not visible in the image. The wire is neatly arranged and held together, suggesting its organized and structured nature.

Servo motors are widely used in various applications that require precise control over angular motion. They are commonly employed in robotics, automation, remote control systems, and other projects where accurate positioning is crucial. Unlike DC motors that continuously rotate, servo motors are designed to rotate to a specific angle and maintain that position until instructed otherwise.

Construction:

An image displaying a 3D representation of a servo motor's construction. The 3D model shows a servo motor with its outer casing removed, revealing its internal components. The main focus is on the control circuit board, which is prominently featured. The circuit board consists of various electronic components, such as resistors, capacitors, integrated circuits, and connectors, arranged in a compact and organized manner. The image showcases the intricate design and complexity of the control circuit board, which is crucial for the servo motor's operation and precise control.

Servo motors consist of several key components. The main components include a DC motor, a control circuit, a feedback mechanism (usually a potentiometer), and a gearbox. The DC motor drives the rotation, while the control circuit and feedback mechanism work together to accurately position the motor shaft. The gearbox helps increase torque while reducing the rotational speed.



Operating Principles:

Servo motors operate based on the principle of closed-loop control. The control circuit receives input signals, typically in the form of pulse width modulation (PWM), which determine the desired position of the motor shaft. The control circuit compares the desired position with the feedback received from the potentiometer, and it adjusts the motor’s rotation accordingly to achieve the desired position. This closed-loop system allows for precise positioning control.

SG90 Servo Motor Specifications:

Operating Voltage: 4.8V – 6.0V

Stall Torque: 1.8 kg/cm (4.8V), 2.2 kg/cm (6.0V)

Operating Speed: 0.1 sec/60° (4.8V), 0.08 sec/60° (6.0V)

Control System: Analog

Angle of Rotation: 180°

Dead Band Width: 5 µs

Dimensions: 22.2 x 11.8 x 31 mm

Weight: 9 grams

Gear Type: Plastic

Motor Type: Coreless

Connector Type: JR/Futaba

Operating Temperature: -30°C to +60°C

PWM Signal: 1 ms to 2 ms

SG90 Servo Motor Features:

Compact Size: The SG90 servo motor has a small and lightweight design, making it suitable for applications where space is limited or weight is a concern.

High Torque: With a stall torque of 1.8 kg/cm (4.8V) and 2.2 kg/cm (6.0V), the SG90 provides a decent amount of torque for its size.

Precise Positioning: The servo motor offers a 180° angle of rotation, allowing for accurate positioning of objects or mechanisms.

Quick Response: It has a fast operating speed of 0.1 sec/60° (4.8V) and 0.08 sec/60° (6.0V), enabling rapid and responsive movement.

Durable Gear Train: The SG90 features a plastic gear train that provides a good balance between durability and cost-effectiveness.

Coreless Motor: The motor design eliminates the need for a traditional iron core, resulting in improved efficiency, reduced weight, and smoother operation.

Wide Operating Voltage Range: It can be powered within the range of 4.8V to 6.0V, making it compatible with various power sources and systems.

Standard Connector: The servo motor comes with a JR/Futaba compatible connector, which is commonly used in RC (radio-controlled) hobby applications.

Operating Temperature Range: The SG90 is designed to operate reliably in temperatures ranging from -30°C to +60°C, making it suitable for a wide range of environments.

Easy Control: The servo motor can be controlled using a PWM (Pulse Width Modulation) signal, with a pulse duration between 1 ms to 2 ms determining the position.



Types of Servo Motors:

There are various types of servo motors available, each designed for specific applications. Some common types include:

  • Standard Servo Motor: These are the most common type of servo motors and are widely used in hobbyist projects. They provide moderate torque and angular rotation.
  • Continuous Rotation Servo Motor: Unlike standard servo motors, continuous rotation servos can rotate continuously in either direction, allowing for continuous motion control.
  • Micro Servo Motor: Micro servos are small-sized servo motors, ideal for projects with limited space or weight constraints. They offer lower torque and angular rotation compared to standard servos.
  • High-Torque Servo Motor: High-torque servos are designed to provide increased torque, making them suitable for applications that require more power.
  • Digital Servo Motor: Digital servos offer enhanced performance and precision compared to analog servos. They often come with additional features such as programmability and improved feedback resolution.

Servo motors are commonly controlled using a standard pulse width modulation (PWM) signal, where the pulse width corresponds to the desired position. Interfacing servo motors with microcontrollers like the ESP32 allows for seamless integration into various projects and opens up endless possibilities for precise motion control.



Servo Positioning

Servo motor positioning refers to the ability to control and set the precise angle or position of a SG90 servo motor shaft. Unlike regular DC motors that continuously rotate, servo motors are designed to move to specific positions and hold them accurately. This positioning capability makes servo motors ideal for applications requiring precise control over angular motion, such as robotics, automation, remote control systems, and more.

SG90 servo motor positioning is achieved by sending control signals to the servo motor, specifying the desired angle or position. These control signals are typically in the form of pulse width modulation (PWM) signals. The duration of the pulse width corresponds to the desired position of the servo motor shaft.

The range of motion for a typical SG90 servo motor is usually between 0 to 180 degrees, although some servo motors may have a limited range depending on the specific model. By varying the pulse width of the control signal, the SG90 servo motor can be positioned at different angles within its range.

An image illustrating the positioning of an SG90 servo motor from 0 to 180 degrees. The servo motor is shown in its physical form, with a small black body and a protruding shaft. The image highlights the movement of the shaft as it rotates from its initial position (0 degrees) to its maximum rotation (180 degrees). The movement is depicted visually, either through an animation or a series of sequential images, capturing the servo motor's ability to accurately position itself at various angles within its range of motion.

When controlling SG90 servo motor positioning, it is important to consider a few key aspects:

  • Pulse Width: The pulse width determines the position of the servo motor shaft. A pulse width of 1 millisecond (ms) typically corresponds to the minimum angle (e.g., 0 degrees), while a pulse width of 2 ms corresponds to the maximum angle (e.g., 180 degrees). Intermediate pulse widths map to the angles in between.
  • Neutral Position: The neutral position is the center point of the servo motor’s range (e.g., 90 degrees). A pulse width of around 1.5 ms is commonly used to set the servo motor to the neutral position.
  • Control Resolution: The control resolution refers to the level of precision or granularity with which the servo motor position can be set. It depends on the resolution of the control signal and the internal feedback mechanism of the servo motor. Typically, a higher control resolution allows for finer positioning control.

To control the positioning of a servo motor, you can use a microcontroller or servo controller that generates the appropriate PWM signals. By adjusting the pulse width of the control signal within the range supported by the servo motor, you can move the motor to the desired angle.

Programming languages and libraries, such as Arduino with the Servo library, provide convenient functions to set the SG90 servo motor position using code. These libraries abstract the details of generating the PWM signals, allowing you to focus on specifying the desired angles for positioning.

By accurately controlling the pulse width of the control signal, servo motor positioning enables precise and repeatable control over the angular motion, making it an essential feature in various applications where accurate positioning is crucial.




About SG90 servo motor wires

An image showing a bundle of colorful wires connected to a servo motor. The wires are neatly organized and labeled with different colors, including red, blue, green, and yellow. They extend from the motor and fan out, indicating various connections. The wires are tightly secured with connectors or soldered joints, ensuring a reliable electrical connection. This image highlights the complexity and precision involved in wiring a servo motor for control and functionality.

SG90 Servo motors typically have three wires, each serving a specific purpose. Here’s a brief overview of the functions of these wires:

Power Supply Wire (Vcc):

This wire is responsible for providing the operating voltage to the servo motor. The voltage required can vary depending on the specific servo motor, but common voltages are 5V or 6V. It is important to supply the servo motor with the appropriate voltage to ensure proper operation.

Ground Wire (GND):

The ground wire serves as the reference point for the electrical circuit. It is connected to the ground or 0V of the power supply. The ground wire completes the electrical circuit and provides the return path for the current flowing through the motor.

Control Signal Wire (typically colored yellow or white):

This wire carries the control signal from the microcontroller or servo controller to the servo motor. The control signal is typically a pulse width modulation (PWM) signal that determines the desired position of the servo motor shaft. The duration of the pulse width corresponds to the desired angle of rotation.

It is essential to correctly connect these wires to the respective pins on the microcontroller or servo controller for proper functioning. In most cases, the power supply wire (Vcc) connects to the 5V or 6V pin, the ground wire (GND) connects to the ground pin, and the control signal wire connects to a digital output pin capable of generating PWM signals.

Additionally, some SG90 servo motors may have an additional wire for the feedback signal, which provides information about the current position of the motor shaft. This wire is not required for basic servo motor operation and control but can be utilized for advanced applications that require feedback.

Understanding the purpose and correct wiring of these servo motor wires is crucial for successful interfacing and control with the ESP32 or any other microcontroller platform.



SG90 servo motor with ESP32 Circuit diagram

An image depicting a circuit diagram that showcases the connection between an SG90 servo motor and an ESP32 microcontroller. The diagram is represented by various electrical symbols and lines indicating the different components and their interconnections. The SG90 servo motor is represented by its distinctive shape, with three wires extending from it. The ESP32 microcontroller is also represented by its symbol, typically a rectangular shape with pins or connectors. The diagram illustrates the electrical connections between the servo motor and the microcontroller, demonstrating how they are wired together for control and communication.

The control signal wire, typically colored yellow or white, from the SG90 servo motor is connected to GPIO13 on the ESP32. GPIO13 is a General-Purpose Input/Output pin on the ESP32, capable of generating PWM (Pulse Width Modulation) signals required for SG90 servo motor control. By connecting the control signal wire to GPIO13, you enable the ESP32 to send control signals to the servo motor and control its position.

The power supply wire (Vcc) of the SG90 servo motor is connected to the 5V pin on the ESP32. This provides the SG90 servo motor with the necessary power supply voltage. It’s crucial to ensure that the voltage supplied by the 5V pin matches the operating voltage requirements of the servo motor. Most servo motors operate at 5V, but it’s important to check the specifications of your specific servo motor to confirm the voltage requirement.

The ground wire (GND) of the servo motor is connected to the GND (ground) pin on the ESP32. This connection provides a common ground reference between the ESP32 and the servo motor, completing the electrical circuit. It ensures that both devices share the same ground reference and enables proper communication and operation.

By establishing these connections, you have enabled the ESP32 microcontroller to control the servo motor’s position by sending control signals through GPIO13. The 5V power supply connection ensures the servo motor receives the necessary power, while the ground connection completes the electrical circuit.

Please note that while the description above provides a general understanding of the connections, it’s always important to consult the datasheets, pin diagrams, and documentation of your specific servo motor and ESP32 board to ensure accurate wiring and compatibility.



Installing SG90 Servo Motor ESP32 Arduino Library

To install the ESP32 Arduino Servo library, you can follow these steps:

Open the Arduino IDE (Integrated Development Environment) on your computer.

An image showcasing the Arduino IDE open on a computer screen. The IDE interface is visible, with its distinctive white background and various menu options. The user has navigated to the library management section, specifically the Library Manager, which is displayed on the screen. The Library Manager window is open, ready for the user to search and install the ESP32 Servo Motor library. The image emphasizes the initial step of accessing the IDE and preparing to install the library for ESP32 servo motor functionality.

Navigate to “Sketch” in the menu bar and select “Include Library” -> “Manage Libraries.”

An image showcasing the Arduino IDE open on a computer screen. The IDE interface is visible, with its distinctive white background and various menu options. The user has navigated to the library management section, specifically the Library Manager, which is displayed on the screen. The Library Manager window is open, ready for the user to search and install the ESP32 Servo Motor library. The image emphasizes the initial step of accessing the IDE and preparing to install the library for ESP32 servo motor functionality.

In the Library Manager window, you will see a search bar at the top right corner. Enter “ESP32Servo” in the search field.

An image showcasing the Arduino IDE open on a computer screen. The IDE interface is visible, with its distinctive white background and various menu options. The user has navigated to the library management section, specifically the Library Manager, which is displayed on the screen. The Library Manager window is open, ready for the user to search and install the ESP32 Servo Motor library. The image emphasizes the initial step of accessing the IDE and preparing to install the library for ESP32 servo motor functionality.

The library you are looking for should appear in the search results. Click on the library entry. You will see an “Install” button next to the library. Click on it to start the installation process. But in my case I already installed it.

Once the installation is finished, you can close the Library Manager window.

The ESP32 Arduino Servo library should now be installed and ready for use in your Arduino sketches. You can include the library in your code by adding the following line at the beginning of your sketch:

#include <ESP32Servo.h>

With the library installed, you will have access to the necessary functions and features to control servo motors using the ESP32 microcontroller. You can proceed with writing your code to control the servo motor’s position and movement using the library’s functions.

Note: Make sure you have an active internet connection while installing the library from the Arduino Library Manager, as it requires downloading the library files from the internet.



SG90 Servo Motor with ESP32 Programming:

Program explanation:

This line includes the library “ESP32Servo.h” that provides the necessary functions to control servo motors using the ESP32 microcontroller.

This line declares a variable named “myServo” of type “Servo,” which represents the servo motor. It creates an instance of the Servo class that will be used to control the servo motor.

This line declares a constant integer variable named “servoPin” and assigns the value 13 to it. It represents the GPIO pin number on the ESP32 board that is connected to the servo motor.




This function is the setup function that is executed once at the beginning of the program. It attaches the servo motor to the GPIO pin specified by “servoPin” using the “attach” method of the Servo class.

this is the main loop of the program, which continuously executes after the setup function. It controls the servo motor by setting its position to three different angles: 0 degrees, 90 degrees, and 180 degrees.

In each iteration of the loop, myServo.write() is called with the desired angle as the argument to move the servo to that position.

After each movement, a delay() of 1000 milliseconds (1 second) is used to pause the program before moving to the next position.

By uploading this code to the ESP32, the servo motor will move sequentially from the starting position to the middle position and then to the ending position, repeatedly. The delay between each movement ensures that the servo motor stays at each position for 1 second before moving to the next one.



output:

Related Articles

Leave a Reply

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

Back to top button