ModularSensors > Pages > FAQs > Arduino Streams and Software Serial

Arduino Streams and Software Serial

In this library, the Arduino communicates with the computer for debugging, the modem for sending data, and some sensors (like the MaxBotix MaxSonar) via instances of Arduino TTL streams. The streams can either be an instance of

Hardware Serial

For stream communication, hardware serial should always be your first choice, if your processor has enough hardware serial ports. Hardware serial ports are the most stable and have the best performance of any of the other streams. Hardware serial ports are also the only option if you need to communicate with any device that uses even or odd parity, more than one stop bit, or does not use 8 data bits. (That is, hardware serial ports are the only way to communicate with a device that doesn't use the 8N1 configuration. Again, always use a hardware serial port for communication if possible!

To use a hardware serial stream, you do not need to include any libraries or write any extra lines. You can simply write in "Serial#" where ever you need a stream. If you would like to give your hardware serial port an easy-to-remember alias, you can use code like this:

HardwareSerial* streamName = &Serial;

AltSoftSerial

If the proper pins are available, AltSoftSerial by Paul Stoffregen is also superior to SoftwareSerial, especially at slow baud rates. Neither hardware serial nor AltSoftSerial require any modifications. Because of the limited number of serial ports available on most boards, I suggest giving first priority (i.e. the first (or only) hardware serial port, "Serial") to your debugging stream going to your PC (if you intend to debug), second priority to the stream for the modem, and third priority to any sensors that require a stream for communication. See the section on Processor/Board Compatibility for more specific notes on what serial ports are available on the various supported processors.

To use AltSoftSerial:

#include <AltSoftSerial.h.
// include the AltSoftSerial library
AltSoftSerial streamName.
// Create an instance of AltSoftSerial

NeoSWSerial

Another possible serial port emulator is NeoSWSerial. While not as stable as AltSoftSerial, it supports using any pin with pin change interrupts for communication. To use NeoSWSerial, you must add the line -D NEOSWSERIAL_EXTERNAL_PCINT to the build flags section of your platformio.ini file (or open the NeoSWSerial.h file and find and remove the two slashes from the start of the line //#define NEOSWSERIAL_EXTERNAL_PCINT) and then recompile the library. There are instructions in the NeoSWSerial ReadMe on how to use EnableInterrupt to activate NeoSWSerial. Note that NeoSWSerial is generally incompatible with the SDI-12 communication library on most 8MHz processors (including the EnviroDIY Mayfly). The two libraries can be compiled together, but because they are in competition for a timer, they cannot be used together. The way this (ModularSensors) uses the SDI-12 library resets the timer settings when ending communication, so you may be able to use the two libraries together if the communication is not simultaneous. Please test your configuration before deploying it!

After correctly compiling NeoSWSerial, to use it:

#include <NeoSWSerial.h>          // for the stream communication
const int8_t neoSSerial1Rx = 11;  // data in pin
const int8_t neoSSerial1Tx = -1;  // data out pin
NeoSWSerial  neoSSerial1(neoSSerial1Rx, neoSSerial1Tx);
// To use NeoSWSerial in this library, we define a function to receive data
// This is just a short-cut for later
void neoSSerial1ISR() {
    NeoSWSerial::rxISR(*portInputRegister(digitalPinToPort(neoSSerial1Rx)));
}

After creating the stream instances, you must always remember to "begin" your stream within the main setup function.

streamName.begin(BAUD_RATE);

Additionally, for the EnviroDIY modified version of SoftwareSerial, (or NeoSWSerial) you must enable the interrupts in your setup function:

// Allow enableInterrrupt to control the interrupts for software serial
enableInterrupt(rx_pin, neoSSerial1ISR, CHANGE);

Neutered SoftwareSerial

The EnviroDIY modified version of SoftwareSerial removes direct interrupt control from the SoftwareSerial library, making it dependent on another interrupt library, but able to be compiled with ModularSensors. This is, by far, the least stable serial port option and should only be used on sensors that are not very picky about the quality of the serial stream or that only require one-way communication (ie, only posting data rather than needing to receive commands).

To use the EnviroDIY modified version of SoftwareSerial:

#include <SoftwareSerial_ExtInts.h.
// include the SoftwareSerial library
SoftwareSerial_ExtInts streamName(tx_pin, rx_pin);

After creating the stream instances, you must always remember to "begin" your stream within the main setup function.

streamName.begin(BAUD_RATE);

Additionally, for the EnviroDIY modified version of SoftwareSerial, you must enable the interrupts in your setup function:

// Allow enableInterrrupt to control the interrupts for software serial
enableInterrupt(rx_pin, SoftwareSerial_ExtInts::handle_interrupt, CHANGE);

SAMD SERCOMs

Example code for creating more serial ports on an Adafruit feather M0 using the SERCOMs is available in the menu a la carte example.

Here are some helpful links for more information about the number of serial ports available on some of the different Arduino-style boards: