1

My two arduino communicating serially with XBee. The receiver arduino also controls the servo motor. Servo works stably when there is no xBee connection. But when Arduino's serial communication, there is a problem in the operation of the servo motor. Servo motor stuttering while rotate, so stuttering occurs.

Can you help me?

My Coordinator Code:

#include <SoftwareSerial.h>
#define xBeeRxPin 10
#define xBeeTxPin 9
int deger3;
int deger4;
String d1,d2,d3;
SoftwareSerial xBeeSerial(xBeeRxPin, xBeeTxPin);

void setup() { 
  Serial.begin(38400);
  xBeeSerial.begin(38400); 
}void loop() {
  
  deger3 = analogRead(A0);
  deger4 = analogRead(A2);
  
  String deger5 =  (String)deger3 + ";" + (String)deger4 ;

  xBeeSerial.println(deger5);
  
  delay(50);
}

My Receiver Code:

#include <SoftwareSerial.h>
#include <Servo.h>
#define xBeeRxPin 10
#define xBeeTxPin 9
SoftwareSerial xBeeSerial(xBeeRxPin, xBeeTxPin);
String d1,d2,d3;

const int trigPin = 4;
const int echoPin = 3;
long duration;
int distance;
Servo myServo;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  xBeeSerial.begin(38400);
  Serial.begin(38400);
  myServo.attach(11); 
}
void loop() { 
  for(int i=15;i<=165;i++){  
    myServo.write(i);
     delay(10);
    digitalWrite(trigPin, LOW); 
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH); 
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, 11200); 
    distance= duration*0.034/2;

    
    if(xBeeSerial.available()>0){
      d1=xBeeSerial.readString();
    }
  }
  
  for(int i=165;i>15;i--){  
    myServo.write(i);
     delay(10);
    digitalWrite(trigPin, LOW); 
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH); 
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, 11200); 
    distance= duration*0.034/2;
  

    if(xBeeSerial.available()>0){
      d1=xBeeSerial.readString();
  }
 }  
}
2
  • What exactly is this "problem in the operation of the servo motor"? Please edit that into your question.
    – ocrdu
    Dec 22 '20 at 13:02
  • I arranged. Stuttering.
    – Enes Orhan
    Dec 22 '20 at 13:11
1

When SoftwareSerial enters an interrupt each time it receives a character and busy-waits (delays) to time in each data sample as the bits in the character come in. No other interrupt service routines can run while this is happening. It also turns off interrupts when it sends.

The servo library for the AVR uses timer interrupts to generate the servo position signals.

When characters are being received, SoftwareSerial will interfere with the generation of the servo position signal because only one interrupt service routine can run at a time.

You may be able to mitigate the problem adding significant delay between characters sent, reducing the total number of characters sent. This would limit the interference and give servo time to get back to its correct position from however far it managed to deviate over the course of an inbound serial character.

There are also alternative libraries that use the AVR's Input Capture feature on an ICP pin to do some of the work of serial in hardware, and that may work for you. Here's one example library.

@ocrdu has rightly prompted me to add that there are servo libraries that will do the servo signal generation in hardware.

4
  • 1
    I removed some material regarding I2C/Wire because this was probably completely useless advice with XBee being involved.
    – timemage
    Dec 22 '20 at 13:29
  • 1
    Maybe a servo library that doesn't use interrupts can be found also?
    – ocrdu
    Dec 22 '20 at 13:31
  • 1
    Yeah, I had a thought to add that, but don't off-hand know of one. If you have a recommendation I'll put it in, or if you put one in a separate answer I'll up-vote it.
    – timemage
    Dec 22 '20 at 13:33
  • 1
    Perhaps I'll add in arduino.cc/reference/en/libraries/servo-hardware-pwm if you add an answer, I'll remove it and specifically call out your answer.
    – timemage
    Dec 22 '20 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.