NodeMCU loosing connection when attaching a servo

Hi all,

I have some weird issue with my new project (WiFi car with camera controlled via Blynk).
I uploaded a code to control the car, and it worked perfect. But when I tried to add the servo code, to control the camera angle, it keeps connect and disconnect to blynk-cloud.com:80.

  • I tried another WiFi (I connected my phone)
  • restart the router
  • update arduino IDE
  • update blynk libraries
  • update servo library
  • change the order of the code
  • change the V pins and the digital pins

But nothing worked. Only when I’m deleting the servo code, it works.
It looks like I’m overloading the Blynk server and it kicks me out but I don’t know why…
Any idea?


#define BLYNK_PRINT Serial 
#include <Servo.h>
#include <ESP8266WiFi.h> 
#include <BlynkSimpleEsp8266.h> 


// You should get Auth Token in the Blynk App. 
// Go to the Project Settings (nut icon). 
char auth[] = "token"; 
// Your WiFi credentials. 
// Set password to "" for open networks. 
char ssid[] = "ssid"; 
char pass[] = "pass"; 

int PWMA=5;//Right side 
int PWMB=4;//Left side 
int DA=0;//Right reverse 
int DB=2;//Left reverse 

Servo servo1;  
Servo servo2;

//Servo code
BLYNK_WRITE(V3){
  servo1.write(param.asInt());  
}

BLYNK_WRITE(V4){ 
  servo2.write(param.asInt());
}
  
// Handling Joystick data 
BLYNK_WRITE(V1){ 
int x = param[0].asInt(); 
int y = param[1].asInt();  

if(x==-1 && y==-1){             //BackWard and Left     
  digitalWrite(PWMA, LOW); 
  digitalWrite(DA, LOW); 
     
  digitalWrite(PWMB, HIGH); 
  digitalWrite(DB, HIGH); 
 
}else if(x==-1 && y==0){        //Left Turn 
  digitalWrite(PWMA, 450); 
  digitalWrite(DA, HIGH); 
    
  digitalWrite(PWMB, 450); 
  digitalWrite(DB, LOW); 
 
}else if(x==-1 && y==1){        //Forward and Left     
  digitalWrite(PWMA, LOW); 
  digitalWrite(DA, LOW); 
    
  digitalWrite(PWMB, HIGH); 
  digitalWrite(DB, LOW); 
 
}else if(x==0 && y==-1){        //BackWard     
  digitalWrite(PWMA, HIGH); 
  digitalWrite(DA, HIGH); 
     
  digitalWrite(PWMB, HIGH); 
  digitalWrite(DB, HIGH);
}else if(x==0 && y==0){        //Stay 
  digitalWrite(PWMA, LOW); 
  digitalWrite(DA, LOW); 
     
  digitalWrite(PWMB, LOW); 
  digitalWrite(DB, LOW); 
 
}else if(x==0 && y==1){        //Forward 
  digitalWrite(PWMA, HIGH); 
  digitalWrite(DA, LOW); 
    
  digitalWrite(PWMB, HIGH); 
  digitalWrite(DB, LOW); 
 
}else if(x==1 && y==-1){        //Backward and Right     
  digitalWrite(PWMA, HIGH); 
  digitalWrite(DA, HIGH); 
     
  digitalWrite(PWMB, LOW); 
  digitalWrite(DB, LOW); 
 
}else if(x==1 && y==0){        //Right turn 
   digitalWrite(PWMA, 450); 
  digitalWrite(DA, LOW); 
   
  digitalWrite(PWMB, 450); 
  digitalWrite(DB, HIGH); 
 
}else if(x==1 && y==1){        //Forward and Right 
  digitalWrite(PWMA, HIGH); 
  digitalWrite(DA, LOW); 

}
}
void setup(){ 
 
 // Debug console 
 Serial.begin(9600); 
 Blynk.begin(auth, ssid, pass); 
 pinMode(PWMA, OUTPUT); 
 pinMode(PWMB, OUTPUT); 
 pinMode(DA, OUTPUT); 
 pinMode(DB, OUTPUT); 

 servo1.attach(6);
 servo2.attach(7);

} 
void loop(){ 
Blynk.run();  
} 

I think the servo.h library is a blocking function.
You should read this:

Pete.

Do you think it will help? I’m not getting to the part where I’m actually moving the servo so I don’t see how it’s going to solve this problem…

Solved!
I attached the servos to the wrong pins… :man_facepalming:
It should be:

 servo1.attach(13);
 servo2.attach(15);

To control Digital pin 7 and Digital pin 8 in NodeMCU
Thanks a lot for the help!