BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Servo controlled over wifi with Blynk

 *  Description
 *    Controlling 2 servos over wifi using Blynk library and nodemcu (ESP8266)
 *  
 *  Input
 *    V0 : Blynk Virtual Pin 0 (I used vertical slider)
 *    V1 : Blynk Virtual Pin 1 (I used horizontal slider)
 *  
 *  Output
 *    V0 desired value as object (servoVertical) new angle
 *    V1 desired value as object (servoHorizontal) new angle
 *    
 *  Important 
 *    Timer has been added to detach servo, this way servo motor will not force
 *    note that when slider(s) are touch(ed) servo(s) re-attach to set new angle 
 *
 *  There are two common types of servo:
 *  White - Red - Black wired servo
 *  Orange - Red - Brown wired servo
 * 
 *  If your servo has White - Red - Black wires, then connect it as follows
 * 
 *  White wire connects to Digital pin D4
 *  Black wire connects to GND pin
 *  Red wire connects to 3V3 pin
 * 
 *  If your servo has Orange - Red - Brown wires, then connect it as follows
 * 
 *  Orange wire connects to Digital pin D4.
 *  Brown wire connects to GND pin
 *  Red wire connects to 3V3 pin
 *  
 */
 
// LIBRARIES //
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

// TOOLS //
BlynkTimer timer;

// GLOBAL VARIABLES //
char auth[] = "AUTH";
char ssid[] = "SSID";
char pass[] = "SSIDPASSWORD";
const int DigitalPinD0 = 16;
const int DigitalPinD1 = 5;

// OBJECTS //
Servo servoVertical;
Servo servoHorizontal;

BLYNK_WRITE(V0) {
  servoVertical.attach(DigitalPinD0);
  servoVertical.write(param.asInt());
}

BLYNK_WRITE(V1) {
  servoHorizontal.attach(DigitalPinD1);
  servoHorizontal.write(param.asInt());
}

// CUSTOM FUNCTION //
void goDetach() {
  servoVertical.detach();
  servoHorizontal.detach();
}

// MAIN //
void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  servoVertical.attach(DigitalPinD0);
  servoHorizontal.attach(DigitalPinD1);
  digitalWrite(LED_BUILTIN, HIGH);
  timer.setInterval(10000L, goDetach);
}

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

I think that newcomers may struggle to follow your instructions to build this project themselves.
Your description of how to attach the servo (you only talk about one here) has the servo attached to pin D4, yet cour code uses pins D0 and D1.

D0 is actually a poor choice of pin, because if it’s pulled LOW on startup then it can put the NodeMCU into programming mode. See this topic for more details:

Pete.

1 Like

I understand that code may be too heavy, it seems I dont have the possibility to edit the code anymore…

thanks for the pin information I didn’t know aboout appropriate pin

 *  Description
 *    Controlling 2 servos over wifi using Blynk library and nodemcu (ESP8266)
 *  
 *  Input
 *    V0 : Blynk Virtual Pin 0 (I used vertical slider)
 *    V1 : Blynk Virtual Pin 1 (I used horizontal slider)
 *  
 *  Output
 *    V0 desired value (servo angle)
 *    V1 desired value (servo angle)
 *   
 */
 
// LIBRARIES //
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

// TOOLS //
BlynkTimer timer; // Import Blynk Timer from Blynk

// GLOBAL VARIABLES //
char auth[] = "AUTH TOKEN"; 
char ssid[] = "SSID";
char pass[] = "SSIDPASSWORD";
const int DigitalPinD4 = 2; // pinmapping from nodemcu (ESP8266) to Arduino
const int DigitalPinD5 = 14;

// OBJECTS //
Servo servoVertical; // Define servoVertical Object
Servo servoHorizontal; // Define servoHorizontal Object

// INPUT //
BLYNK_WRITE(V0) { // incomming data from blynk virtual pin
  servoVertical.attach(DigitalPinD4); // re-attach Object to digital pin
  servoVertical.write(param.asInt()); // magical line that makes servo angle change
}

BLYNK_WRITE(V1) {
  servoHorizontal.attach(DigitalPinD5);
  servoHorizontal.write(param.asInt());
}

// CUSTOM FUNCTION //
void goDetach() { // function that detach servo (stop buzzing noise)
  servoVertical.detach();
  servoHorizontal.detach();
}

// MAIN //
void setup() {
  Serial.begin(9600); // initialize serial communication
  Blynk.begin(auth, ssid, pass); // initialize board wifi connection 
  servoVertical.attach(DigitalPinD4); // attach servo 
  servoHorizontal.attach(DigitalPinD5);
  digitalWrite(LED_BUILTIN, HIGH); // disable on board light
// use Blynk timer for 10000 ms (10sec) and then after run goDetach function
  timer.setInterval(10000L, goDetach); 
}

void loop() {
  Blynk.run(); // launch application
  timer.run(); // launch Blynk timer 
}