ESP8266 Servo Motor Control

In this tutorial ESP8266 we will see How to control Servo motor using NodeMCU web server? where the position of the Servo Motor can be controlled through a Web Page (within the same WiFi Network).

Servo motor control using NodeMCU


Servo Motors are one of the most commonly used motors in both industries and DIY Projects.

I’ll be creating a simple Web Page (HTML) with a circular knob. When this web page is accessed from a laptop or mobile phone that is connected to the same WiFi Network as the ESP8266, you can control the position of the Servo Motor by adjusting the knob.

Web Controlled Servo using ESP8266

Normally, in order to control a Servo Motor with Arduino (for example), all you need is a Servo Motor, Arduino and a Potentiometer. Depending on the position of the POT, the PWM value of the Servo Motor input changes and consequently the position of the Servo Motor’s shaft changes.

In case of a Web Controlled Servo Motor using ESP8266, ESP8266 drives the Servo Motor but the input isn’t from a POT but a Web Page we created with AJAX.

The slider in the web page sends the angle values and is received by the NodeMCU, which acts as a Web Server. The NodeMCU, upon receiving the value, transmits corresponding PWM to servo, which then changes the position of the Servo Motor according to the value.

One important thing to remember here is that both the Server and client should be on the same network i.e. the ESP8266, which acts as the Server, and the Laptop (or a Mobile Phone), which is the client, must be connected to the same WiFi network.

Circuit Diagram

The circuit diagram for Web Controlled Servo using ESP8266 WiFi Module is given in the image below.

interfacing servo motor with NodeMCU

Components Required

  1. ESP8266 based Board (NodeMCU)
  2. Servo Motor
  3. Jumper Wires
  4. Laptop

Working of the Circuit

The main program responsible for everything i.e. connecting ESP8266 to WiFi, getting information from Web and finally controlling the Servo.

Pin D5 of NodeMCU will provide the necessary PWM signal to the Servo (Orange). Its other pins are power supply pins and are connected to 5V and GND (Red and Brown).

ESP8266 Servo Arduino Code

The Arduino IDE Code for the Web Controlled Servo using ESP8266 tutorial is given below.

Program is divided in tow parts Web Page (index.h) and Main Hardware Code ESPservo.ino


before uploading code make changes in SSID and Password as per your wifi


Press Ctrl+Shift+N give name index.h and copy paste below code.

Working of Web Controlled Servo using ESP8266

Let us now see the working of the Web Controlled Servo using ESP8266. After making all the connections as per the circuit diagram, upload the NodeMCU code.

After the code is uploaded, if you open the serial monitor, you can see the status of the ESP8266 WiFi Module. After all the initialization steps are done (set mode to Station mode, connect ESP8266 to WiFi and start web server), you can proceed for Web Control.

Now Get IP address of ESP8266 from serial monitor and open it using any web browser. If everything is right, when you change the position of the slider, the position of the servo Motor will be changed.

ESP8266 Servo Motor Control using web


The idea behind Web Controlled Servo using ESP8266 is to implement a web-controlled device (in this case, a Servo Motor) i.e. to control a motor through internet.

This application can be further extended to more advanced and complex projects like controlling a Robot from the Internet.

Leave a Reply