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.

Comments

  1. Alex Rossie says:

    I always have this problem.
    Does former mean current or previous? and please don’t reply with “the former” or “the latter”.

    • Here’s your answer ๐Ÿ˜‰
      http://bit.ly/w3RStw

    • Renee says:

      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.

    • Sorry… you kind of set yourself up for a smart-ass answer ๐Ÿ˜‰

      To add to Renee’s very well written description:

      I usually think of Latter as Later (i.e. The Future), and Former is the opposite of Later (i.e. the Past)… in reference to the order in which you read the words ๐Ÿ˜‰

    • David Dean says:

      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.

    • Shadyman says:

      “formerly” ~= ‘previously’ (as in ‘the artist formerly known as Prince’)

      An easy way to remember former vs latter is that ‘latter’ comes ‘later’.

  2. I always use NewSoftSerial in my Arduino projects, so I’m going to have to check this out! Thanks HAD!

  3. Dan says:

    “The obvious solution is to write one in software. ”

    Isn’t the obvious solution to use a more capable chip with multiple UARTs?

    • medix says:

      **coughxmegacough**

    • Paul says:

      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!

      • rasz says:

        There is a whole world of other chips , you dont have to use arduino.

        Btw, $60 buys you a FULL BLOWN 128MB 500MHZ Linux board and you use arduino?

      • my2c says:

        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! ๐Ÿ™‚

      • my2c says:

        – 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…

      • axodus says:

        @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…)

      • Some Random Dude says:

        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?

      • Daid says:

        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.

      • Garbz says:

        @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.

      • Ian Tester says:

        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.

    • Dan says:

      “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)

    • Eirinn says:

      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 ๐Ÿ™‚

  4. Elias says:

    The former means the earlier mentioned one, the latter means the one that is mentioned later in the sentence.

  5. Johnnie says:

    *you’re

  6. Andy says:

    Use a chipKIT Uno32 from Digilent. PIC based Arduino clone that has multiple UARTS for $27.

    • Paul says:

      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.

  7. Reggie says:

    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?

  8. Reggie says:

    @Paul, thanks for taking the time to write the library, It’ll be much appreciated by a lot of people I’m sure.

  9. biomed says:

    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.

    • Alex says:

      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.

  10. Bau says:

    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.

  11. There are also 4 and 6 pins avr’s, they are called the Tinyxx, xx being two numbers you know..

  12. Reggie says:

    Bau, there’s nothing wrong with pics, except in the context of this article, they’re irrelevant, just as arm, intel, amd etc. are too ๐Ÿ™‚

  13. david says:

    what i want to know is .. what kind / brand breadboard is that?

  14. synth says:

    nice grammar, Szczys.

  15. 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…

    • 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.

    • Paul says:

      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”….

  16. Dennis Ferron says:

    Or you could just use the Propeller where all the serial ports are implemented in software already.

Speak Your Mind

*

Related Hacks in arduino hacks

  • Garden sensors measure soil moisture and greenhouse temperature
  • More pins and more power with a DIY Sanguino
  • A much easier take on an Android garage door opener
  • UnoJoy makes your Arduino play well with Xbox 360 or PS3
  • RC car controller and receiver replacement