AltSoftSerial Looks To Speed Up Arduino Software UART

So let’s say your using an Arduino in your project. You already have the hardware-based serial interface working with one portion of the project and need a second serial port for unrelated hardware. The obvious solution is to write one in software. But this is a place where working in the Arduino environment gets really hairy. Since there’s a layer of abstraction between the code and the hardware interrupts, it can be difficult to know if you are going to have timing problems. But there’s a new library available which seeks to reduce the latency of software-based serial communications so that you don’t have to worry about it.

It’s named AltSoftSerial because it is a software-based serial library that is an alternative to the NewSoftSerial package. The former can function with just  2-3 microseconds of latency, while the latter has as much as a 174 microsecond hit. If it functions as advertised that’s quite an improvement. It’s not hard to put together a hardware test platform, and the example program is only about a dozen lines of code (which is the beauty of working in this environment) so give it a try if you have a free hour here or there.

42 thoughts on “AltSoftSerial Looks To Speed Up Arduino Software UART

    1. If I’m making a sentence about Thing A and Thing B, then the former would be Thing A and the latter would be Thing B

      Former and latter follow the same order as the objects being addressed.

      Think former as being first and latter being later.

      Though it’s a bit formal, and requires that the reader have good memory.

    2. I always cringe a little when authors do this. Relational references to a list in a previous sentence always bother me. But it gets bad when there’s a relational property of those two items, as there is here. So you’re left with, “is ‘former’ the old serial library, or the first item in the preceding list?” You just have to work it out by context. In this case, “former” meant the first item in the list (as it usually does), not the older library.

    1. Yes, you are absolutely right, using a board with several real hardware serial ports is the obvious solution. That obvious solution is named “Arduino Mega” and generally is priced at $60.

      If you already have a $30 Arduino Uno and need to connect 1 more device, a software-only solution starts to look pretty good.

      Even better, on Teensy ($16), Wiring-S ($28) or Sanguino ($35), there is one extra hardware serial port, so a software solution lets you have TWO serial devices. I don’t believe there are any lower cost Arduino-compatible boards with two unused serial ports.

      For full disclosure – I am the author of this library, and the author of Teensyduino (the add-on software that makes Teensy compatible with Arduino).

      I wrote this library because people have trouble with the existing software serial libraries when they try to build projects which have simultaneous data transfer. Sure, an expensive board with lots of real serial ports is a perfect answer in a technical sense, but a more compatible software-only solution is a lot more “obvious” when it comes to hardware cost!

      1. Thanks Paul! – I have spent a number of hair pulling hours troubleshooting dropped data because of simultaneous rx/tx on hardware and software serial with a 328P, I’ll look forward to giving this a try in the future! :-)

      2. – And btw, you may be able to get linux board for $60, but that still doesn’t compare to roll your own arduino compatible as I usually use. $5 or $6 gets you rolling with one of those…

      3. @Paul
        great library, thanks for sharing it.
        @rasz
        you got me interested here, which linux board do you use, and compared to the arduino or any other micro-controller how hard will it be to cross over and use it? (i’m used to direct control the hardware in c/c++. not really knowing how to write kernel drivers for linux…)

      4. Out of curiosity, where does one procure this $60 assboard, and does it run on the few milliamps of current like the Arduino or have digital or analog IO readily available?

      5. You’re problem is not that the chips have 1 UART. The problem is that you stick with only what you know.

        First problem is the Arduino is obviously expensive. 30$… I can build one for 30$, including shipping costs for all components and have my PCB manufactured per piece (which is very expensive, PCB production in bulk is much cheaper)

        The next problem, why UART? It’s outdated, not everyone has a real UART anymore. I know, let’s use USB! http://www.obdev.at/products/vusb/benefits.html and you can with a simple AVR. It’s even cheaper in hardware then any UART solution. And you get a free power source!

        But you stick with what you know and pay 30$+ for an Arduino.

      6. @Daid, Yes a ASCII output by one device readable by any other device by simply knowing the speed at which you’re transferring is totally comparable to implementing the USB protocol and writing software to read the data you want. Even if you’re paid minimum wage I’ll bet you the average hacker will be able to implement UART, go to work, and earn the $30 for the Arduino before most people finish figuring out how to code for USB.

        UART may be outdated, but there’s a good reason people still use it.

      7. Bravekit over in Ukraine has two STM32-based “header” boards. The STM32F100 board has two USARTS for $15, while the STM32F103 board for $26 has three. And lots of other I/O, of course. Their USARTS can do DMA-driven I/O too, for potentially very large buffers and less intervention needs from the CPU.

        But programming such uC’s are undoubtedly much more difficult than Arduino compatibles. So thanks for working on this library.

    2. “The next problem, why UART? It’s outdated, not everyone has a real UART anymore. I know, let’s use USB!”

      Who says you’re communicating with a computer?

      What if you need full duplex communication between two boards but don’t have an extra wire for a clock? (This happened to me recently in a project at work. Custom cord we had a billion of, and they couldn’t give me an extra wire)

    3. You are absolutely right! Use another chip and then do the many years to become an EE – never mind you just wanted to make a cool RFID reader for your carport, it’s completely worth it! /sarcasm.

      In the ideal world one would just make a chip from logic gates and be done with it :)

    1. ChipKit is impressive hardware, but the Arduino compatibility still leaves quite a lot to be desired.

      Sure, you get multiple serial ports. But do ANY of them support buffered transmit? Admittedly Arduino added this only in 1.0, but it is present now on all hardware serial ports on Arduino. Teensyduino has had it for years. Without transmit buffering, you’ll really only be able to maintain full output rate on a single port, even even that requires wasting CPU cycles waiting in blocking transmit.

      For another example, I recently tested my Encoder library with a ChipKit board.

      http://www.pjrc.com/teensy/td_libs_Encoder.html

      You can read about it near the bottom of the page in the “Low Performance Polling Mode” section. It’s a perfect example of incredible hardware that can’t do a fraction of what lesser hardware with good software can accomplish.

      I’m sure Diligent (and maybe community contributions?) will eventually improve the software. But if you look the hardware alone, you’ll get a pretty unrealistic idea of what you can accomplish by using on the easy-to-use Arduino functions and Arduino libraries.

  1. LOl, at all the pic fans and ‘you can get a full blown assboard for $£60’ guys, this isn’t for you, this is for people that are using an arduino or compatible atmega chips that could do with an extra port.

    That’s the thing with tinkerers, you don’t necessarily know that you’ll need extra things until you have a need for them, why spend $60 when I can spend $10 and do stuff in software?

  2. Former…. the one that came first. Usually used in the temporal sense as in the Apple][ came before the IBM, where IBM would be the latter. Several examples above may be confusing as they refer to the structure of the sentence such as “IBM was the new kid on the block, knocking the APPLE][ from it’s king of the hill position” where in this instance IBM is the former IN THE SENTENCE.

    That will either clear it all up… or totally confuse you.

    Glad to be of help.

    1. While your explanation is good, I just wanted to point out that your example is not. IBM was around decades before Apple. IBM was probably around before Jobs himself. If you mean the IBM PC, then, carry on.

  3. IMHO ChipKit came about just so Microchip is part of the game, not that they needed to be. Of course the software support won’t be the same as Atmel.

    However the real benefit for me is using the ubber-small PICs – those with only 4-6 pins that fit into real small places. Sure there is no GCC support for those but at this level of development C becomes impractical. Let’s not start the Atmel vs Microchip touchy-feely favorite chain.

    Overall I agree that Ardunio’s contribution to the community was really in making the software platform available and extensible so what we have today is not easy to beat.

  4. You need to clarify what is meant by latency in this context… my immediate thought was that it was a turnaround time – i.e in a simple echo program how long does it take for me to see my character again.

    Having read the website it’s not round trip latency in a simple echo test program context, but an interrupt latency – as in how much is having this software library in my software is going to potentially delay all other interrupts in the worst case.

    Copying headline numbers without clear definition is never good…

    1. Right. Interesting to know what we are actually talking about here…

      However, the maximum baud rate is often not the most important question. Each library imposes interrupt latency on other libraries. AltSoftSerial causes approximately 2-3 µs latency. NewSoftSerial causes 10 bit times of latency for other libraries. Running at 57600 baud, that’s 174 µs! This latency is the primary difference between AltSoftSerial and NewSoftSerial.

    2. Yes, you’re absolutely correct, the HAD summary dropped the word “interrupt” before latency.

      Hopefully the take away message is still the fact that an improved/alternate software serial library is now available. Either that, or finer grammatical point of using “former” and “later”….

  5. Can you give a few more details for the naive, on how this library uses timers, and where we can see potential conflicts? I’m seeing some weird results when I also have a “tone(…)” command running in the background on my Arduino Fio. Is this possibly due to a timer conflict? Thanks,

    -Jordan

    1. I believe I figured out the problem or at least how to mitigate it. I had been calling “tone” without the optional duration argument. Adding in a duration seems to clear up the issues I was having.

  6. Hi

    Altsoftserial library is really fantastic for 2 way communication.
    I was using softwareserial with GSM for sending data over GPRS over 10 seconds and it was working but the problem was: i was not able to check response from GSM if any error occurred.

    Then i use altsoftserial and it’s life saving but i can see one minor problem here.
    Now instead of sending data in interval of 10 seconds it’s send 2 data in 20 seconds and sometime 3 data in 30 seconds.

    Looking for your help!!

    Thanks,
    Somnath

Leave a Reply to DanCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.