ESP8266 - Servo Motor

This tutorial instructs you how to use ESP8266 to control servo motor. In detail, we will learn:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Servo Motor
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Servo Motor

A servo motor is a component capable of rotating its shaft (usually within a range of 0 to 180 degrees) to control the angular position of an object.

The Servo Motor Pinout

The servo motor has three pins:

  • VCC pin (usually red) must be connected to VCC (5V)
  • GND pin (usually black or brown) must be connected to GND (0V)
  • Signal pin (usually yellow or orange) which receives the PWM control signal from an ESP8266's pin.
servo motor pinout

ESP8266 - Servo Motor

Some of the ESP8266 pins can be programmed to produce a PWM signal. Connecting the servo motor's signal pin to an ESP8266 pin and programming it to generate PWM on the same pin allows us to control the servo motor.

Thanks to the ESP8266 Servo library, controlling a servo motor is easy. We do not need to understand how the servo motor works or how to generate a PWM signal. All we need to do is learn how to use the library.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Servo Motor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

For the sake of simplicity, the above wiring diagram is utilized for testing or learning purposes, and for a small-torque servo motor. In actuality, we strongly suggest using an external power supply for the servo motor. The wiring diagram below illustrates how to connect the servo motor to an external power source.

The wiring diagram between ESP8266 NodeMCU and servo motor external power supply

This image is created using Fritzing. Click to enlarge image

How To Program For Servo Motor

The Arduino-ESP8266 core already comes with its built-in servo library, so we do not need to install it.

  • Include the library:
#include <Servo.h>
  • Create a Servo object:
Servo servo;

If you have more than one servo motor, simply declare additional Servo objects:

Servo servo1; Servo servo2;
  • Assign the control pin of ESP8266 to the signal pin of the servo motor. For instance, pin D7:
servo.attach(D7);
  • Finally, turn the servo motor to the required angle. For instance, 90°
servo.write(90);

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-servo-motor */ #include <Servo.h> Servo servo; // create servo object to control a servo int pos = 0; // variable to store the servo position void setup() { servo.attach(D7); // attaches the servo on pin D7 to the servo object } void loop() { for (pos = 0; pos <= 180; pos += 1) { // rotate from 0 degrees to 180 degrees // in steps of 1 degree servo.write(pos); // tell servo to go to position in variable 'pos' delay(10); // waits 10ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // rotate from 180 degrees to 0 degrees servo.write(pos); // tell servo to go to position in variable 'pos' delay(10); // waits 10ms for the servo to reach the position } }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect your ESP8266 to the PC using a USB cable.
  • Launch the Arduino IDE, select the correct board and port.
  • Copy the code provided and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the ESP8266.
How to upload code to ESP8266 NodeMCU using Arduino IDE
  • Check out the outcome: The servo motor moves in a clockwise and counter-clockwise direction.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

How to Control Speed of Servo Motor

Utilizing the map() and millis() functions, we can regulate the speed of the servo motor in a smooth manner without hindering other code.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-servo-motor */ #include <Servo.h> Servo servo; unsigned long MOVING_TIME = 3000; // moving time is 3 seconds unsigned long move_start_ms; int angle_start = 30; // 30° int angle_stop = 90; // 90° void setup() { servo.attach(D7); move_start_ms = millis(); // start moving // TODO: other code } void loop() { unsigned long progress = millis() - move_start_ms; if (progress <= MOVING_TIME) { long angle = map(progress, 0, MOVING_TIME, angle_start, angle_stop); servo.write(angle); } // TODO: other code }

Video Tutorial