Welcome to EnviroDIY, a community for do-it-yourself environmental science and monitoring. EnviroDIY is part of WikiWatershed, an initiative of Stroud Water Research Center designed to help people advance knowledge and stewardship of fresh water.
New to EnviroDIY? Start here

Reply To: SDI12/Software Serial library conflicts: Arduino-based data logger

Home Forums Other Data Loggers SDI12/Software Serial library conflicts: Arduino-based data logger Reply To: SDI12/Software Serial library conflicts: Arduino-based data logger

Sara Damiano

    Yes, the SDI-12 library will conflict with SoftwareSerial, EnableInterrupt, and every single other library that defines and uses pin change interrupt vectors. This is a known issue. There are notes about it here: https://github.com/EnviroDIY/Arduino-SDI-12/issues/8. There are also lots of places around the web where you’ll find lots of suggestions for dealing with SoftwareSerial conflicts.

    Because you’re using a Mega, by far the easiest way to get around that is to not use SoftwareSerial. The Mega has 4 hardware serial ports. [Serial: 0 (RX) and 1 (TX); Serial 1: 19 (RX) and 18 (TX); Serial 2: 17 (RX) and 16 (TX); Serial 3: 15 (RX) and 14 (TX).] Unless there’s a real necessity to use software to emulate a serial port, you should always, always, always use a hardware serial port. It will be more stable, more accurate in sending and receiving data, has more bit/parity options, and won’t conflict with other libraries. This is true on *all* Arduino boards, no matter what you’re trying to communicate with. Hardware serial is better. I’ve found several instruments that just cannot handle the instability with software port emulation but will work fine on a hardware serial port. (Looking at you S::CAN…)

    If you’re already using all of the hardware serial ports on the Mega, or you’re trying to do everything on an Uno, which has only one hardware serial port (already dedicated to USB programming), the second best thing is to use another library called “AltSoftSerial.” That library is based on timer interrupts instead of pin change interrupts so it’s more stable/accurate than SoftwareSerial and will not conflict with SDI-12 or others. Unfortunately, it only works on very specific pins on each board, so you have to make sure those pins are free.

    If you still really want to use SoftwareSerial, you have to use versions of both the SoftwareSerial and SDI-12 libraries that have been modified so as not to hog the interrupt vectors. Exactly how you modify them depends on which pin you want to use for SDI-12 and for SoftwareSerial. If you’re set on pins 62/A8 and 63/A9 for software serial on the Mega, those are both on port K/PCINT2_vect. So you probably want to tell the SoftwareSerial library that it’s only allowed to control that one vector. Do do this, open the file SoftwareSerial.cpp and find and modify the section defining interrupts like this:

    Then you have to figure out which interrupt whatever pin you want to use for SDI-12 is on, and then find the same section of code in SDI-12.cpp and comment out all the interrupt vectors except whichever one controls the pin that you’re using for SDI-12. If you’re already claiming PCINT2_vect for SoftwareSerial, you will not be able to use pins A8-A15 for SDI-12 because those pins are all on PCINT2_vect. If you make those modifications, you should be able compile the libraries together.

    Just another note:

    The errors saying “Multiple libraries were found for “SD.h”” and “Multiple libraries were found for “Adafruit_Sensor.h”” have nothing to do with SoftwareSerial or SDI-12. Go thorough your library folder and delete those duplicate libraries or you might get some unexpected errors or oddly behaving code. If you’re shopping for an SD card library, I prefer SDFat.