Go Down

Topic: servo start shaking when another servo is being control via bluetooth (Read 2223 times) previous topic - next topic

thisbebornwild

I've been learning about arduino for about a month. Right now I'm trying to control 2 servos, one being control by bluetooth (lets call it servo 1) and the other by a regular potentiometer (servo 2) through analog input pin. This is just one fraction of my project. I want to be able to control one servo by a potentiometer, the other wirelessly through bluetooth.

My problem is that whenever I'm controlling the one through bluetooth (servo 1), the other one that is supposely control by a potentiometer (servo 2) with analog would start shaking. However, whenever I'm moving servo 2 (the one that is control by a potentiometer), the other one does not shake. It seems like the interference is only one way.

At first I thought it was caused by overcurrent drain, so I tried to power everything individually (2A on each servo, USB 500ma on arduino) with one common ground, but it didn't help. After that I deleted all the code on servo 2 thinking it might've caused by some mistakes in my code, but it didn't help either. Servo 2 still shakes when I'm moving servo 1 via bluetooth even when there's no code regarding its movement. Thinking the potentiometer may be broken, I even changed my code and make both servos under the control of bluetooth but it didn't solve anything as one of them still shakes while the other is receiving signal. I figured that the only way servo 2 would stop shaking is to completely exclude it from the system, meaning not putting "servo2.attach(pin);" at void setup. But that means I can't control it with potentiometer through analog either.

Can anyone point me at the right direction at solving this problem? Any help is much appreciate.

I'm using arduino uno, hc 05 bluetooth module and both servos are sg90.

Code: [Select]

#include <SoftwareSerial.h>
#include <Wire.h> 
#include <Servo.h>

Servo servo1;
Servo servo2;

int bluetoothTx = 0;
int bluetoothRx = 1;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

const int potpin = A2;                           //potentiometer on analog input A2, for servo 2
int val;                                               //variable for knob movement of servo 2
unsigned long knobpreviousTime =0;        //timer for knob, using millis instead of delay
unsigned long knobinterval = 15;         
                                         
void setup()
{
 servo1.attach(3);
 servo2.attach(9);
 Serial.begin(9600);             //Setup usb serial connection to computer
 bluetooth.begin(9600);       //Setup Bluetooth serial connection to android
}

void loop()
{
if(bluetooth.available()){                                 //Read from bluetooth and write to usb serial
  int servopos = bluetooth.read();
  Serial.println(servopos);
  servo1.write(servopos);
  }

 unsigned long knobcurrentTime = millis();           //Even if i delete this part, servo 2 still shakes when I control servo 1 through bluetooth
 if(knobcurrentTime - knobpreviousTime > knobinterval){
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  servo2.write(val);                  // sets the servo position according to the scaled value
  knobpreviousTime = knobcurrentTime;
  }
}


Robin2

How are you powering your servos? They should NOT be powered from the Arduino 5v pin. Give them their own power supply and connect the servo GND to the Arduino GND.

Fluctuations in the Arduino voltage may cause the potentiometer to give different values.

If that is not the problem what about only moving the potentiometer servo when the pot value changes -rather than every 15 millisecs.

You might also consider modifying your code so that a very small change in the pot value does not cause the servo to move.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

DuaneDegn

Does the servo shake even if the Bluetooth instruction keeps the servo at the same location?

limitium

Hi. I have exactly the same problem with servo sweep on BT message. I use hc-05 BT module and tries with a different servos. Servo and BT module has external power supply. At the end I came with this simplified code. I don't change servo position at all, but on any byte was sent to BT my servos are shaking.

Code: [Select]

#include <SoftwareSerial.h>
#include <Servo.h>

SoftwareSerial BTDevice(10, 11); //rx,tx
Servo sPan;
Servo sTilt;

int sPanPin = 3;
int sTiltPin = 13;

void setup() {

 BTDevice.begin(9600);

 sPan.attach(sPanPin);
 sPan.write(90);

 sTilt.attach(sTiltPin);
 sTilt.write(90);
}
void loop(){

}

Robin2

Hi. I have exactly the same problem with servo sweep on BT message.
Please read carefully all of the earlier Replies in this Thread and answer the questions in them.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

limitium

I have read your previous answer and didn't find any useful for me information. Servo and BT module are connected to pc power supply which has 75W on +5 should be en ought for sg90 right?



and this is a result of receiving bytes from phone on button tap: Video

Robin2

I have read your previous answer and didn't find any useful for me information.
Which makes me wonder why you asked your question in this Thread rather than starting your own Thread.

Your photo is very nicely focused and has nice colours. However it is quite useless for showing wiring connections. Make a pencil drawing of all the connections and post a photo of the drawing.

Please just post the output as text so we can read it quickly over a slow internet connection.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

SoftwareSerial uses pin-change interrupts and puts lots of jitter on the timing of the ISR used by Servo library.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

limitium

Which makes me wonder why you asked your question in this Thread rather than starting your own Thread.

Your photo is very nicely focused and has nice colours. However it is quite useless for showing wiring connections. Make a pencil drawing of all the connections and post a photo of the drawing.

Please just post the output as text so we can read it quickly over a slow internet connection.

...R
Hey! Why are you so offensive? Because of someone wrote that your basic answer didn't help in all situations? I did a photo only for you with words "Look, man, everything is connected and rechecked 100 times don't worry about that" and made a video where you can see that basically it works, but with artifacts in behavior. Just stop to write empty messages and increase entropy of the internet.

SoftwareSerial uses pin-change interrupts and puts lots of jitter on the timing of the ISR used by Servo library.
That was a really interesting to read about interruptions, software serial emulator and their limitations. But I concerned that for resolving so simple task I need information that so deep from the information in tutorials.

I resolved the problem by switching to the hardware Serial.
Thank you greatly helped

Robin2

I did a photo only for you with words "Look, man, everything is connected and rechecked 100 times don't worry about that"
Alas, similar statements occur in many Threads and eventually prove to be wrong. We have no means of judging your level of expertise so it seems wise to be cautious rather than waste time chasing the wrong problem.

...R

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up