ESP8266 - Potentiometer Servo Motor

This tutorial instructs you how to use ESP8266 to control the angle of a servo motor based on the input value from a potentiometer. 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 kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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 and Potentiometer

If you are unfamiliar with servo motors and potentiometers, including pinout, functioning, and programming, the following tutorials will be helpful:

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Servo Motor Potentiometer

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

  • Read the value from the potentiometer (ranging from 0 to 1023)
int analog_value = analogRead(A0);
  • Convert it it to an angle in the range of 0 to 180.
int angle = map(analog_value, 0, 1023, 0, 180);
  • Control the servo to the specified angle.

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #include <Servo.h> Servo servo; // create servo object to control a servo void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); servo.attach(D5); // attaches the servo on pin D5 to the servo object } void loop() { // reads the value of the potentiometer (value between 0 and 1023) int analog_value = analogRead(A0); // scales it to use it with the servo (value between 0 and 180) int angle = map(analog_value, 0, 1023, 0, 180); // sets the servo position according to the scaled value servo.write(angle); // print out the value Serial.print("Analog: "); Serial.print(analog_value); Serial.print(", Angle: "); Serial.println(angle); delay(100); }

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 your computer using a USB cable.
  • Launch the Arduino IDE, select the appropriate board and port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to transfer the code to the ESP8266.
Arduino IDE Upload Code
  • Open the Serial Monitor
  • Turn the potentiometer
  • Check out the servo motor's rotation
  • View the result in the Serial Monitor
Analog: 0, Angle: 0 Analog: 85, Angle: 14 Analog: 201, Angle: 35 Analog: 286, Angle: 50 Analog: 370, Angle: 65 Analog: 444, Angle: 78 Analog: 521, Angle: 91 Analog: 608, Angle: 106 Analog: 690, Angle: 121 Analog: 793, Angle: 139 Analog: 907, Angle: 159 Analog: 1023, Angle: 180 Analog: 1023, Angle: 180
Autoscroll Show timestamp
Clear output
9600 baud  

Code Explanation

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

Video Tutorial


  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!