How to control Servo Motor Using Blynk with bluetooth (HC-05) connection

I tried controlling servo motor with arduino and it worked normally but when i tried same with blynk - slider with bluetooth connection is it not responding. Pls Help
Here is my code:

#define BLYNK_PRINT DebugSerial
#include <Servo.h>
int servoPin=6;
Servo myservo; 
int pin=11;
int i=0;
// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>

SoftwareSerial DebugSerial(0, 1); // RX, TX

#include <BlynkSimpleStream.h>

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

BLYNK_WRITE(V3)
        {
          servo.write(param.asInt());
        }
void setup()
{myservo.attach(servoPin);
  // Debug console
  
  DebugSerial.begin(9600);
  pinMode(11,INPUT);
pinMode(6,OUTPUT);
  // Blynk will work through Serial

  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

void loop()
{i=analogRead(0);
int val=analogRead(pin);
Serial.println("Hello World!!!");
    myservo.write(i);
  delay(50);
  Blynk.run();
}

Well, aside from the stuff in your void loop() that you DON’T want in that loop when using Blynk…

{i=analogRead(0);
int val=analogRead(pin);
Serial.println("Hello World!!!");
    myservo.write(i);
  delay(50);

You are not actually writing to your pre-setup servo designation (myservo) with the Blynk function… it should be:

BLYNK_WRITE(V3)
{
 myservo.write(param.asInt());
}

THX it worked .
But also there is a delay of 2-3 sec after i give command, do u know how to remove it??

Assuming you already removed those other commands from the void loop() then perhaps the issue is the bluetooth interface?? It isn’t the most reliable, possibly still BETA.

Nope, when i tried to control only led with Bluetooth it worked like hand to hand smoothly, but after connecting motor delay occurs don’t know why…??
Here’s what i have modified–

#define BLYNK_PRINT DebugSerial
#include <Servo.h>
int servoPin=5;
Servo myservo; 
int i=0;
#include <SoftwareSerial.h>

SoftwareSerial DebugSerial(0, 1); // RX, TX

#include <BlynkSimpleStream.h>

char auth[] = "57ac041e2a054fdb8d2368735c403f8a";

BLYNK_WRITE(V0)
        {
          myservo.write(param.asInt());
        }
void setup()
{myservo.attach(servoPin);
  
  DebugSerial.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

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

Nope what? Nope you didn’t remove those commands from the void loop() ?.. if they are stiil there, then that will cause unnecessary delays for the Blynk library, which could translate into the issues you are seeing… The solution to placing commands and functions in the void loop() is to use timed functions instead.

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app

EDIT.… OK now I see your code… remove that delay in the void loop()

Also, when posting code here please add in some formatting characters to make it properly viewable…

Blynk - FTFC

The Blynk library needs to run constantly in the background… housekeeping, running Blynk functions, maintaining the communication link to the App (via the server with normal WiFi link)… so any blocking code is BAD… that includes delays.

Proper Blynk coding edict required use of BlynkTimer to account for any required delays and timing.

http://docs.blynk.cc/#blynk-firmware-blynktimer

And finally… In my observations, the servo library seems to react with Blynk library… perhaps the shared internal timers? (at least on Arduino MCUs) regardless, I have noticed that while sometimes subtle, the servo will jitter and move erratically at times… even when not actually being moved. I have gotten around the idle jittering by detaching the servo shortly after any movement.

https://www.arduino.cc/en/Reference/ServoDetach

1 Like