ESP8266 NodeMCU – Simple Servo control

In this tutorial, we will learn how to control a small servo-motor. If you want to control the position of a mechanism, just have to use this tutorial. Good for beginners who want to make stuff move without building a motor controller with feedback & gear box, especially since it will fit in small places

Parts Required

To build the global setup you must have the following parts:

Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because, I will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.

Servo sg90

Figure 1

This SG90  is a very good quality motor widely used in RC aeromodelling projects, Arduino, Raspberry etc. Is tiny and lightweight, but with high output power. A Servo can rotate approximately 180 degrees (90 in each direction), and works just like another Servo.

Where we have the servo especifications:

  • • Vcc voltage: 3,0 – 7,2V;
  • • Degree of freedom/rotation: 180 graus;
  • • Speed: 0,12 seg/60º without load;
  • • Torque: 1,2 (4,8V) and 1,6 (6,0V);
  • • Temperature: -30C ~ +60C;
  • • Dimentions: 32 x 30 x 12mm;
  • • weight: 9g.

The servo is very simple to operate, but it is not just plug and play. In figure 2, we have the servo-motor pinout. The red one is for Vcc, the black/bronw is for Gnd and the yelow/orange is for the signal that control the position.

Figure 2

The signal that we send to the servo is caled a PPM signal is one of the radio receiver protocols used in RC Devices to transfer data. It is signal similar to PWM, but with some diferences. In figure 3 we can se an example of a PPM signal used by our servo motor and the times required to build the signal.

Figure 3

To generate this signal, we need to choose a pin in our NodeMcu, that support PWM. Despite the fact, that this signal is not a PWM, the PPM signal uses the same timers that are present in PWM pins, like figure 4.

Figure 4


Figure 5

Connect the signal cable (Yelow) to GPIO pin 2 (D4) because it is a PWM pin, Vcc (Red) to 3.3V and GND (Black), like figure 5. Everything will be powered from your PC using micro-usb to usb cable. In this configuration, the servo will be powered directly from the GPIO 2 pin with 3.3V. It is low voltage level but it works. All the current for the will be drawn from the board, and since each GPIO delivers only no more than a few mA, the torque can be low. Let’s try it.


Copy the main sketch bellow to your Arduino IDE project and save it. The Servo.h library is allready included in th arduino IDE. Heve fun!!

Leave a Reply

Your email address will not be published. Required fields are marked *