NewSoftSerial

A New Software Serial Library for Arduino

News: NewSoftSerial is in the core!  Starting with Arduino 1.0 (December, 2011), NewSoftSerial has replaced the old SoftwareSerial library as the officially supported software serial library.  This means that if you have 1.0 or later, you should not download this library.  To port your code to 1.0, simply change all NewSoftSerial references to SoftwareSerial.

NewSoftSerial is the latest of three Arduino libraries providing “soft” serial port support. It’s the direct descendant of ladyada’s AFSoftSerial, which introduced interrupt-driven receives — a dramatic improvement over the polling required by the native SoftwareSerial.

Without interrupts, your program’s design is considerably restricted, as it must continually poll the serial port at very short, regular intervals. This makes it nearly impossible, for example, to use SoftwareSerial to receive GPS data and parse it into a usable form. Your program is too busy trying to keep up with NMEA characters as they arrive to actually spend time assembling them into something meaningful. This is where AFSoftSerial’s (and NewSoftSerial‘s) interrupt architecture is a godsend. Using interrupt-driven RX, your program fills its buffer behind the scenes while processing previously received data.

Improvements

NewSoftSerial offers a number of improvements over SoftwareSerial:

  1. It inherits from built-in class Print, eliminating some 4-600 bytes of duplicate code
  2. It implements circular buffering scheme to make RX processing more efficient
  3. It extends support to all Arduino pins 0-19 (0-21 on Arduino Mini), not just 0-13
  4. It supports multiple simultaneous soft serial devices.*
  5. It supports a much wider range of baud rates.**
  6. It provides a boolean overflow() method to detect buffer overflow.
  7. Higher baud rates have been tuned for better accuracy.
  8. It supports the ATMega328 and 168.
  9. It supports 8MHz processors.
  10. It uses direct port I/O for faster and more precise operation.
  11. (New with version 10).  It supports software signal inversion.
  12. (New) It supports 20MHz processors.
  13. (New) It runs on the Teensy and Teensy++.
  14. (New) It supports an end() method as a complement to begin().

*But see below for an important caveat on multiple instances.
**Be circumspect about using 300 and 1200 baud though. The interrupt handler at these rate becomes so lengthy that timer tick interrupts can be starved, causing millis() to stop working during receives.

Using Multiple Instances

There has been considerable support for an library that would allow multiple soft serial devices. However, handling asynchronously received data from two, three, or four or more serial devices turns out to be an extremely difficult, if not intractable problem. Imagine four serial devices connected to an Arduino, each transmitting at 38,400 baud. As bits arrive, Arduino’s poor little processor must sample and process each of 4 incoming bits within 26 microseconds or else lose them forever. Yikes!

It occurred to me, though, that multiple instances could still be possible if the library user were willing to make a small concession. NewSoftSerial is written on the principle that you can have as many devices connected as resource constraints allow, as long as you only use one of them at a time. If you can organize your program code around this constraint, then NewSoftSerial may work for you.

What does this mean, exactly? Well, you have to use your serial devices serially, like this:

#include <NewSoftSerial.h>

// Here's a GPS device connect to pins 3 and 4
NewSoftSerial gps(4,3);

// A serial thermometer connected to 5 and 6
NewSoftSerial therm(6,5);

// An LCD connected to 7 and 8
NewSoftSerial LCD(8,7); // serial LCD

void loop()
{
  ...
  // collect data from the GPS unit for a few seconds
  gps.listen();
  read_gps_data();  // use gps as active device
  // collect temperature data from thermometer
  therm.listen();
  read_thermometer_data(); // now use therm
  // LCD becomes the active device here
  LCD.listen();
  LCD.print("Data gathered...");
  ...
}

In this example, we assume that read_gps_data() uses the gps object and read_thermometer_data() uses the therm object. Any time you call the listen() method, it becomes the “active” object, and the previously active object is deactivated and its RX buffer discarded. An important point here is that object.available() always returns 0 unless object is already active. This means that you can’t write code like this:

void loop()
{
  device1.listen();
  if (device1.available() > 0)
  {
    int c = device1.read();
    ...
  }
  device2.listen();
  if (device2.available() > 0)
  {
    int c = device2.read();
    ...
  }
}

This code will never do anything but activate one device after the other.

Signal Inversion

“Normal” TTL serial signaling defines a start bit as a transition from “high” to “low” logic.  Logical 1 is “high”, 0 is “low”.  But some serial devices turn this logic upside down, using what we call “inverted signaling”.  As of version 10, NewSoftSerial supports these devices natively with a third parameter in the constructor.

NewSoftSerial myInvertedConn(7, 5, true); // this device uses inverted signaling
NewSoftSerial myGPS(3, 2); // this one doesn't

Library Version

You can retrieve the version of the NewSoftSerial library by calling the static member library_version().

int ver = NewSoftSerial::library_version();

Resource Consumption

Linking the NewSoftSerial library to your application adds approximately 2000 bytes to its size.

Download

The latest version of NewSoftSerial is available here: NewSoftSerial12.zip.  Note: don’t download this if you have Arduino 1.0 or later.  As of 1.0, NewSoftSerial is included in the Arduino core (named SoftwareSerial).

Change Log

  1. initial version
  2. ported to Arduino 0013, included example sketch in package
  3. several important improvements: (a) support for 300, 1200, 14400, and 28800 baud (see caveats), (b) added bool overflow() method to test whether an RX buffer overflow has occurred, and (c) tuned RX and TX for greater accuracy at high baud rates 38.4K, 57.6K, and 115.2K.
  4. minor bug fixes — add .o file and objdump.txt to zip file for diagnostics.
  5. etracer’s inline assembler fix to OSX avr-gcc 4.3.0 interrupt handler bug added.
  6. ladyada’s new example sketch, fix to interrupt name, support for 328p.
  7. etracer’s workaround is now conditionally compiled only when avr-gcc’s version is less than 4.3.2.
  8. 8 MHz support and flush() and enable_timer0()  methods added
  9. digitalread/write scrapped in favor of direct port I/O.  Revised routines now get perfect RX up to 57.6K on 16MHz processors and 31.25K on 8MHz processors.
  10. inverted TTL signalling supported.  20MHz processors supported.  Teensy and Teensy++ supported.  New end() method and destructor added to clean up.
  11. added listen() method to explicitly activate ports.
  12. warn users about 1.0 conflict

Acknowledgements

Many thanks to David Mellis, who wrote the original SoftwareSerial, and to the multi-talented ladyada, whose work with AFSoftSerial is seminal.  Ladyada also provided the “Goodnight, moon” example sketch, fixed a problem with the interrupt naming (see v6) and tested NSS with the 328p.

Thanks also to rogermm and several other forum users who have tested NewSoftSerial and given useful feedback.

The diligent analysis of forum user etracer yielded the root cause of a tricky problem with NSS on OSX.  A bug in avr-gcc 4.3.0 causes the compiler to fail to generate the proper entry and exit sequences for certain interrupt handlers.  etracer identified the problem and provided an inline workaround.  etracer’s fix is in NSS 5.

User jin contributed a large body of work based on NSS and identified a potential problem that could result in data loss (fixed in NSS 5).  jin also made a variant of NSS that supports 4-pin serial, with the additional pins providing a very nice RTS/CTS flow control.  We may see this in NSS in the near future.

Thanks to Garret Mace, who contributed the delay tables for 20MHz processors and claims that he can send and receive at 115K baud.  Cool!

Thanks to Paul Stoffregen, both for his fine work with Teensy and Teensy++, and for contributing some useful suggestions that help NewSoftSerial run on them without modification.

I appreciate any and all input.

Mikal Hart

