How to control Servo without slider

Hi all, I’m currently writing a program to control the servo motor. I used relay as a normally open switch, the servo will alternates between 0 and 180 angle only if there is a high received by relay. The program ran perfectly with arduino uno. But when I try to control the servo through Blynk, it doesn’t work. Kindly help me to solve this.

Below is the code that run perfectly without Blynk and ESP8266:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int relay = 7;

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(relay,OUTPUT);
}

void loop() {
  digitalWrite(relay,1);
  myservo.write(0) ; //servo rotate clockwise
  delay(5000); // waits 5s for servo to move up
  myservo.write(180); //servo rotate anticlockwise
  delay(4800);// waits 4.8s for the servo to move down
}

Below is the Blynk code run with ESP8266:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>

// You should get Auth Token in the Blynk App.
char auth[] = "013bfc7a32a14c2fab205935658d8147";
char ssid[] = "xxxx";
char pass[] = "yyyy";

Servo myservo;
int relay = 7;

#define EspSerial Serial

#define ESP8266_BAUD 115200 //ESP8266 baud rate

ESP8266 wifi(&EspSerial);

void setup()
{
  Serial.begin(9600); // Set console baud rate
  delay(10);
  EspSerial.begin(ESP8266_BAUD); // Set ESP8266 baud rate
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  pinMode(relay, OUTPUT);
  myservo.attach(9);
}

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

void motor()
{
  if (digitalRead(relay) == HIGH)
  {
    myservo.write(0);
    delay(5000);
    myservo.write(180);
    delay(4800);
  }
  else
  {
    myservo.detach();
  }
}

Hello. We have ready example for servo.

Please have a look here - https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FServo

Is it possible to control the servo without using slider?

Sure. Instead of delay(5000) you need to use timer.setInterval(5000L, servo);. Here is example for timer.

When you specify “without using slider”… do you want another way of manually controlling it via the App, or just an automated control (sensing the relay pn high) in the sketch?

Automated, once the relay (button widget in apps) is on, the servo will start rotating.

It took some fiddling, but this seems to work OK for me, try it out.

//===== Servo Sweep Switch Function =====
BLYNK_WRITE(V0) // Switch Widget
{
  SrvoButtonState = param.asInt();
  if (SrvoButtonState == 1) {
    myservo.attach(servoPin);
    timer.setTimer(800L, Servo1, 1);  // Run Servo1 loop every 800ms
  } else
    myservo.detach();
} // END Blynk Function
void Servo1() {
  myservo.write(160);
  timer.setTimer(800L, Servo2, 1);  // Run Servo2 loop every 800ms
}
void Servo2() {
  myservo.write(0);
  Blynk.syncVirtual(V0);  // Check if switch still activated
}

Once you confirm it works on your system, you can modify it to work as a normal loop of it’s own with “is relay active” flag checks instead of SrvoButtonState = param.asInt(); and Blynk.syncVirtual()

It works!!! Thank you so much.

can u show whole programming

Aside from this being a very old topic… No… since your request implies me setting up the entire base Blynk code for whatever hardware is to be used… and something you should already be able to do.

But once you do that, and include the servo library, my functions can be easily dropped in.

1 Like