Skip to Content

How to Drive Servo Motors Using ESP32 – A Complete Tutorial

How to Drive Servo Motors Using ESP32 – A Complete Tutorial

A servo motor is an excellent choice for projects that require accurate and smooth movement. 

In this article, I will show you how to drive a servo motor using ESP32. ESP32 provides you the freedom to control and monitor systems remotely.

We will learn more about the basic working principles of servo motors, the types of servo motors you find, and the applications of servo motors. 

I have provided a step-by-step connection guide to connect an ESP32 to a Servo Motor.

You will find a working ESP32 and Servo motor code with a line-by-line explanation.

By the end of this article, you will also find a few frequently asked questions about the ESP32 and the servo motor interface.

So, grab your breadboard, and let’s get started.

(I also have an article on How To Control Servo Motors using Arduino if you want to work with an Arduino microcontroller instead.

Components Needed To Build ESP32 And Servo Motor Project

Hardware Components

Software

Guide

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.

Basics of The ESP32 And Servo Motor Projects

In this section, we will understand the basics of the Servo Motor and the features of ESP32 needed to complete the Servo motor project.

So, what makes the Servo motor so famous?

I opt for servo motors when there is a need for precision, accuracy, and ease of use.

You can find servo motors in many applications, such as robotics, manufacturing, aerospace, and even the automotive industry. 

It is easy to control servo motors. You can use a simple PWM signal to control the servo motor’s position.

Hence, the servo motor is famous among makers, hobbyists, and DIY enthusiasts., like you!

Even when loaded, the Servo motor can maintain the position precisely.

An auto-correct feature inside the servo motor measures the motor position to the input signal. I will explain that shortly. 

A basic servo motor is shown below here.

basic servo motor

The Servo motor has three pins

  1. Power pin – Connect this to the 5 V supply pin of the ESP32
  2. GND pin – Connect this to the ground pin of the ESP32 board
  3. Signal pin – Connect a PWM pin on ESP32 to the signal pin

You control the servo motor using PWM-compatible pins. To maintain the servo motor angle, you have to vary the high pulse width of the PWM signal.

Typical characteristics of PWM signal needed to drive a servo motor are here:

  1. The frequency of the PWM signal is 50 Hz (40 Hz to 400 Hz range)
  2. The high pulse width of 1 ms indicated 0-degree rotation
  3. The high pulse width of 1.5 ms indicates 90-degree rotation
  4. The high pulse width of 2 ms indicates 180-degree rotation

The below image shows a typical PWM signal used to drive the servo motor.

These servo motors can turn from 0 degrees to 180 degrees and vice-versa. You can opt for 360-degree servo motors if you need a full rotation. 

The 360-degree servo motors can rotate completely 360 degrees and in either direction. In this case, you can control the rotation’s speed and direction but not the position. 

There are also servo motors that provide you with an additional feedback signal.

The servo motor provides an analog voltage signal directly proportional to the rotation angle. 

The feedback signal helps ESP32 ensure the motor has rotated to the desired position.

An example use case where feedback is critical is when there is insufficient power for the servo motor or an overload condition. 

Overall, the servo motor is effortless to drive once you understand the basics.

Let’s see the PWM capabilities of the ESP32 module.

ESP32 has 16 PWM controllers built in. All GPIOs which can act like an output can work as PWM pins.

Hence, driving a single servo motor is not a big task for ESP32. 

Almost all pins of ESP32 share more than one function.

Hence make sure that the pin you prefer to use for Servo motor is free and has PWM functionality.

In the image above, the PWM pins are those that have a bent pin. 16 PWM channels ideally mean you can control 16 servo motors. 

You can find servo motors in several applications such as:

  1. Robotics – high precision and accuracy makes servo motors first choice in robot arm movement
  2. Industrial automation – Control machinery, conveyor belt, speed controlling of packing machines etc
  3. Aerospace – Controlling landing gear, control surfaces and more
  4. Medical equipments – Servo motors are used in medical equipment, surgical robots etc

I will show you how to connect a servo motor to an ESP32 in the next section.

Step-By-Step Instructions To Connect The Servo Motor With ESP32

In this section, we will build a project using ESP32 and a Servo motor. Since we use the PWM line to control the servo motor, you can complete the connections with fewer wires.

Let’s get started with the hardware connections!

How To Connect The Servo Motor With ESP32?

Below is the step-by-step connection guide to complete the ESP32 and Servo motor connection.

Step 1: Start with the GND connections

Start with the GND connections

Connect the GND pin of the ESP32 module to the GND of the Servo motor. Choose any GND pins available on the ESP32 for the connection.

It is an excellent practice to start with the GND connections.

Step 2: Connect the PWM line

Connect the PWM line

Connect any GPIO that is a PWM compatible pin to the signal pin of the Servo. I am using GPIO18 in this example. 

Step 3: Connect the Power Pin

Connect the Power Pin

Connect the power pin of the servo motor to the supply pin of the ESP32. If the servo motor demands higher current, you can use a dedicated power supply. 

Step 4: Connect the potentiometer to the ESP32

Connect the potentiometer to the ESP32

Let’s connect a potentiometer to the ESP32 so that we can control the servo motor angle easily.

We will program ESP32 to read the analog value from the potentiometer and drive the servo motor to the corresponding angle.

Step 5: The complete connection

The complete connection

Congratulations. This completes all the required connections among the servo motor, potentiometer, and ESP32.

In the coming section, we will go through the code which you can program to verify the connections.

⚡ESP32 Code Example For The Servo Motor Project

In this section, you can find the complete ESP32 code to test the servo motor and potentiometer connections you have just completed in the previous section. 

Please follow the instructions given in the guide to install the ESP32 core on Arduino UNO. 

This program controls the servo motor. It performs the sweeping action from 0 to 180 degrees and back to zero degrees.

 /* Sweep
  by BARRAGAN 
  This example code is in the public domain.

  modified 8 Nov 2013
  by Scott Fitzgerald

  modified for the ESP32 on March 2017
  by John Bennett

  see http://www.arduino.cc/en/Tutorial/Sweep for a description of the original code
*/

#include "ESP32Servo.h"

Servo myservo;  // create servo object to control a servo
// 16 servo objects can be created on the ESP32

int pos = 0;    // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
int servoPin = 13;

void setup() {
  // Allow allocation of all timers
  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);
  myservo.setPeriodHertz(50);    // standard 50 hz servo
  myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 18 to the servo object
  // using default min/max of 1000us and 2000us
  // different servos may require different min/max settings
  // for an accurate 0 to 180 sweep
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    delay(15);          // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    delay(15);          // waits 15ms for the servo to reach the position
  }
}

