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

    10 years ago

    Its not working anymore, I downloaded it and I compiled its first example and it shows problem.


  2. Faruk

    10 years ago

    Hello.first of all thank you for your all libraries.I’m using tinygps++ in my project.But i’ll ask some different question.I’m using pins 0,1 of Arduino Uno for xbee shield and it receives gps infos from other arduino.At the same time i want to use pins 8,9 of same arduino for gps module and data. Is it possible to gather datas from both device(xbee shield and gps module) in one sketch and one arduino? ( as you know 0,1 are hardware serial pins,SoftwareSerial is used for 8,9 pins)Which way can you tell?can you give some details?If you could reply and help i’d be so happy.from now thank you very much for your help.I look forward to your reply.Kind regards,Faruk


  3. Mikal

    10 years ago

    @Faruk,

    You should be able to receive on both HW and SW serial. BUT you need to make sure that your code processes the incoming stream sufficiently quickly.


  4. xxx

    10 years ago

    How I can get simply work it on Arduino release 1.6
    I all time get this error

    In file included from sketch_nov13a.ino:1:
    C:\Users\user2\Documents\Arduino\libraries\NewSoftSerial/NewSoftSerial.h:33:2: error: #error NewSoftSerial has been moved into the Arduino core as of version 1.0. Use SoftwareSerial instead.
    In file included from sketch_nov13a.ino:1:
    C:\Users\user2\Documents\Arduino\libraries\NewSoftSerial/NewSoftSerial.h:99: error: conflicting return type specified for ‘virtual void NewSoftSerial::write(uint8_t)’
    C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Print.h:48: error: overriding ‘virtual size_t Print::write(uint8_t)’


  5. Mikal

    10 years ago

    @xxx, you shouldn’t use NewSoftSerial in new versions of Arduino. As the error says, the code is now in the core.


  6. Steve Nussbaum

    10 years ago

    Hello Mikal,
    I am new to the arduino products and was hoping you could look at a small sketch I wrote that I’m having difficulty with. I’m trying to use two softserial ports and the listen() function to select between them. How can I send you this sketch as I have not used this forum before.
    Steve


  7. Steve Nussbaum

    10 years ago

    Mikal, I see my request has already posted.
    I’ve pasted it below. The sketch has two serial devices, a TBI interface that sends three bytes of data every 500ms. The bytes are a long RPM value and the third byte is a a voltage value. The idea is to read from the TBI device then read the EM-506 GPS module data. I am only trying to grab the speed info for now. Im using the “listen()” fcn to activate the TBI then I want to activate the GPS module. The TBI data is properly read only when the GPS.listen() is commented out. Same for the GPS module. I have to comment out the TBI.listen() in order for the GPS data to received and displayed. Any advice would be appreciated.

    #include

    #include
    #include
    #include

    /* This sample code demonstrates the normal use of a TinyGPS object.
    It requires the use of SoftwareSerial, and assumes that you have a
    4800-baud serial GPS device hooked up on pins 2(rx) and 3(tx).
    */
    /* This code is expanded to incorporate the LCD with the GPS sketch Nov 5, 2014 SHN */
    /* It keeps the usb interface as is to display data on serial monitor */
    /* This version works well 11/5/2014 12:42 pm shn */

    /* 11/6/2014 This code has been expanded to read the ThunderBolt Ignition serial output of RPM and Batt volts */
    /* The TBI is being simulated by the PIC Demo Plus board. It sends variable rpm and a fixed batt volts value 14.5 */
    /* The format is a 3 byte packet, (msb)RPM_HI,RPM_LO,Batt_volts */
    // #define BACKLIGHT_PIN 13
    // new code from web site
    // 11/12/2014 Added code to turn one one soft serial at a time. Reads rpm and batt volts ok with occasional scrambled
    // info.

    #define I2C_ADDR 0x27
    #define BACKLIGHT_PIN 3
    #define En_pin 2
    #define Rw_pin 1
    #define Rs_pin 0
    #define D4_pin 4
    #define D5_pin 5
    #define D6_pin 6
    #define D7_pin 7

    #define TBI_RX 4
    #define TBI_TX 5
    #define GPS_RX 2
    #define GPS_TX 3

    LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin,BACKLIGHT_PIN,POSITIVE);
    TinyGPS gps;

    SoftwareSerial TBI(TBI_RX,TBI_TX); // port for ignition module pin 4 (PD4) for RX pin 5 (PD5) for TX 11/6/2014
    SoftwareSerial GPS(GPS_RX, GPS_TX); // modified to 2,3 SHN 11/04/2014
    /* Globals */

    int RPM_HI = 0;
    int RPM_LO = 0;
    long RPM;
    float Batt_Volts = 0;
    float speed_mph ; // calculated from speed_kmph
    float speed_kmph = 30; // from gps module

    /* static void smartdelay(unsigned long ms);
    static void print_float(float val, float invalid, int len, int prec);
    static void print_int(unsigned long val, unsigned long invalid, int len);
    static void print_date(TinyGPS &gps);
    static void print_str(const char *str, int len);
    */
    void setup()
    {
    // For the GPS interface
    pinMode(GPS_RX, INPUT);
    pinMode(GPS_TX, OUTPUT);

    // For the TBI interface pin 4 = rx = input, pin5 = tx = output
    pinMode(TBI_RX, INPUT);
    pinMode(TBI_TX, OUTPUT);
    // for the LCD

    // Switch on the backlight
    pinMode ( BACKLIGHT_PIN, OUTPUT );
    digitalWrite ( BACKLIGHT_PIN, HIGH );
    lcd.begin(16,2); // initialize the lcd

    lcd.home (); // go home
    lcd.print(“Hello, ARDUINO “);
    lcd.setCursor ( 0, 1 ); // go to the next line
    lcd.print (“GPS Data Display “);
    delay ( 1000 );
    lcd.home();
    lcd.print(“RPM: | Vbat:”);
    lcd.setCursor(0,1);
    lcd.print(“MPH: | “);
    // setup done for the lcd

    // Start serial ports
    GPS.begin(4800); // start GPS port, uses listen() to enable
    TBI.begin(4800); // start up the TBI soft uart turns off the ss uart

    delay(50);

    }

    // Main Loop

    void loop()
    {
    // Read TBI data and display to terminal and lcd

    TBI.listen();

    if(TBI.overflow() == 0)
    {
    while(TBI.available()>0)
    {
    Batt_Volts = TBI.read();
    RPM_HI = TBI.read();
    RPM_LO = TBI.read();
    Batt_Volts = Batt_Volts/10; // scale it
    TBI.write(Batt_Volts); // regurgitate it, send out to tx pin 5 back to TBI simulator
    TBI.write(RPM_HI);
    TBI.write(RPM_LO);
    // speed_kmph = speed_kmph + 5;
    }
    }
    // build rpm value
    RPM = RPM_HI; // load in MSB first
    RPM = RPM << 8; // shift msb into position
    RPM = RPM + RPM_LO; // add lower byte

    lcd.setCursor(11,1);
    lcd.print(Batt_Volts);

    lcd.setCursor(4,0);
    lcd.print(RPM);

    // Retreive speed from GPS
    GPS.listen(); // if this is commented out the TBI works, if in it does not and vice versa.
    // delay(100); // not sure how much if any delay is needed
    while(GPS.available())
    {
    speed_kmph = speed_kmph + 5;
    //speed_kmph = gps.f_speed_kmph();
    int c = GPS.read();
    if(gps.encode(c)) //if encode returns a true, new data is available
    {
    speed_kmph = gps.f_speed_kmph();
    }
    }
    // delay(200);
    // speed_kmph = gps.f_speed_kmph();
    // for lcd displaying speed
    // speed_kmph = speed_kmph + 5; // for testing
    speed_mph = speed_kmph/1.6 ;
    lcd.setCursor(4,1);
    lcd.print(speed_mph);

    }


  8. Steve Nussbaum

    10 years ago

    Mikal, I see one problem with the statement “while(GPS.available();”,
    I changed it to “while(GPS.available() > 0);” but still I can only read either the data from the TBI interface or the GPS interface. I have to comment out either the TBI.listen() to read GPS stuff or comment out GPS.listen() to read data from the TBI.
    I’m a little confused over the validity of the pins I’ve selected. Using pin 2 for the GPS RX and pin 4 for the TBI RX pin. That ok?
    Thanks for your critique.
    S


  9. Mikal

    10 years ago

    Steve,

    The pin selection is ok assuming you’re using Uno.

    To read from both devices, the logic you are using should NOT be:

    void loop()
    {
      TBI.listen();
      while (TBI.available())
        ... process TBI data
      GPS.listen();
      while (GPS.available())
        ... process GPS data
    }
    

    This never will accomplish anything because there will never be anything available immediately after you start listening. I would use logic where I listened first to one device and then the other in intervals of, say, 5 seconds:

    void loop()
    {
      TBI.listen();
      for (unsigned long start=millis(); millis() - start < 5000;) // listen for 5 seconds
        if (TBI.available())
          ... process TBI data
      GPS.listen();
      for (unsigned long start=millis(); millis() - start < 5000;) // listen for 5 seconds
        if (GPS.available())
          ... process GPS data
    }
    

    Keep in mind that you begin listening to each device at a random time, so your “process XXX data” code should not make any assumptions about where in the stream the data begins flowing.


  10. Steve

    10 years ago

    Mikal, thanks so much for your guidance. Reading each input for 5 seconds worked fine. I had read your tutorial in the section preceding this forum but I did not fully understand the reasoning behind your explanation of why “this code will not work” when the examples seemed to follow that code. But I get it now. You must test for data availability for a few seconds before data is read into the buffer.
    However, I do need to update the display with RPM and speed much more frequently than at 5 second intervals. Maybe the 2 soft serial port approach is not a good design. Since I’m not using the USB port when this application runs I could use the hard uart for the GPS data and one soft port for the TBI interface.
    Thanks again for you help and great work on the libraries.
    Steve


  11. Steve

    10 years ago

    Mikal,
    I increased the packet rate from 500ms to 250ms from the TBI. Then I changed the “for” loop from 5000ms to 250 ms for both devices. I took it for a drive and everything seems to update fast enough so I will stick with the two soft serial port design for now.
    Here’s the latest version for the sketch. Just another example for folks to see how to use two ports within a program.

    Steve

    void loop()
    {
    // Read TBI data and GPS Speed data to display on lcd

    TBI.listen();
    for(unsigned long start = millis(); millis() – start 0)
    {
    Batt_Volts = TBI.read();
    Batt_Volts = Batt_Volts/10; // scale it
    lcd.setCursor(11,1);
    lcd.print(Batt_Volts);
    RPM_HI = TBI.read();
    RPM_LO = TBI.read();
    // regurge to verify input is being read
    TBI.write(Batt_Volts); // regurgitate it, send out to tx pin 5 back to TBI simulator
    TBI.write(RPM_HI);
    TBI.write(RPM_LO);
    }

    // build rpm value
    RPM = RPM_HI; // load in MSB first
    RPM = RPM < 100 && RPM < 9999)
    {
    lcd.setCursor(5,0);
    lcd.print(RPM);
    }
    }
    }

    // Retreive speed from GPS
    GPS.listen();
    for(unsigned long start=millis(); millis() – start 0)
    {
    int c = GPS.read();
    if(gps.encode(c)) //if encode returns a true, new data is available
    {
    speed_mph = gps.f_speed_mph();
    }
    }
    // speed_mph = speed_kmph/1.6 ;
    lcd.setCursor(4,1);
    lcd.print(speed_mph);
    }

    }


  12. David

    10 years ago

    Mikal,

    I’m wondering if you might be able to help me combine the NewSoftSerial library here with PWM output? I’ve been looking over the code and I’ve tried simply enabling PWM at the correct frequency on the same pin but this unfortunately does not work. Any insight would be great.

    Regards,

    David


  13. Steve

    10 years ago

    Hi Mikal,
    Now that my GPS and TBI soft ports are working I added a microSD shield to the stack. Now I have an Arduino UNO, microSD shield and the GPS shields stacked in this order. I ran the SD example from the web site without issue. After pasting this code into setup section of my GPS sketch it does not work. It seems the line “if(myFile)” always returns a false value. After narrowing things down by process of elimination I found that taking out the line “if(gps.encode(c))” which is down in the main loop, it makes the SD functions work fine. It appears like the SD library functions are not working properly if that line of code is compiled. I made sure that the “ss” pin is set as an output. I’m at a loss for what to do next.
    Any help would be greatly appreciated.
    Steve


  14. Steve

    10 years ago

    Mikal,
    I have this program running now. I needed to hold off declaring the object in the set up section and put it in the main loop as shown.
    All I/o seems to work. I am getting good files on the SD card etc. Now I am trying to save the highest MPH reached between resets. I added a new variable called max_speed. I use this to compare with the latest speed_mph from the gps code. However, adding these lines prevents the sd file from opening. I have no idea why this is happening, any enlightenment would be appreciated.
    If I comment out the “max_speed = speed_mph” in the GPS section, it works.

    #include
    #include
    #include
    #include
    #include
    /* This sample code demonstrates the normal use of a TinyGPS object.
    It requires the use of SoftwareSerial, and assumes that you have a
    4800-baud serial GPS device hooked up on pins 2(rx) and 3(tx).
    */
    /* This code is expanded to incorporate the LCD with the GPS sketch Nov 5, 2014 SHN */
    /* It keeps the usb interface as is to display data on serial monitor */
    /* This version works well 11/5/2014 12:42 pm shn */

    /* 11/6/2014 This code has been expanded to read the ThunderBolt Ignition serial output of RPM and Batt volts */
    /* The TBI is being simulated by the PIC Demo Plus board. It sends variable rpm and a fixed batt volts value 14.5 */
    /* The format is a 3 byte packet, (msb)RPM_HI,RPM_LO,Batt_volts */
    // #define BACKLIGHT_PIN 13
    // new code from web site
    // 11/16/2014 Added code to turn one one soft serial at a time. Reads rpm for 250 ms and gps for 250ms.
    // seems to update at a reasonable rate.
    //
    // This versions adds the micro SD memory card via Arduino shield from Sparkfun
    // This card is 4GB in size. 11/20/2014

    #define I2C_ADDR 0x27
    #define BACKLIGHT_PIN 3
    #define En_pin 2
    #define Rw_pin 1
    #define Rs_pin 0
    #define D4_pin 4
    #define D5_pin 5
    #define D6_pin 6
    #define D7_pin 7

    #define TBI_RX 4
    #define TBI_TX 5
    #define GPS_RX 2
    #define GPS_TX 3
    #define SD_CS 8

    LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin,BACKLIGHT_PIN,POSITIVE);

    SoftwareSerial TBI(TBI_RX,TBI_TX); // port for ignition module pin 4 (PD4) for RX pin 5 (PD5) for TX 11/6/2014
    SoftwareSerial GPS(GPS_RX, GPS_TX); // modified to 2,3 SHN 11/04/2014
    /* Globals */

    int ledPin = 7; // indicator led
    int write_flag = 0; // counts 10 passes thru readings and stores
    int RPM_HI = 0;
    int RPM_LO = 0;
    long RPM;
    float Batt_Volts = 0;
    float Batt_Volts_New = 0;
    float Batt_Volts_Old = 0;
    float Batt_Volts_Diff = 0;
    long Record = 0;

    float speed_mph ; // direct from gps module
    float max_speed;
    int year;
    byte month, day, hour, minute, second, hundredths;
    float latitude, longitude;
    unsigned long date,time,fix_age; // date:ddmmyy time:hhmmsscc

    File myFile; // set up instance

    void setup()
    {

    // For the GPS interface GPS_RX = pin 2, GPS_TX = pin 3
    pinMode(GPS_RX, INPUT);
    pinMode(GPS_TX, OUTPUT);

    // For the TBI interface pin 4 = rx = input, pin5 = tx = output
    pinMode(TBI_RX, INPUT);
    pinMode(TBI_TX, OUTPUT);

    // Serial.begin(115200);
    // Serial.print(“Initializing SD card…”);
    // for the SD card
    pinMode(10, OUTPUT); // per SD install instructions
    pinMode(8, OUTPUT);
    pinMode(7, OUTPUT); // for red led
    digitalWrite(ledPin, HIGH);
    delay(300);
    digitalWrite(ledPin, LOW);
    SD.begin(8);

    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    myFile = SD.open(“Firefly.txt”, FILE_WRITE);

    // if the file opened okay, write to it:
    if (myFile)
    {
    myFile.println(“Firefly Data Logger: “);
    // close the file:
    myFile.close();
    }
    else
    {
    myFile.println(“write error in set up”);// if the file didn’t open, print an error:
    }

    // for the LCD

    // Switch on the backlight
    pinMode ( BACKLIGHT_PIN, OUTPUT );
    digitalWrite ( BACKLIGHT_PIN, HIGH );
    lcd.begin(16,2); // initialize the lcd

    lcd.home (); // go home
    lcd.print(“Hello, ARDUINO “);
    lcd.setCursor ( 0, 1 ); // go to the next line
    lcd.print (“GPS Data Display “);
    delay ( 1000 );
    lcd.home();
    lcd.print(“RPM: | Vbat:”);
    lcd.setCursor(0,1);
    lcd.print(“MPH: | “);
    // setup done for the lcd

    // Start serial ports
    GPS.begin(4800); // start GPS port, uses listen() to enable
    TBI.begin(4800); // start up the TBI soft uart turns off the ss uart

    delay(50);

    }

    // Main Loop

    void loop()
    {
    // SD operation
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    if((write_flag >= 500) && (date != 0))
    {
    digitalWrite(ledPin, HIGH);

    myFile = SD.open(“Firefly.txt”, FILE_WRITE);
    if (myFile) // if the file opened okay, write to it:
    {
    myFile.print(“Line:”); // record number
    myFile.print(Record);
    myFile.print(” Date:”);
    myFile.print(date);
    myFile.print(” Time:”);
    myFile.print(time);
    myFile.print(” Lat:”);
    myFile.print(latitude, 5);
    myFile.print(” Lon:”);
    myFile.print(longitude, 5);
    myFile.print(” RPM: “);
    myFile.print(RPM);
    myFile.print(” “);
    myFile.print(“VBat: “);
    myFile.print(Batt_Volts);
    myFile.print(” “);
    myFile.print(“MPH: “);
    myFile.println(speed_mph);
    // close the file:
    myFile.close();
    }
    write_flag = 0; // clear flag
    Record++; // bump record #
    digitalWrite(ledPin, LOW);
    }

    // Read TBI data and GPS to display on lcd
    TBI.listen();
    for(unsigned long start = millis(); millis() – start 0)
    {
    Batt_Volts = TBI.read();
    Batt_Volts = Batt_Volts/10; // scale it
    lcd.setCursor(11,1);
    lcd.print(Batt_Volts);
    RPM_HI = TBI.read();
    RPM_LO = TBI.read();
    // regurge to verify input is being read
    TBI.write(Batt_Volts); // regurgitate it, send out to tx pin 5 back to TBI simulator
    TBI.write(RPM_HI);
    TBI.write(RPM_LO);
    }

    // Battery value filter. Needs new reading to be within 2 volts of the old to post

    Batt_Volts_Old = Batt_Volts_New; // shift data
    Batt_Volts_New = Batt_Volts;
    Batt_Volts_Diff = Batt_Volts_New – Batt_Volts_Old;
    abs(Batt_Volts_Diff);
    if(Batt_Volts_Diff < 2)
    {
    lcd.setCursor(11,1);
    lcd.print(Batt_Volts_New);
    }

    // build rpm value
    RPM = RPM_HI; // load in MSB first
    RPM = RPM < 100 && RPM < 9999)
    {
    lcd.setCursor(5,0);
    lcd.print(RPM);
    write_flag++; // increment write_flag, every 10th time write to SD
    }
    }
    }

    // Retreive speed from GPS
    TinyGPS gps; // moved from the top and it works.
    GPS.listen();

    for(unsigned long start=millis(); millis() – start 0)
    {
    int c = GPS.read();
    if(gps.encode(c)) //if encode returns a true, new data is available
    {
    speed_mph = gps.f_speed_mph();
    gps.f_get_position(&latitude, &longitude);
    gps.get_datetime(&date, &time);
    // gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &fix_age); //
    }

    }

    if(max_speed < speed_mph)
    {
    // max_speed = speed_mph; // if left uncommented it does not work the SD file will not open.
    }

    lcd.setCursor(4,1);
    lcd.print(speed_mph);
    }

    }


  15. Mikal

    10 years ago

    @Steve, is this on Uno? If so, I don’t think you can put the USB port into “host” mode to connect a GPS device. Maybe use a Mega or Teensy w/ 2 “hard” serial ports?


  16. Mikal

    10 years ago

    @Steve,

    Gee, I don’t know. Are you out of memory perhaps?


  17. Steve

    10 years ago

    Mikal, thanks for your response. This project is using a UNO. After compiling it says Ive used 24K out of 32k of memory so Im assuming Im not out of flash. Ive stayed with a soft serial for the GPS module. Also using a soft serial for the TBI interface. It’s using the I2C for the LCD display and the SPI for the microSD card. All is working unless I try to set another parameter equal to speed_mph. If I do this the SD file will not open. This is my issue now. Initially I had the statement “TinyGPS gps;” up the presetup area and I could not open a file on the SD card. I moved it down into the main loop just prior to the “gps.encode(c)” which occurs after the SD file access and it seems to function. So there appears to be some kind of interaction between the TinyGPS code and the SD code. I have no idea what is actually going on here.


  18. Abhi

    10 years ago

    Hi Steve, even i am trying to do something like what you are doing. interfacing LCD and GPS into Arduino with Edison. Getting errors on softwareserial port hooked to GPS shield.


  19. Mikal

    9 years ago

    @Abhi,

    I doubt if SoftwareSerial can be ported to Edison easily. Can you use the built-in Edison UARTs instead?


  20. John

    9 years ago

    Mikal
    I am new to Arduino and will reread your conversation with Steve many times.
    I am hopeful to build an GPS based NTP server with a YUN. I was able to get the GPS card to work on a UNO card with only a one week learning span. I then moved the GPS to the YUN using the same pins 2&3. After the third day of pounding a call to the vendor of the GPS card recommended that I move to pins 8&9 because 2&3 did not have PCINT. This worked well. I then wanted to learn where this limitation came from. I first tried to read about SoftwareSerial library — not much found, was pointed to NewSoftSerial lib. Then I found that that Lib is now included in the current development product. I am stuck again. Without reading the source, where can I read about SoftwareSerial and the limitations per processor? How does PCINT fit into NewSoftSerial? SoftwareSerial does not work the same on all pins?


  21. Mikal

    9 years ago

    @John,

    http://arduino.cc/en/Reference/softwareSerial

    is about the only “real” documentation I know about. I haven’t played with Yun, so I can’t advise, but if there are pins that don’t support PCINT, I agree that it seems like they should be mentioned on the documentation page.


  22. Jesse

    9 years ago

    Do you have a version that supports config? data, parity, stop bits

    For instance, SERIAL_8E1


  23. Arpan

    9 years ago

    Hi Mikal, thanks for developing the newSoftwareSerial; this made our tasks much simpler to handle. However, when trying to use this with ATmega32+arduino bootloder it fails as ATmega32 doesn’t has the PCINT feature.
    Is there anything in the newSoftwareSerial library that would enable me to use it in polling mode?

    I tried going back to old IDE but that doesn’t has a variant option. and old library doesn’t has functions like readString() too which is quite helpful.


  24. Jinoj

    9 years ago

    Hi,

    Can you please tell me how to enable the flow control in Arduino Uno using the NewSoftserial?
    Can you please provide the code/ library for the same?

    I need to enable it for interfacing with a Sierra HL8 modem.

    Thank you very much for the support,
    ~Jinoj


  25. Mikal

    9 years ago

    @Jesse– sorry, no. That would be a cool addition though.


  26. Mikal

    9 years ago

    @Arpan, ah, too bad. No, I’m afraid it really just doesn’t support polling, and making it do so would be pretty hard. Sorry!


  27. Mikal

    9 years ago

    @Jinoj,

    Regrets: NewSoftSerial doesn’t support flow control, although in theory it wouldn’t be too hard to add new CTS and RTS pins support. Some people have done this in the past with older versions of NewSoftSerial.


  28. Aldo

    9 years ago

    Hi, I am using Arduino MEGA2560 there are some problems about RX & TX?
    Thank you in advance.


  29. ltdat

    9 years ago

    My error
    Arduino: 1.6.4 (Windows 7), Board: “Arduino Uno”

    In file included from sketch_apr24b.ino:2:0:
    C:\Users\ThanhDat\Documents\Arduino\libraries\NewSoftSerial/NewSoftSerial.h:33:2: error: #error NewSoftSerial has been moved into the Arduino core as of version 1.0. Use SoftwareSerial instead.
    #error NewSoftSerial has been moved into the Arduino core as of version 1.0. Use SoftwareSerial instead.
    ^
    In file included from sketch_apr24b.ino:2:0:
    C:\Users\ThanhDat\Documents\Arduino\libraries\NewSoftSerial/NewSoftSerial.h:99:16: error: conflicting return type specified for ‘virtual void NewSoftSerial::write(uint8_t)’
    virtual void write(uint8_t byte);
    ^
    In file included from E:\arduino-nightly\hardware\arduino\avr\cores\arduino/Stream.h:26:0,
    from C:\Users\ThanhDat\Documents\Arduino\libraries\NewSoftSerial/NewSoftSerial.h:40,
    from sketch_apr24b.ino:2:
    E:\arduino-nightly\hardware\arduino\avr\cores\arduino/Print.h:48:20: error: overriding ‘virtual size_t Print::write(uint8_t)’
    virtual size_t write(uint8_t) = 0;
    ^
    Error compiling.

    This report would have more information with
    “Show verbose output during compilation”
    enabled in File > Preferences.


  30. Mikal

    9 years ago

    @Aldo, not all pins on the MEGA support “pin change interrupts”, therefore, not all pins can be used to receive data.


  31. Mikal

    9 years ago

    @ltdat, not sure exactly what you are saying. You shouldn’t use NewSoftSerial in Arduino 1.6.4.


  32. Michael

    9 years ago

    Mikal,
    Greetings and thanks for your work; the site looks nice and has lots of great info. Unfortunately, I was not able to use your code for my project, which is running on a Mega ADK….like Aldo, I was not getting good results, but that is not unusual, as we all tend to learn…eventually.

    To Aldo specifically — the Mega ADK has 4 hardware serial ports, which is great for those of us that have this board. They are supported by the IDE as Serial, Serial1, Serial2 and Serial3 and there is already an interrupt service routine to handle the hardware, so — SoftwareSerial and derivatives are not necessary. If you wire one of these up, you can write a handler to receive your data and move on to using that data.

    I just got my PMB-688 GPS working on Serial1 and will be adding GPS information to my Adafruit CC3000 HTTP server that is live….although still a little ‘crashy’ at this point because — well, just because….

    The Mega ADK was a terrible choice for me for Android development, but it does not share many of the limitations that other Arduino boards have — it has a lot of hardware already built in. I am replacing it with a Galileo soon, however. My intent is to use the Galileo as the HTTP server and connect boards such as the Mega ADK and my Propellers to that as individual data collection stations.

    I will try to document my efforts and results at my site — I have several RSS feeds started there and will create one for this project.


  33. Marius

    9 years ago

    I am communicating with HC-06 on 9600bps toward a BT client.
    If I send data from client to ardu (~5..10 samples/second)
    (24 byte /packet), the data arrives OK on ardu. As long
    I start sending data from ardu to the client the received
    data on ardu start getting garbage. This happen on both
    SofwareSerial / NewSoftwareSerial libraries.


  34. Oscar S.

    9 years ago

    Hi,
    I’m trying to wake up from sleep mode the arduino on received data by software serial. The processor wakes up, but the first received bytes aren’t correct. ¿The first’s bytes after wake up must be correct with software serial?

    Thanks,


  35. Witty

    9 years ago

    Hi Mikal
    Thank you for the support you are providing.
    I have one problem,i have my final year project using GPS module, Emic 2 TTS and Arduino Uno. The problem is that i have managed to use the serial ports by interchanging them but it seems the GPS cant read a new data, it only reapeat the first data read. Please am Very much confused,dont know what to do.Please Help


  36. Mikal

    9 years ago

    @Oscar,

    Yeah, I’m afraid that won’t work with software serial. I’m guessing that initial transition wakes the processor, but does not trigger the pin change interrupt until the second transition.


  37. Mikal

    9 years ago

    @Witty, are you sure the GPS is repeating data? That seems hard to explain.


  38. michael

    7 years ago

    Hi, I am looking a best solution for the two module gps and gprs shield I also used Dpin(7,8 and 3,4) of arduino uno. Then 7 and 8 pin connected to the gprs while the 3 and 4 pin connected to gps. The two module is working well but I couldn’t combine the two module in one program. I already used the softwareserial library while in the code I am using listen() but its nothing work. Now, I’ve try your newsoftserial but they give me an error

    C:\Users\C\Documents\Arduino\libraries\NewSoftSerial\NewSoftSerial.cpp:43:24: fatal error: WConstants.h: No such file or directory

    #include “WConstants.h”

    can you share your idea about this.
    any help?


  39. unique nigam

    7 years ago

    check out this for Software Uart for AVR microcontroller tested on 1200 baud rate
    http://groomix.info/programming/Software-Uart-for-AVR-microcontroller-tested-on-1200-baud-rate/


  40. Frantisek

    7 years ago

    Hi, please is possible modify NewSoftSerial to send two inverting serial signal Tx+ Tx- on two digital pins at the same time?

