ESP32 - Servo Motor

In this tutorial, we are going to learn:

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Servo Motor
3×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Servo Motor

Servo motor is a component that can rotate its handle (usually between 0° and 180°). It used to control the angular position of the object.

Pinout

The servo motor used in this example includes three pins:

  • VCC pin (typically red) needs to be connected to VCC (5V)
  • GND pin (typically black or brown) needs to be connected to GND (0V)
  • Signal pin (typically yellow or orange) receives the PWM control signal from an ESP32's pin.
  • Servo Motor Pinout

How It Works

This section is the in-depth knowledge. DON'T worry if you don't understand. Ignore this section if it overloads you, and come back in another day. Keep reading the next sections.

After connecting VCC pin and GND pin to 5V and 0V, respectively, we can control the servo motor by generating proper PWM signal to signal pin.

The angle is determined by the width of PWM signal.

Datasheet of the servo motor provides us the following parameters:

  • Period of PWM (PERIOD)
  • Minimum width of PWM (WIDTH_MAX)
  • Maximum width of PWM (WIDTH_MIN)

These parameters are fixed in ESP32 Servo library. We do NOT need to know the value of parameters.

The angle is determined as follows:

  • If PWM's width = WIDTH_MIN, the servo motor rotates to 0°.
  • If PWM's width = WIDTH_MAX, the servo motor rotates to 180°.
  • If PWM's width is between WIDTH_MIN and WIDTH_MAX, the servo motor rotates to angle between 0° and 180° in proportion.
  • How Servo Motor Works

ESP32 - Servo Motor

Some of ESP32 pins can be programmed to generate PWM signal. We can control the servo motor by connecting the servo motor's signal pin to an ESP32's pin, and programming to generate PWM on the ESP32's pin.

Thanks to ESP32 Servo library, controlling servo motor is a piece of cake. We even do NOT need to know how servo motor works. We also do NOT need to know how to generate PWM signal. We JUST need to learn how to use the library.

Wiring Diagram

ESP32 Servo Motor Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program For Servo Motor

  • Include the library:
#include <Servo.h>
  • Declare a Servo object:
Servo myServo;

If you control more than one servo motors, you just need to declare more Servo objects:

Servo myServo1; Servo myServo2;
  • Set the control pin of ESP32, which connects to the signal pin of the servo motor. For example, pin 9:
myServo.attach(9);
  • Lastly, rotate the angle of the servo motor to the desired angle. For example, 90°:
myServo.write(90);

ESP32 Code

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE, select the right ESP32 board and COM port
  • On Arduino IDE, Go to File Examples Servo Sweep example
/* Sweep by BARRAGAN <http://barraganstudio.com> This example code is in the public domain. modified 8 Nov 2013 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Sweep */ #include <Servo.h> Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } 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 } }
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • How to upload ESP32 code on Arduino IDE
  • See the result: Servo motor rotates slowly about 180° in clockwise and counter-clockwise direction

Code Explanation

Read the line-by-line explanation in comment lines of code!

How to Control Speed of Servo Motor

By using map() and millis() functions, we can control the speed of servo motor smoothly without blocking other code

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-servo-motor */ #include <Servo.h> Servo myServo; unsigned long MOVING_TIME = 3000; // moving time is 3 seconds unsigned long moveStartTime; int startAngle = 30; // 30° int stopAngle = 90; // 90° void setup() { myServo.attach(9); moveStartTime = millis(); // start moving // TODO: other code } void loop() { unsigned long progress = millis() - moveStartTime; if (progress <= MOVING_TIME) { long angle = map(progress, 0, MOVING_TIME, startAngle, stopAngle); myServo.write(angle); } // TODO: other code }

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Challenge Yourself

Use the servo motor to do one of the following projects:

Additional Knowledge

  • The Servo library supports up to 12 motors on ESP32 UNO and 48 on the ESP32 Mega.
  • Power from 5V pin of ESP32 maybe NOT enough for servo motor in one of the following cases:
    • Using a high-torque servo motor, which can carry a high-weight thing.
    • Using many servo motors.

In these cases, we may need to provide an extra power source for servo motor.

ESP32 Servo Motor Extra Power

Image is developed using Fritzing. Click to enlarge image

As we can see in the above diagram, the VCC pin of servo motor doest NOT connect to the 5V pin of ESP32. It connects to the positive pin of an extra power source. The negative pin of extra power source is connected to the GND pin of ESP32.

Alternative Servo Motors

The above code also works with the following servo motors:

Servo Motor on Commercial Products

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere