(Servo jittering and PWM issues) Arduino UNO + Bluetooth + Servo + H-bridge

Why?

  • Pin 3 always do max pwm every (±) 5 seconds?
  • Pin 9 can’t do pwm when i setting it?
  • All servo have a twitch in every (±) 6 second?
//================================Blynk=======================================
#define BLYNK_USE_DIRECT_CONNECT

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3);

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>


char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";

//================================Sevo========================================
#include <Servo.h>
Servo Sv1;
Servo Sv2;
int pos1;
int A;
int pos2;
int B;
//=================================PWM=========================================
int C;
int D;
int E;
int F;
int G;

void setup()
{
  DebugSerial.begin(9600);
  
  Sv1.attach(12);
  Sv2.attach(13);

  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);

  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

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

BLYNK_WRITE(V8) //FIX
{
  A;
  int AN = A;
  Serial.print("AN = ");
  Serial.print(AN);
  Serial.println(" ");
  A = param.asInt();
  if ( AN <= A)
  {
   for (pos1 = AN; pos1 <= A; pos1 += 1) 
   { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    Sv1.write(pos1);              // tell servo to go to position in variable 'pos'
    Serial.print("A+ = ");
    Serial.print(pos1);
    Serial.println(" ");
    delay(50);                       // waits 15ms for the servo to reach the position
    }
  }
  else if ( AN >= A)
  {
     for (pos1 = AN; pos1 >= A; pos1 -= 1)  
     { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    Sv1.write(pos1);              // tell servo to go to position in variable 'pos'
    Serial.print("A- = ");
    Serial.print(pos1);
    Serial.println(" ");
    delay(50);                       // waits 15ms for the servo to reach the position
    }
  }
  Serial.print("A = ");
  Serial.print(A);
  Serial.println(" ");
  delay(20);
}

BLYNK_WRITE(V9) //FIX
{
  B;
  int BN = B;
  Serial.print("BN = ");
  Serial.print(BN);
  Serial.println(" ");
  B = param.asInt();
  if ( BN <= B)
  {
   for (pos2 = BN; pos2 <= B; pos2 += 1) 
   { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    Sv2.write(pos2);              // tell servo to go to position in variable 'pos'
    Serial.print("B+ = ");
    Serial.print(pos2);
    Serial.println(" ");
    delay(50);                       // waits 15ms for the servo to reach the position
    }
  }
  else if ( BN >= B)
  {
     for (pos2 = BN; pos2 >= B; pos2 -= 1)  
     { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    Sv2.write(pos2);              // tell servo to go to position in variable 'pos'
    Serial.print("B- = ");
    Serial.print(pos2);
    Serial.println(" ");
    delay(50);                       // waits 15ms for the servo to reach the position
    }
  }
  Serial.print("B = ");
  Serial.print(B);
  Serial.println(" ");
  delay(20);
}

BLYNK_WRITE(V6) //FIX
{
  int C = param.asInt();
  if( C <= 85)
  {
    float c = (85.0 - C) * (255.0 / 85.0);
    analogWrite(5, c);
    digitalWrite(4, LOW);
    Serial.print("M1A = ");
    Serial.print(c);
    Serial.println(" ");
  }
  if( C >= 170)
  {
    float c = (255.0 - C) * (255.0 / 85.0);
    analogWrite(5, c);
    digitalWrite(4, HIGH);
    Serial.print("M1U = ");
    Serial.print(c);
    Serial.println(" ");
  }
  else if( C >= 86)
  {
    digitalWrite(4, 0);
    digitalWrite(5, 0);
    Serial.println("Stop");
  }
  delay(20);
}

BLYNK_WRITE(V7) // FIX
{
  D = param.asInt();
  if( D <= 85)
  {
    float d = (85.0 - D) * (255.0 / 85.0);
    analogWrite(6, d);
    digitalWrite(7, LOW);
    Serial.print("M2A = ");
    Serial.print(d);
    Serial.println(" ");
  }
  if( D >= 170)
  {
    float d = (255.0 - D) * (255.0 / 85.0);
    analogWrite(6, d);
    digitalWrite(7, HIGH);
    Serial.print("M2U = ");
    Serial.print(d);
    Serial.println(" ");
  }
  else if( D >= 86)
  {
    digitalWrite(6, 0);
    digitalWrite(7, 0);
    Serial.println("Stop");
  }
  delay(20);
}

BLYNK_WRITE(V10)
{
  E = param.asInt();
  if( E <= 85)
  {
    analogWrite(10, E);
    Serial.print("MA = ");
    Serial.print(E);
    Serial.println(" ");
  }
  if( E >= 170)
  {
    analogWrite(10, E);
    Serial.print("MU = ");
    Serial.print(E);
    Serial.println(" ");
  }
  else if( E >= 86)
  {
    digitalWrite(10, 0);
    Serial.println("Stop");
  }
  delay(20);
}

BLYNK_WRITE(V0)
{
  E = param.asInt();
  if( E == 1)
  {
    analogWrite(10, 30);
    Serial.println("Maju");
  }
  else
  {
    digitalWrite(10, 0);
    Serial.println("Stop");
  }
}

BLYNK_WRITE(V1)
{
  E = param.asInt();
  if( E == 1)
  {
    analogWrite(10, 250);
    Serial.println("Mundur");
  }
  else
  {
    digitalWrite(10, 0);
    Serial.println("Stop");
  }
}

BLYNK_WRITE(V11)
{
  F = param.asInt();
  if( F <= 85)
  {
    analogWrite(11, F);
    Serial.print("MA = ");
    Serial.print(F);
    Serial.println(" ");
  }
  if( F >= 170)
  {
    analogWrite(11, F);
    Serial.print("MU = ");
    Serial.print(F);
    Serial.println(" ");
  }
  else if( F >= 86)
  {
    digitalWrite(11, 0);
    Serial.println("Stop");
  }
  delay(20);
}

BLYNK_WRITE(V2)
{
  F = param.asInt();
  if( F == 1)
  {
    analogWrite(11, 30);
    Serial.println("Maju");
  }
  else
  {
    digitalWrite(11, 0);
    Serial.println("Stop");
  }
}

BLYNK_WRITE(V3)
{
  F = param.asInt();
  if( F == 1)
  {
    analogWrite(11, 250);
    Serial.println("Mundur");
  }
  else
  {
    digitalWrite(11, 0);
    Serial.println("Stop");
  }
}

BLYNK_WRITE(V4)
{
  G = param.asInt();
  if( G == 1)
  {
    analogWrite(10, 44);
    analogWrite(11, 44);
    Serial.println("Maju");
  }
  else
  {
    digitalWrite(10, 0);
    digitalWrite(11, 0);
    Serial.println("Stop");
  }
}

BLYNK_WRITE(V5)
{
  G = param.asInt();
  if( G == 1)
  {
    analogWrite(10, 150);
    analogWrite(11, 150);
    Serial.println("Mundur");
  }
  else
  {
    digitalWrite(10, 0);
    digitalWrite(11, 0);
    Serial.println("Stop");
  }
}

Edit your post as some parts of the sketch are invisible.

@Richard_Hadiputra I fixed your posted code formatting as required for forum viewing.

As for your issues…

All are generally related to a Servo Library conflict… On UNO It blocks PWM on pin 9 & 10

I have also noticed (at least on Arduinos) that the servo library doesn’t play nicely with SimpleTimer. Can cause servo jittering, and now that that BlynkTimer is built in it appears that the jittering can happen even without a timer defined.

I have found it best to use servo detach() when not actually moving the servo around.

Hard to say whether Bluetooth is also adding to the fun… it is still in beta and I have noticed it can cause (or at least reveal) additional timing issues with delayed reactions to app commands and some PWM inaccuracies.

1 Like

Where would one detach in the Blynk context? I have a lot of buzzing on servos.
There are 3 and they respond to HTTPS request via WIFI + BlynkCloud,
So don’t they need to be ‘ready’ and attached when pinged?

  servoRAIN7.attach(13); 
  servoWIND9.attach(14); 
  servoSUN8.attach(2); 
}
void loop()

  {
    Blynk.run();
  }

Why reopen an old topic?

The whys, hows and wheres to attach/detach servos is entirely dependent on your needs and uses, so best to create a new topic with actual context of your question, showing all your code and explaining your particular needs.

1 Like