Language: EN

esp32-servo

How to use a servo with an ESP32

A servo motor, also known simply as a servo, is an electromechanical device used to control the angular position of a specific axis to a specific angle.

Unlike conventional motors, servos are closed-loop devices. That means they contain sensors and internal circuits to receive control signals and adjust their position to the angle we indicate.

Servos are widely used in robotics projects, 3D printers, remote-controlled vehicles, robotic arms, and other systems that require precise motion control.

Using a servo on the ESP32 with the Arduino IDE is more complicated than with a conventional Arduino because the ESP32 Core for Arduino does not include a version of the Servo library.

Connecting the servo to the ESP32

Before starting to control a servo with the ESP32, we must connect it correctly. A typical servo has three wires: one for power (VCC), one for ground (GND), and one for signal control (signal).

Here is the pin configuration for the servo:

  • Red wire (VCC): Connect to a 5V pin on the ESP32 or an external 5V power source.
  • Brown or black wire (GND): Connect to a GND pin on the ESP32.
  • Yellow or white wire (signal): Connect to one of the GPIO pins on the ESP32.

Unless with very small servos, you should not power the servo from the ESP32. Even with very small servos, it is always better to power it from another source.

Servo Library for the ESP32

As we mentioned, unlike a “conventional” Arduino, the ESP32 Core does not include a native library for controlling servos.

Instead, we will have to download one. Fortunately, the community has developed several libraries. For example, I like this one GitHub - madhephaestus/ESP32Servo: Arduino-compatible servo library for the ESP32

This one is also well known. Although I like it less because it has less compatibility with ESP32 variants GitHub - RoboticsBrno/ServoESP32: Generate RC servo signal

In any case, be attentive to the compatibility with your ESP32 model, as not all libraries are compatible with all models.

How to use a Servo with an ESP32

Include the Servo library

To start, we must include the Servo library in our program. Depending on your library, you should add one of the following lines at the beginning of your code:

// for the madhephaestus library
#include <Servo.h>

// for the RoboticsBrno library
#include <ESP32Servo.h>

From here, using the servo should be the same as what we are used to. When in doubt, consult the documentation and examples of the library you are using.

Create a Servo object

After including the library, create a Servo object to interact with the servo. This is done with the following line of code:

Servo myServo;

Configure the servo pin

Before using the servo, we need to tell the Servo library which pin it is connected to. Use the attach() function to do this:

const int servoPin = 18; // GPIO pin to which the servo is connected

void setup() 
{
  myServo.attach(servoPin);
}

Control the servo

Once you have set up the pin, you can control the servo using the write() function. The value passed as an argument to write() represents the angle at which you want the servo to move. For example, a value of 0 degrees will move the servo to its leftmost position, while a value of 180 degrees will move it to its rightmost position.

void loop() 
{
  myServo.write(0);    // Move the servo to 0 degrees
  delay(1000);         // Wait for 1 second
  myServo.write(90);   // Move the servo to 90 degrees
  delay(1000);         // Wait for 1 second
  myServo.write(180);  // Move the servo to 180 degrees
  delay(1000);         // Wait for 1 second
}

In this example, the servo moves to the positions of 0, 90, and 180 degrees with an interval of 1 second between each movement.