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 & gearbox, 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 kg.cm (4,8V) and 1,6 kg.cm (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 plugged and play. In figure 2, we have the servo-motor pinout. The red one is for Vcc, the black/brown is for Gnd and the yellow/orange is for the signal that controls the position.

Figure 2

The signal that we send to the servo is called a PPM signal is one of the radio receiver protocols used in RC Devices to transfer data. It is a signal similar to PWM, but with some differences. In figure 3 we can see 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 in figure 4.

Figure 4

Circuit

Figure 5

Connect the signal cable (Yellow) to GPIO pin 2 (D4) because it is a PWM pin, Vcc (Red) to 3.3V and GND (Black), as 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.

Coding

Copy the main sketch below to your Arduino IDE project and save it. The Servo.h library is already included in the Arduino IDE. Have fun!!


#include < Servo.h>

Servo servo;
int val;

void setup() {
Serial.begin(115200);
servo.attach(2); // attaches the servo on pin D4 to the servo object
servo.write(0);
}

void loop() { // main program loop

  servo.write(90);
  delay(1000);
 

for(val = 0; val <= 180; val++){
servo.write(180); // sets the servo position according to the scaled value
Serial.print("Angle:");
Serial.println(val);
delay(1000); // waits for the servo to get there
}

for(val = 180; val >= 0; val--){
servo.write(0); // sets the servo position according to the scaled value
Serial.print("Angle:");
Serial.println(val);
delay(1000); // waits for the servo to get there
}

}

2 thoughts on “ESP8266 NodeMCU – Simple Servo control

  1. Having read this I believed it was very enlightening.
    I appreciate you finding the time and effort to put this information together.
    I once again find myself personally spending a significant amount of
    time both reading and leaving comments. But so what, it was still worthwhile!

Leave a Reply

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock