Servo control with Button on blynk app using BLYNK_WRITE(Vpin)

Hi guys,
just wanted to share my project on controlling a servo motor plugged onto a nodemcu board through the blynk app using a button widget. So basically what happens is that when the button widget is pressed on the app, the servo turns on and rotates based on a simple servo sweep sketch from the Arduino IDE examples. Once the button is pressed, the servo rotates continuously until the button is in the off position which is when the servo stops. This is done using virtual pins to run a certain type of code (in this case the servo sweep code) when the virtual pin is set to a high value (i.e when the button is pressed). The code below is tailored to run on a nodemcu board. Minor tweaks and changes can be made to make it suitable for different for different boards and also to modify how the servo rotates.

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth token";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "network name";
char pass[] = "password";

Servo servo;

void cradle() {
//you begin your own personal code for servo here
  int pos;

  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
//your personal code for servo should end here

  int pinValue = param.asInt();
  if (pinValue == 1) {    // if Button sends 1
    cradle();             // start the function cradle; // Run rest of show in-between waiting for this loop to repeat or quit.
    int pinValue = 0;  // Set V3 status to 0 to quit, unless button is still pushed (as per below)
    Blynk.syncVirtual(V3); // ...Then force BLYNK_WRITE(V3) function check of button status to determine if repeating or done.

void setup()
  // Debug console

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  servo.attach(4); //attaches servo to pin 4 (D2 on node mcu)

void loop()

If you are wondering how I did this, all the credit goes to @GTT. (Im a newbie myself)
I just took a look at a few topics he had solved and followed his instruction on reading the blynk docs :grinning:
Hope this was helpful!


Looking good :slight_smile:

If you are concerned about somthing like the cradle() function taking too long (FYI, it isn’t now, even 180*10ms is only 1.8 seconds), then you should insert the somewhere inside it’s for() loop processing, otherwise it will not do anything beneficial sitting where it is, after the function completes… by then the main loop is already running.

1 Like

Thanks for taking a look at my project :slightly_smiling_face:. Was hoping to get some feedback. Will try and make the following changes to the code.

Just wanted you to know this example didn’t work for me.

What hardware are you running it on, and what exactly does “doesn’t work” mean?


I’m sorry. thec code works. i double checked and saw that my servo was not connected to vcc on the bread board. I’m using a nodemcu.

thank you for this post! it works on my project :slight_smile:

A post was split to a new topic: Servo control question

You can generate the code in sketch builder in blynk.
what i mean is, it’s more comfortable to code it for the performance of your servo.
try button as well as it’s quantity or angle you want.