NodeMCU ESP8266 unable to fully rotate Servo 180 degrees

Hi!

I have an Arduino Uno R3 as well as a NodeMCU ESP8266. I am attempting to make the MG995 Servo spin to certain angles. My setup works when connected to the Arduino Uno R3, but does not work too well when connected to the NodeMCU.

Here is the code I use for both:

#include <Servo.h> 
#define Servo_PWM 2 //pin 2 for NodeMCU, pin 9 for Arduino Uno
Servo MG995_Servo;  

void setup() {
  Serial.begin(115200); 
  delay(1000);

  MG995_Servo.attach(Servo_PWM); 
  MG995_Servo.write(0);
}

void loop() {
  Serial.println("turn 1");
  MG995_Servo.write(180);
  delay(1000);
  MG995_Servo.detach();
  delay(1000);
  
  MG995_Servo.attach(Servo_PWM);
  Serial.println("turn 3");
  MG995_Servo.write(0);
  delay(1000);
  MG995_Servo.detach();//Stop
  
  delay(4000);
  MG995_Servo.attach(Servo_PWM);
}

As you may note in this code, I am attempting to spin the servo clockwise and counterclockwise with time intervals in between. When working with the Arduino, it works fine. However, when connected to the NodeMCU, the servo only moves approximately half of what I am asking it to do. For example, it spins 90 degrees clockwise and anticlockwise.

For the NodeMCU circuit, I am using a mini breadboard. I have connected a 5mm Terminal Block which is receiving power from a 5V2A power supply brick connected to a wall socket. I have connected GND to GND and POWER to POWER on the servo. I have confirmed that the servo works as intended and this is the same setup I use when connected to the Arduino Uno R3. I have the servo's signal power connected to D4 on the NodeMCU (which is GPIO 2). I have the NodeMCU GND connected to the same GND as the servo (via the breadboard). I am powering the NodeMCU via the USB port and connected to my laptop.

The setup for the NodeMCU is the exact same as the one with the Arduino Uno (except I connect the servo's signal wire to GPIO 9 on the Arduino). So I am not sure what is causing the servo to not fully rotate.

Any ideas?
Thanks in advance!

Just to add to my original post, I have also tested this with the NodeMCU ESP32. In this case, instead of the standard Servo.h library, I used the ESP32Servo.h library. With the exact same setup, it worked perfectly.

Is this perhaps a library issue? Where the standard Servo.h library is incompatible with the NodeMCU ESP8266? If so, is there a specific library I might be able to use with it (like I have done with the ESP32)?

Hi @iamastic,
I tested it with an ESP8266 and an SG90 servo and the same problem occurred.

There is a library for ESP8266 in the folder:
C:\Users\xxx\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\Servo

It should work fine with ESP8266.

Maybe it's a voltage issue as the servo control pin is for 5V and the ESP8266 only provides 3.3V, but I'm not sure about that.

Try using a transistor to act as a level converter for the servo.

RV mineirin

Yes, may be. One library may consider 100 for full servo range, another library 255.

@iamastic, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

I'll check the library out and also see if I can get hold of a transistor. Thanks!

1 Like

yaa there is an issue with default servo library.it won't allow you to move servo motor more than 90 degrees on nodemcu/esp8266. i tried arduino create agent where we can upload code via browser then it was working fine.

use this
myservo.attach(D4, 500, 2400);
working perfectly almost 180 degrees.

4 Likes

Hi kilgag, I tested your suggestion, and it works! But I would like to know why. The SG90 has a duty cycle of 1 ms ranging from 1 ms to 2 ms. Therefore, the correct parameters in the .attach() method should be 1000 and 2000, isn't it? But I tested that, and it does not work.

Thank You Bro. That's works fine for me

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.