Page last updated on July 3, 2013 at 7:37 pm
647 Responses → “NewSoftSerial”

  1. Nenad

    16 years ago

    Hi there,
    I just started using your library and its pretty awesome.
    I looked through the code and it looked to me that there was a slight chance for improvement.
    I was thinking about replacing tunedDelay() with a timer based interrupt. Did you ever consider that? If you explain i.e give me sample code instead of assembly, and some timing info for this routine, maybe I could offer some help too.
    Thanks


  2. Kurt Schulz

    16 years ago

    Hi Mikal,

    I’m having exactly the same problem described by Andre Crone when compiling the library with the Antipasto Arduino IDE. I’d really like to use NewSoftSerial to talk to the TouchShield Slide. Any thoughts on what might be the problem?

    Thanks, Kurt


  3. Mikal

    16 years ago

    Nenad, at some point I do think we should get rid of tunedDelay. The reliance on that inline assembler sometimes chokes certain Linux distributions. If you can come up with an alternate that doesn’t affect the timing — and that’s a big if — go for it. I’m reluctant to consume a timer resource, though, when the current implementation works well for most people.

    Mikal


  4. Mikal

    16 years ago

    Andre–

    Sorry for the slow reply. Reading through those errors, I have to conclude that in your environment NULL is not defined. It may be as simple a matter as adding this to the top of NewSoftSerial.h:

    #define NULL 0

    or possibly

    #define NULL ((void *)0)

    Mikal


  5. Mikal

    16 years ago

    Hi Kurt–

    Thanks for reminding me that I need to get back to Andre. See his response.

    Mikal


  6. Kurt Schulz

    16 years ago

    You’re right Mikal – it was as simple as defining NULL in the header file.

    Thanks!


  7. sm

    16 years ago

    Quick question… is there a way to configure only a single pin for Tx or Rx? For example, can I set up only a Tx pin to support a serial LCD unit and thus not waste a pin for Rx (since it will never be used)?

    Thanks for some great work!


  8. Mikal

    16 years ago

    sm —

    Yeah, if you just choose an invalid pin number like -1 for the direction that you don’t use, you can save the pin. For example, on a GPS app that only does receives you might do something like

    NewSoftSerial nss(3, -1);
    Mikal


  9. sm

    16 years ago

    Excellent! Thank you Mikal.


  10. aenigma

    16 years ago

    Wanting to find out if I can do all this in the same sketch on an ATmega328 running at 3.3V/8MHz:

    1. Use NewSoftSerial to communicate with GPS (pins 3,4 on Arduino)
    2. Use the GPS logging libraries here: http://www.ladyada.net/make/gpsshield/logging.html, replacing the hardware UART with NewSoftSerial, to log GPS/sensor data to SD card
    3. Read/write serial data to the hardware UART (connected to XBee, for example)

    Is this asking too much of the available processing/RAM?

    Thanks!


  11. Timc995

    16 years ago

    JDMartin,
    If you have 7 arduinos and need to have them all feed data, you might be able to set them up in a round robin. IE: #1 sends to #2. #2 echoes #1 (or generates its own data) and sends #3, etc. This way, all of the output is sequentially fed downstream and you don’t have to deal with multiple simultaneous reads at the tail end.


  12. DDD

    16 years ago

    So would I be able to have 4 seperate outputs (1 hardware UART and 3 newsoftserial) and run them all at 31250 baudrate?
    If I sent a byte to each one how out of sync would they all be?

    e.g.
    If did this:
    Serial.print(myByte);
    MySerial1.print(myByte);
    MySerial2.print(myByte);
    MySerial3.print(myByte);

    How much delay would there be between each byte being sent?


  13. Mikal

    16 years ago

    DDD, my guess is that if you were doing 4 transmits (only), this should work fine. Each call to NewSoftSerial::print() would take 0.3 ms.

    Mikal


  14. Jeff O'Brien

    16 years ago

    I noticed you added support for a 20mhz clock.
    Is there anyway to make it work with an arduino running a 12mhz clock?

    I’m using wiblock’s NB1A board that runs a 12mhz clock
    http://wiblocks.luciani.org/NB1/NB1A-index.html

    let me know if i need to bail…
    Thanks
    Jeff


  15. Mikal

    16 years ago

    Probably– at 8MHz you are somewhat capped on baud rate though. If you’re using using one of those 4800-baud GPS, I would expect it to work ok.


  16. Mikal

    16 years ago

    Jeff–

    If someone could generate the appropriate timing tables for 12MHz — I don’t have any test devices — I’m sure it would work fine. You could just do an approximation by averaging the 8 and 16MHz values.

    Mikal


  17. Obi

    16 years ago

    Hello Mikal thank you for your support with these libraries I was wondering if the Mega supported newsoftserial library have been released yet


  18. Patrick

    16 years ago

    I could use some help. I am using this for my GPS. I am trying to get the bearing and i understand that the GPS does that for you.

    // track angle
    parseptr = strchr(parseptr, ‘,’)+1;
    trackangle = parsedecimal(parseptr);

    Is the above in the code for the bearing?
    if it is, i can not get it to tell me bearing.
    It prints random numbers and sometimes some weird characters. Every other part of the code works and i have written my own distance code that works. I also wrote code for telling me bearing from my current location to a different GPS point. I would like to get my current bearing when i move but i can not get the above code to work. Any help would be appreciated.


  19. Mikal

    16 years ago

    Hi Obi–

    No, not yet. But the next version of Arduino will likely contain a version of NewSoftSerial that does support Mega.

    Thanks,

    Mikal


  20. Mikal

    16 years ago

    Patrick, are you using TinyGPS? TinyGPS does support the “course” field. Is that what you’re talking about?

    Mikal


  21. Patrick

    16 years ago

    Mikal

    I am using your newsoftserial library and the example gps code that comes with it.


  22. Patrick

    16 years ago

    Mikal

    I was able to easily switch my formulas over to the tinygps library. So i will use that one since it gives heading. Thank you.


  23. kee nethery

    16 years ago

    Not to be unappreciative but … is there any documentation? For example: it is not obvious in NewSoftSerial myGPS(3, 2); if pin 3 is TX or RX. I’m sure I can connect up to a serial LCD and figure it out but it would be nice if there was documentation.

    As a suggestion, perhaps grab all the docs associated with: http://arduino.cc/en/Reference/Serial and alter them to describe NewSoftSerial. Less work for you (or someone knowledgeable) and it would allow beginners to compare and contrast. Thanks!


  24. Joe

    16 years ago

    I am using 10c of your NewSoftSerial and see strange behaviour in my sketch.
    I can call the print method from the sketch but when it is called from a lib
    via a ptr passed in to the “translator” contructor, print does nothing. I have put this on the o-scope to verify.

    nss.print(‘X’, BYTE); // Put this in for DEBUG. It works.

    translator.sendPackerRequest();

    if (translator.waitForPacket() == true) { // The “print” in “waitForPacket()” does nothing…
    Serial.println(“DEBUG: Received Packet”);
    // Received packet, and passed checksum. Now parse it.
    //translator.parsePacket(); // Done in waitForVipecAPacket now
    //translator.translate();
    //translator.sendPacket();
    }
    else {
    //Serial.print(“.”);
    Serial.println(“DEBUG: Failed to Receive Packet”);
    }


  25. Mikal

    16 years ago

    Kee, the documentation is indeed a little weak I agree. For your specific situation the RX is always the first parameter (just like in SoftSerial).

    Mikal


  26. Mikal

    16 years ago

    Joe, without looking at the code for this “translator” object I wouldn’t be able to guess why your application doesn’t print anything.

    Mikal


  27. Spencer

    16 years ago

    I am trying to load your code into my arduino and its not working. I am new and maybe I don’t understand what to do. I have an Arduino and a gps and I am pasting your code into my version 18 of the arduino IDE and it comes up with errors. Do I need to download TinyGPS to arduino? Please help.


  28. Mikal

    16 years ago

    You didn’t say what code you are trying, but if it requires TinyGPS, then you need to install it correctly. Perhaps if you listed the first couple of error messages?…


  29. Phlogi

    16 years ago

    Hi there

    Is there any development version of this that has already support for the Arduino Mega? If not I’ll start porting it soon.


  30. pantelis

    16 years ago

    Does the new newsoftserial version (10c) makes the feedgps() function of tinyGPS not compatible or something?

    i mean:
    ////////////////////////////////////////////////
    from newsoftserial
    /////////////////////////////////////////////////
    This means that you can’t write code like this:
    void loop()
    {
    if (device1.available() > 0)
    {
    int c = device1.read();

    }

    ////////////////////////////////////
    tinyGPS
    bool feedgps()
    {
    while (nss.available())
    {
    int c = nss.read();
    if (gps.encode(c))
    {
    // process new gps info here
    }
    }
    }
    ////////////////////////////////////////////

    I’m using tinyGPS with a ping ultrasonic sensor. I didnt try to create a newsoftserial object for the sensor (yet), but it seems that it fails to work as it does, when I use it without the gps and a newsoftserial object.
    For example, I have set my vehicle to stop its motors when the sensor reads an obstacle in a distance of 70cm. Instead, the vehicle stops well below 70cm and sometimes it does not stop at all.

    here’s what I do:

    void loop()
    {
    get_gps_data();
    if (newdata)
    {
    if(sensor_readings < 70) avoid_obstacle_routine();
    forward();
    }
    }
    —————————————–
    bool feedgps()
    {
    while (nss_gps.available())
    {
    if (gps.encode(nss_gps.read()))
    return true;
    }
    return false;
    }

    void get_gps_data()
    {
    nss.begin(4800);
    unsigned long start = millis();

    // Every 1 second we print an update
    while (millis() – start < 1000)
    {
    if (feedgps()) newdata = true;
    }

    if (newdata) gpsdump(gps);
    nss_gps.end();
    } // end of get_gps_data()

    ——————————————-

    What am I doing wrong? Should I declare an nss object for my sensor? Maybe not use the nss.end() method in the end of the get_gps_data function. I’m kind of lost here :(


  31. Larry

    16 years ago

    Very impressive library! I’m just having one weird issue: I have to use the same baud rate on the NewSoftSerial instance as I do on the hardrware UART or there seem to be sync issues.

    I explain: First, I successfully H/W 4800 read, parse and H/W 4800 serial print NMEA data from a GPS module, confirming the data by looking at it in a serial monitor app. Second, I repeat this success now using NewSoftSerial, connecting the GPS to RX pin 2 and setting 4800 baud on both NewSoftSerial pin 2 and H/W UART. Third, all I change is baud to 9600 on UART and now there is a problem. The board generates correct output for a second or two, but then the data generated gets choppy and corrupted and finally ceases. It’s like it’s having trouble synchronizing the 4800 baud on pin 2 and the 9600 on the H/W. Though I know the two are really uncorrelated.

    Can you help? Does this have to do with trying to use two serial interfaces at once? Any suggested corrective action?

    Thank you!


  32. Mikal

    16 years ago

    @pantelis —

    In most cases you would never call nss.end(). There shouldn’t be any incompatibility between TinyGPS and NewSoftSerial. You should probably only call nss.begin() once at the beginning of your program.

    Mikal


  33. Wolfgang

    16 years ago

    High Mikal,

    Many Txs for the NSS!
    Unfortunately I cannot receive proper datas from my 9DOF Razor board (8Mhz) sent by the standard serial to my 2009Board (16Mhz), no matter which boud rate. The razor reports properly via the 3.3V FTDI to the serial screen on the computer. Sending datas from the razor to the 2009board (receiving with nss) and resending via standard serial to the computer does not work. If I send 1 digit with one serial.print from the razor it is ok, two digits it still receives the first one only. If I do it with two serial prints (one digit each), I can put it through to the computer.- Strange
    May I kindly ask to help?- many thanks in advance,
    b.r.
    Wolfgang


  34. pantelis

    16 years ago

    Right, after running some more tests, also using a nss object for the ping, I realised that the lib was working fine. It was actually the ping that was closing obstacles in a shallow angle and couldn’t detect them, unless it was close enough so the echo was able to reflect and return to the ping in order to take a measure.


  35. jab

    16 years ago

    Thanks for what might be the most useful Arduino library ever! But.. :)
    Recently I have started using some Teensy2.0 boards, and I have a problem with NSSv10c and receive when using high pin numbers. I have a test controlling four Sony camera modules. Cam1 on arduino pin 14&15, cam2 16&17, cam3 18&19 and cam4 on pin 20&21. Cam1 works fine both send and receive. But cam2,3&4 will only transmit and does not receive incoming data (read() == -1). I have scoped the pins and conformed there is data on all tx&rx pins, but only the NNS instance using pin 14&15 receives data. I have also made sure only one NSS instance will send and receive before moving one to the next one as stated in the using multible NSS examples.


  36. Mikal

    16 years ago

    @Wolfgang: is NSS used only for receiving on the Arduino? Or are you transmitting too? Transmitting with NSS can cause received bytes to be lost, but I wouldn’t expect transmitting with Serial to affect NSS reception. Hm…

    Mikal


  37. Wolfgang

    16 years ago

    High Mikal,

    Yes, it triggers the datas to be sent, thereafter 25 mills delay, as faster I can´t receive anything. If I want to send -179 from the razor with hardware serial to the 2009board receiving with nss, what would be the best code at what max speed?

    many thanks in advance for your help!

    b.r.
    Wolfgang


  38. Mikal

    16 years ago

    Wolfgang, I can’t immediately figure out why that doesn’t work. Perhaps if you shared your code?

    Mikal


  39. Mikal

    16 years ago

    @jab,

    Thanks for that most useful data. I haven’t played with a teensy 2.0, so I haven’t been able to verify anything, but it seems like good data. If you connect only one camera (and one NSS object to support it) to, say, pins 20 and 21, does that work in isolation? (I’m trying to figure out whether the pin mappings are wrong for high pins, or whether there is a problem with multiple objects.)

    Thanks!

    Mikal


  40. Wolfgang

    16 years ago

    Dear Mikal,

    this send the datas from the razor at 38400 baud, rolli is type int and is defined in main page. If connected to the comp directly, datas are received in perfect order.

    void printdata(void)
    {
    rolli= int(ToDeg(roll)+180);
    if (Serial.available()) {
    Serial.read();
    Serial.print ((rolli));
    }
    }

    This receives the data from the razor and send to serial window of Arduino 0018:

    #include
    NewSoftSerial Gyro(4,5);
    byte roll=0;

    void setup() {
    Gyro.begin(38400);
    delay(100);
    Gyro.flush();
    Serial.begin(57600);
    delay(100);
    }

    void loop() {

    Gyro.print(1);
    delay(30);
    if (Gyro.available()) {
    delay(25);
    Serial.println(Gyro.read(),BYTE);
    }

    Gyro.flush();
    Serial.flush();
    }

    If I add more Serial.print(Gyro.read(),BYTE), I receive more bytes (amount how many digits are sent), which are not correct ASCII characters. If there are no more bytes, I receive -1.

    many thanks again,
    b.r.
    Wolfgang


  41. jab

    16 years ago

    @Mikal: It is most likely a pin mapping/interrupt related problem. I can swap pins on any of the four NSS instances controlling cameras, and the one using pins 14&15 start working both ways. While the others will only transmit.


  42. jab

    16 years ago

    @Mikal: Sorry, forgot to answer you specific question about a single NSS on high pins. I left the project at work, so I will test that first thing tomorrow morning.


  43. jab

    16 years ago

    @Mikal: I have just confirmed that a single NSS object on pin 20&21 does not work with the Teensy 2.0, so the problem is most likely not related to using multiple objects.


  44. jab

    16 years ago

    @Mikal: After some more testing, it would seem that NSS receive on the Teensy 2.0 will only work if PORTB (PCINT) pins are used as RX pins.


  45. JLux on Arduino

    16 years ago

    But supposing I need send to this command to Arduino:

    Print #4 , Chr(&H80) ; Chr(&Ha4) ; Chr(&H30) ; Chr(&H02) ; Chr(&H09);

    (this command is a command to be send to a fingerprintsensor)

    How can I accomplish that task with NewSoftSerial ??

    JLux


  46. Mikal

    16 years ago

    See email JLux…

    Mikal


  47. aka ged

    16 years ago

    Hi Mikal,
    I really like the idea of what you’re doing with NewSoftSerial, but
    I’m having trouble compiling it into a sketch for my Serial graphicl LCD 128×64.
    Using Arduino-0018 with a duemillanove 328p.
    OpSys:Win2000 Pro SP4. Have the used run.bat to sort the java PATH.
    The NewSoftserial directory is in my sketch folder.
    Getting these compile errors:-

    27: error: NewSoftSerial.h: No such file or directory In function ‘void setup()’:
    In function ‘void loop()’:
    Bad error line: -2

    Now, I have been a programmer in days gone by, is the any other way to direct the compiler to non-standard libraries ? Or, am I missing something really basic?? :-(

    tia aka ged

3 Trackbacks For This Post
  1. VDIP1 USB Host Controller « Arduino Fun

    […] chose the NewSoftSerial library to give access to the VDIP1.  The first attempt was to use the AFSoftLibrary and it just […]

  2. Project Lab

    […] software running on the Arduino ATMEGA328 chip utilizes the wonderfully robust NewSoftSerial library for communicating with the EM-406a GPS module and the very convenient TinyGPS library for […]

  3. Box Round 2 « Stromberg Labs

    […] is available here. I borrowed from a couple of people’s Arduino libraries to get this done, notably NewSoftSerial from Arduiniana and the GPS Parsing code from the Arduino website for parsing the NMEA strings. […]

Leave a Reply