Let’s walk through the code.

#include "ESP32Servo.h"

This line includes the ESP32Servo library, which provides the Servo class for controlling servo motors.

Servo myservo; 

This line declares a Servo object called myservo. The object is used to control the servo motor. 

In the setup() function, all the instructions will be executed only once (after power and after every reset. 

In the next few lines we are allocating the available timers on the ESP32. The time period is set to 50 Hz (typical for a hobby servo motor).

  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);
  myservo.setPeriodHertz(50);    // standard 50 hz servo
  myservo.attach(servoPin, 500, 2400);

The parameters for the attach() function specify the pin number, the minimum pulse width in microseconds (500), and the maximum pulse width in microseconds (2400).

The loop function is repeatedly called after the setup function has been completed.

In this code, the loop function sweeps the servo motor from 0 to 180 degrees and back.

FAQs About The ESP32 And Servo Motor Projects

I have included a list of the most frequently asked questions about projects built using ESP32 and the servo motor module.

If you have more questions, please post them in the comments section.

I will be glad to answer them.

1. What is an ESP32?

ESP32 is a low-cost, low-power system-on-chip controller. It has integrated WiFi and Bluetooth capability. 

2. Can I control a Servo motor with an ESP32?

You can control a Servo motor with an ESP32 using a Servo library. The Servo motor needs a PWM signal.

ESP32 supports PWM signal on all of its GPIO outputs. 

3. What Servo library should I use with my ESP32?

The ESP32Servo library is popular for controlling Servo motors with an ESP32. You can refer to the example in the article for usage.

4. How do I connect a Servo motor to my ESP32?

Connect the Servo motor’s signal wire to a PWM-capable GPIO pin on the ESP32.

5. How can I control the speed of my Servo motor?

You can control the speed of your Servo motor by changing the delay time in your code.

A longer delay will result in slower movement. A shorter delay will result in faster movement. 

6. How can I control multiple Servo motors with my ESP32?

You can control multiple Servo motors with your ESP32 by creating multiple Servo objects in your code and assigning them to different GPIO pins.

You can use all the GPIOs that have output functions as PWM pins. There are 16 PWM drivers in an ESP32.

Conclusion

In this article, I have covered all the essential information required to connect a Servo motor to the ESP32. 

I have provided a step-by-step connection guide, working ESP32 code, and FAQs about the Servo motor project. 

You should now be able to connect a servo motor to an ESP32, control the angle, and build bigger projects confidently. 

What projects are you working on that involve servo motors? 

Did you find any section challenging to understand? 

Are there any points you would love to read further?

I’d love to hear from you – let us know in the comments section below if there’s anything else you’d like me to cover in future articles or if I can do anything to improve this one.