SoftwareSerial conflicting with Servo?

I am not really sure the best place to put this inquiry, but this seems like a good place to start.
I am using the Arduino development environment, but not the Arduino boards. I'm programming and Atmega328P.

I wrote one program to learn how to do servos. I attached it to a pot and it worked great.
I wrote another program to learn how to use SoftwareSerial. I have the UART attached to a physical device, and the software serial port attached to a Raspberry Pi. I use this the software serial for diagnostics.

I merged these two applications because I want to control the servo from the data coming into the UART.
However, during the "loop()" section of my application, where I am printing data to the software serial device the servo is just bouncing around for some reason. I've made not attempts to control it at this point.

After some ridiculous debugging, I finally figured out that when data is sent to the software serial port, the servo reacts.
I tried changing ports around for the serial device, but no luck!

My software serial port is currently using 4,10, and my servo is on 9.

Any suggestions on what could be the cause of this? It's really handy to diagnose over the serial line, but not when the servo is going nuts.


SoftwareSerial library turns off interrupts during a character transmit, which
will severely affect the timing of timer1 interrupts used by Servo library.

If you are using a low baud rate this inhibition of interrupts isn't needed anyway,
but the library does it whatever the baudrate for some reason (it probably needs
fixing) For a high baud rate the interrupts are disable to ensure the transmit
character has correct timing.

You can try commenting out the cli() call in SoftwareSerial::write ()...

Oh wow. Thanks for such a concise answer! I'm not sure what defines a 'low' baud rate, but I did read there are issues with using SoftwareSerial over 9600. So, I stuck with 9600.

I will check out the SoftwareSerial's write() call, as you recommended.


I was doing some research on this topic for a project of mine as well. The recommendation I got from someone else that did this research before me (thanks google) was to increase baud rate so that the servo is affected less when you send via software serial fast enough. I think if you are ok with 9600 rate or even lower ones, comment out the cli() will be best.

I use this the software serial for diagnostics.

If it's not necessary during normal operation, it would be worth disabling sending as far as possible. If it's still a problem, and assuming you've restricted the output so it occurs as rarely as possible, you could detach the servo at the start of SoftwareSerial output and reattach after the output has completed. Most servos will go inert while they are detached. This assumes you can afford to disable the servo briefly every time the output occurs. When you reattach the servo it will probably glitch briefly, but that is likely to be less intrusive than sending it an inaccurate signal during the whole of the SoftwareSerial output.