Servo motor keep spining

Hey,
I’m trying to control a servo motor (30 kg) and to make it spin 90 deg beck and forth.
And my servo keep spinning and doesn’t stop.
When i press the button in the blynk app it just stop and start again.
Please what shuld i do?

this is my code:

#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial
#include <Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define BLYNK_PRINT Serial

int pos=0;;
int pos2=0;;
char auth[] = "afALTS4s5s_lu3Z_yNeimE4wcAdpJ3o0";
char ssid[] = "Dvir";
char pass[] = "dvir5558";
 
Servo servo;
Servo servo2;

void setup() {
  
  
  Serial.begin(115200);
  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
 Serial.println("WiFi connected");  
    }

Blynk.begin(auth, ssid, pass, "blynk-cloud.com");

  servo.attach(18); // NodeMCU D1 pin (GPIO-18)
  servo.write(0);
 
}

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

BLYNK_WRITE(V1)
{
  
  for (pos=0; pos<=90; pos+=1)//open valve
  {
   
   servo.write(pos);
   delay(15);
   }
   delay(30);
  for (pos=90; pos>=0; pos-=1)//close valve
  {
   servo.write(pos);
   delay(50);
  }
}

Try adding some serial print statements and you’ll have a better idea about what’s happening to your pos variavle when you do this…

and this…

Adding some Blynk.run() commands into your for loops (once you get them looping correctly) would help you avoid disconnections caused by your delays.

You may also want to add an if statement into your BLYNK_WRITE function which checks the value coming from the button widget so that it only operates on a press rather than a press and release.

Pete.

I didn’t understand you for the second advise.
But for the first one, I’m receiving 1 2 3…90 89 88 87…1
but the servo is just keep spinning without control.
So i go to youtube and found a guy that explain how to control a servo motor.

i copied his code and did exactly what he said and no changes at all, the servo just keep spinning and spinning…

this is his code:

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <Servo.h>



Servo servo;



char auth[] = "Blynk Auth Token";

char ssid[] = "your ssid";

char pass[] = "wifi password";



void setup()

{

  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);



  servo.attach(15); // NodeMCU D8 pin

  

 }

  

void loop()

{

  

  Blynk.run();

  

}

BLYNK_WRITE(V1)

{

   servo.write(param.asInt());

}



BLYNK_WRITE(V2)

{

  servo.write(0);

}

BLYNK_WRITE(V3)

{

  servo.write(90);

}

please what is wrong with my servo or me???

I’d say that’s a question for the YouTuber who published the code.

Pete.

Hey Pete,
I’m really frustrated for now i just wont to move the servo 90 deg beck and forth.
i tried a lot of codes and changes and the motor doesn’t wont to listen to my codes.
maybe you can direct me to a tutorial that works 100% in that subject please.

My advice would be to stick with one sketch rather than keep chopping and changing.

Then, post your code, details of your hardware and connections, what you see in your serial monitor, what widgets you have set-up in your app and how they are configured, and details of EXACTLY what happens when you boot your device and when you operate the widget buttons.

Pete.

ok i have some “good news” i tried a different code :confused: and now instead of choosing the angle I’m choosing the speed of the spin/ i make a slider 1-180, when 1 is the fastest spin clockwise and 90 the servo stop, 180 full speed in anticlockwise. the servo isnt stopping!!! in the video the servo stop!! maybe something changed in blynk setting through time? or what?
im using a 360 servo 30kg, and i think that this is the problam?
the code:

#define BLYNK_PRINT Serial
#include <Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>


Servo servo1;


char auth[] = "afALTS4s5s_lu3Z_yNeimE4wcAdpJ3o0";
char ssid[] = "Dvir";
char pass[] = "dvir5558";


void setup()

{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  servo1.attach(15);// NodeMCU D8 pin

   
 }


void loop()

{
  Blynk.run();
}

BLYNK_WRITE(V1)

{
   servo1.write(param.asInt());
}

ok so the problem is the 360 servo. i switch to a 180 and everything works fine.
now i need to understand how to make the 360 to stoppp.

this is a table from the place i bought the servo maybe you can help me figure it out:

continuous rotation servo has no feedback mechanism so it has no way of knowing its angular position.

Thank you very much for everybody :smiley:
I understand now. this servo i have isn’t controlled like a regular one.
this is the code that make him stop:

#define BLYNK_PRINT Serial
#include <Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>


Servo Foodservo;
Servo Treatservo;


char auth[] = "afALTS4s5s_lu3Z_yNeimE4wcAdpJ3o0";
char ssid[] = "Dvir";
char pass[] = "dvir5558";


void setup()

{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);// connect to blyk server
  Foodservo.attach(15);//  pin 15
Treatservo.attach(5);
   
 }


void loop()

{
  Blynk.run();
}

BLYNK_WRITE(V1)
{
int FoodServoState =param.asInt();
if (FoodServoState==1)
{
Foodservo.writeMicroseconds(1000);
delay (2000);
Foodservo.writeMicroseconds(1500);
delay (2000);
Foodservo.writeMicroseconds(2000);
delay (2000);
Foodservo.writeMicroseconds(1500);
}
}
BLYNK_WRITE(V2)
{
int TreatServoState =param.asInt();
if (TreatServoState==1)
{
Treatservo.writeMicroseconds(1000);
delay (2000);
Treatservo.writeMicroseconds(1500);
delay (2000);
Treatservo.writeMicroseconds(2000);
delay (2000);
Treatservo.writeMicroseconds(1500);
}
}
1 Like

so you managed to figure-out what this meant…

Pete.