7 Trackbacks For This Post
  1. Подключение GPS L30 модуля используя GPS Шилд от SparkFun » Arduino Market

    […] NewSoftSerial […]

  2. Tema 5 – Proyectos Arduino | Aprendiendo Arduino

    […] NewSoftwareSerial: http://arduiniana.org/libraries/newsoftserial/ […]

  3. Twitter Poem Box -Arduino for Projects

    […] Download the TrueRandom http://code.google.com/p/tinkerit/wiki/TrueRandom, NewSoftSerial http://arduiniana.org/libraries/newsoftserial/, and Twitter […]

  4. 아두이노의 통신 방법, 핀 정리 (Serial, UART, Software Serial, SPI, I2C) | Hard Copy Arduino

    […] NewSoftSerial (Arduino IDE 1.0 이후 버전만 지원) – Serial 모듈별로 인스턴스를 생성해서 여러개를 사용할 수 있지만 한번에 하나의 인스턴스만 전송/수신 할 수 있습니다. 다른 라이브러리와의 충돌 가능성도 약간 있는 듯 합니다. http://arduiniana.org/libraries/newsoftserial/ […]

  5. Please wait your turn! Stratoballoon GPS Sensor Sketch « Mark Gilbert's Blog

    […] to the GPS receiver, I’d be writing to the data logger serially.  I found information here about running multiple devices serially – the short answer is that you have to access the serial […]

  6. RFID cat door using Arduino -Use Arduino for Projects

    […] This project consists of several ‘modules’ that you need to hook up to the Arduino and test in advance. First hook-up the RF reader. You can use the 5v output of the Arduino to power it, and a digital port (I used 2) to get the signal. The RDM630 that I used also has pins for a led that I don’t use. It also has an RX pin to send info back to the RF reader, but I don’t use that either. Hook-up your antenna, get a tag and use the serial monitor of the Arduino to see if it’s detected. Now you can also start working on improving the antenna by trying adding or removing turns, trying different shapes et cetera. Power the Adruino with the 9v power supply, not just USB because at least in my case that didn’t work. You can download the file named ‘rfid3.pde’ to test. The code requires NewSoftSerial.h which can be obtained here […]

  7. Android-Arduino Comms: Wireless Integration for Smart Control

    […] this project (bluetooth_chat_LCD.pde attached below) – NewSoftSerial library from Mikal Hart: http://arduiniana.org/libraries/newsoftserial/ – Eclipse – Android Development Kit (explicitly follow all of Google’s installation […]

Leave a Reply