Language: EN

esp32-servo

How to use a servo with an ESP32

A servomotor (also known simply as a servo) is an electromechanical device used to control the angular position of a specific axis at a specific angle.

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

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

Using a servo with the ESP32 in the Arduino IDE is somewhat 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 we start controlling a servo with the ESP32, we need to connect it properly. A typical servo has three wires: one for power (VCC), another for ground (GND), and one for the control signal (signal).

Here is the pin configuration for the servo:

  • Red wire (VCC): Connect to a 5V pin on the ESP32 or to an external 5V power supply.
  • 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.

Except for very small servos, you should not power the servo from the ESP32. In fact, even with very small servos, it is always better to power them from another source.

Servo Library for the ESP32

As we mentioned, unlike a “conventional” Arduino, the ESP32 core does not include a native library to control servos.

Instead, we will need 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 lower compatibility with ESP32 variants GitHub - RoboticsBrno/ServoESP32: Generate RC servo signal.

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

Both libraries have their peculiarities and additional functions. Check their documentation if you want to dive deeper into them.

How to use a Servo with an ESP32

Include the Servo library

To get started, we need to 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 <ESP32Servo.h>

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

From here, the usage of 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 you have chosen, create a Servo object to interact with the servo. This is done with the following line of code:

Servo miServo;

Which works with both libraries I recommended.

Configure the servo pin

Before we start 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 pinServo = 18; // GPIO pin to which the servo is connected

void setup() 
{
  miServo.attach(pinServo);
}

Control the servo

Once you have configured the pin, you can control the servo using the write() function. The value passed as an argument to write() represents the angle you want the servo to move to. For example, a value of 0 degrees will move the servo to its farthest left position, while a value of 180 degrees will move it to its farthest right position.

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

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