Want to control two servos with blynk

I am new to both Blynk and arduino, I am trying to control two  servo with blynk app but with the following code only one servo is getting controlled whereas other motor is not getting controlled rather.In short both motor work fine individually but when tried to control both only one gets control
       // ********************************************************
          Download latest Blynk library here:
            https://github.com/blynkkk/blynk-library/releases/latest
          Blynk is a platform with iOS and Android apps to control
          Arduino, Raspberry Pi and the likes over the Internet.
          You can easily build graphic interfaces for all your
          projects by simply dragging and dropping widgets.
            Downloads, docs, tutorials: http://www.blynk.cc
            Sketch generator:           http://examples.blynk.cc
            Blynk community:            http://community.blynk.cc
            Follow us:                  http://www.fb.com/blynkapp
                                        http://twitter.com/blynk_app
          Blynk library is licensed under MIT license
          This example code is in public domain.
         *************************************************************
          Rotate a servo using a slider!
          App project setup:
            Slider widget (0...180) on V3
         *************************************************************/

        /* Comment this out to disable prints and save space */
        #define BLYNK_PRINT Serial


        #include <SPI.h>
        #include <Ethernet.h>
        #include <BlynkSimpleEthernet.h>
        #include <Servo.h>

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

        Servo servo;

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

        Servo servo1;

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


        void setup()
        {
          // Debug console
          Serial.begin(9600);

          Blynk.begin(auth);

          servo.attach(9);
          
          servo1.attach(11);
        }

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

Did you try without Blynk to move both servo’s seperately? And how are they wired up?

Yes i have tried to move servos separately without blynk and they worked fine

I have found that Blynk’s internal use of hardware interrupt timers can cause some issues with the servo libraries use of same interrupt timers; delays, stuttering, etc.

Try acquiring the slider inputs as you have done, but then running the servo actions, one after another, in a separate, possibly timed, loop?

You might have to play around with using different pins for one of the servo, or even trying a different servo library.

try two separate sliders for both the servo’s. also using virtual pin is not required as the slider can directly control any pwm pin

The OP already had a slider for each, and when using servos, running with virtual pins and servo library is much smoother than direct pin control via the app. I have ran servos both way, many times… Try it, you will see :wink:

It may also be an idea to put a little capacitor on each servo. That should allow for even smoother operation.

@Lichtsignaal Yes, that can help with power issues, as will having separate power source (actually essential with multiple servos), but the OP issue is only one servo is being controllable at a time… from my testing of multiple servos over Blynk, it seems to be a conflict between Blynk, Timers and Servo Libraries as all tend to rely on the interrupt timers (the more timers I had running the more erratic the jittering). And that was even with separate power for the servos.

@Darshini I have tried a few things with servos and the only reasonable way to stop jittering and help smooth out control was by attaching and detaching a servo before and after each movement. This might help with multi-servo control as well,

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

@Darshini This is some code I used to sweep a single servo back and forth using timers instead of delays, however you might be able to see how it works and modify it to control two servos together.

Instead of driving a servos from each slider function, you would need to take the data values from your slider inputs as the occur and feed them to a timer loop that ran the servos ever 200-500ms or so, depending on the overall purpose of your sketch.

//===== 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
}

Would it be an option to use a separate I2C board for controlling servo’s? I use a similar thing which can address 16 servo’s via I2C. I think it’s an Adafruit clone board.

1 Like

Yes i have used two seperate sliders for two servos but its not working

Actually We are using servos for controlling robotic arm we want to control only four sevos

are you using the pwm pins only. use value 10-255

Yes i too have tried with virtual pin but its working for only one servo but with code attached by me it is not controlling two servos

oh ok I will give it a try

There are boards with less channels, a bit of digging on Ebay reveals more :slight_smile: Search for arduino servo board or something similar.

Yes, I know… your code will not work sufficiently. That is why I suggested using a timer loop. And @Lichtsignaal’s suggestion of a I2C external servo controller IC is also a great idea as it avoids the otherwise troublesome Blynk/Arduino/Timer interrupt conflicts.

As for PWM pins… servos don’t properly run on PWM, they run better on PPM; Similar but different :wink: That is what the Servo Libraries do - provide a proper PPM signal.

http://forum.arduino.cc/index.php/topic,5701.0.html

How do you do it without blynk ? Can you show me

This post is 2 years old.

Plus, if it isn’t about BLYNK, I am not sure why you would ask here on the BLYNK forum.

A good place to start would be HERE.

1 Like