Servo and SoftwareSerial using together
Moderators: adafruit_support_bill, adafruit

Servo and SoftwareSerial using together

by mwtse on Fri Jun 15, 2012 7:03 am

I'm using UNO, Arduino 1.0.1,

Pin 9 as the PWM output of Servo.

When I use SoftwareSerial, the PWM timing is affected. I observe the output with a CRO, the output pulse is lengthen periodically. When I comment out the code that uses SoftwareSerial, the PWM output is dead steady.

Servo servo;
SoftwareSerial serial(4,255); // I do not need Tx, since it is used to read sensor

servo.attach(9);
servo.write(100);

Any chance of a work around?
mwtse
 
Posts: 8
Joined: Sun May 20, 2012 4:33 am

Re: Servo and SoftwareSerial using together

by adafruit_support_bill on Fri Jun 15, 2012 7:07 am

The Servo library and SoftwareSerial both use the same timer. There is a ServoTimer2 library that uses a different timer. There are several versions of this library out there - some of which may not be compatible with current versions of the IDE. If you search these forums, there was a thread on the topic not long ago.
User avatar
adafruit_support_bill
 
Posts: 29128
Joined: Sat Feb 07, 2009 9:11 am

Re: Servo and SoftwareSerial using together

by mwtse on Fri Jun 15, 2012 7:35 pm

Thanks, let me search for it.
mwtse
 
Posts: 8
Joined: Sun May 20, 2012 4:33 am

Re: Servo and SoftwareSerial using together

by mwtse on Sun Jun 17, 2012 8:52 pm

I've switched to ServoTimer2 library but the problem persist. CRO shown that the pulse width change (become much wider) nearly every second (I've put a 1 second delay). However, the change in pulse width do not occur every time. The following is the bare minimum code that can reproduce the problem, any hint?


#include <ServoTimer2.h> // changed from Servo.h
#include <SoftwareSerial.h>
ServoTimer2 servo; // create servo object to control a servo
SoftwareSerial sensorSerial(2, 255, true);
//pin 3 of Arduino connect to Rx of sensor, a HIGH enable the ranger sensor
const unsigned char sensor=3;

void setup()
{
servo.attach(9); // attaches the servo to a pin
servo.write(100); // sets the servo position

pinMode(sensor, OUTPUT);
digitalWrite(sensor, LOW); // switch off the range sensor until needed
sensorSerial.begin(9600); // set the data rate for the SoftwareSerial port
}

void loop() {
digitalWrite(sensor,HIGH);
delay(1); // 1 ms is enough to get senor to make 1 measuerment
digitalWrite(sensor,LOW);
delay(49); // sensor need time to make measurement
while (sensorSerial.available()) {
char c = sensorSerial.read();
}
delay(1000);

}
mwtse
 
Posts: 8
Joined: Sun May 20, 2012 4:33 am

Re: Servo and SoftwareSerial using together

by adafruit_support_bill on Mon Jun 18, 2012 4:54 am

That is strange. How much does the pulse width change?
What happens if you reset the servo position in the loop? Does it go back to the original width?
User avatar
adafruit_support_bill
 
Posts: 29128
Joined: Sat Feb 07, 2009 9:11 am

Re: Servo and SoftwareSerial using together

by pjakobs on Mon Mar 11, 2013 3:22 am

I'm looking at the same problem, trying to built a car that uses GPS navigation. While I could switch to the hardware serial for the "finished article", I'd lose the ability to use serial debug output. Not good.

I understand that it's software serial blocking interrupts and thus leading to the Servo code overshooting the end of the PWM pulse. I'm not 100% sure if that really is the case, I've removed all cli() calls from SoftwareSerial.cpp, which didn't particularly change the serial performance (I guess at 9600 Baud and reading only, there's enough time anyway), but it didn't change the servo behaviour either.

I havn't found anything really useful on the web, has there been any work on this?

pj
pjakobs
 
Posts: 2
Joined: Sun Mar 10, 2013 1:10 pm

Re: Servo and SoftwareSerial using together

by adafruit_support_bill on Mon Mar 11, 2013 5:36 am

I haven't seen anything on the topic for a while. There are a couple of options to sidestep the whole issue: You could move to a Mega with its additional hardware serial ports. Or you could offload the servo control to a PWM breakout board: https://www.adafruit.com/products/815
User avatar
adafruit_support_bill
 
Posts: 29128
Joined: Sat Feb 07, 2009 9:11 am

Re: Servo and SoftwareSerial using together

by pjakobs on Mon Mar 11, 2013 10:36 am

yeah, I thought of the latter. I actually even have the 16Ch board here, but I think I rather go with a Tiny85 and try to use it to translate serial to I2C. That would also offload the tedious task of waiting for serial input and free up resources on the Arduino.
pjakobs
 
Posts: 2
Joined: Sun Mar 10, 2013 1:10 pm