I have a complex requirement that uses a number of libraries that depend on timing I.e. OneWire. When using the official GSM shield on a mega, it loads a version of software serial to talk to the GSM. I want to make it use a spare hardware serial port on the MEGA for more timing stability. How should I modify the GSM library to (optionally) auto detect a MEGA and then use a nominated serial port. I am happy to jumper the correct pins from the mega serial hardware port to the bent outwards pins 2 and 3 of the GSM shield ( pin 2 is already bent out if you use a MEGA anyway).

  • #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) ........
    – jippie
    Apr 19, 2014 at 18:00
  • Thanks this will work but I need the help to change the libraries from using the customized soft serial code and allow me to select a hardware serial pin...this is the real issue.
    – ShanevanJ
    Apr 19, 2014 at 18:32
  • AltSoftSerial might help you in this case... Apr 20, 2014 at 22:46
  • Do you absolutely need to use serial0?
    – TheDoctor
    May 6, 2014 at 13:17
  • 1
    I don't think AltSoftSerial is going to help as the goal seems to be to use one of the additional available hardware serial ports. Presumably going through the library and changing the soft serial references to the alternate hardware serial object would do it (or putting the selection inside ifdef's as jippie proposed). Aug 8, 2014 at 19:39

1 Answer 1


Before we start, note that it might not be the right pinout for the GSM shield to use a hardware serial port on the Mega. Beware of some nasty wiring.

Poking around on Github, I found this is the directory for the GSM library. Ladies and gentlemen, it's time to break something important get our hands dirty! Draw your chairs close for a tale of touching code you shouldn't touch!

Click on the src folder to open it up. When I first opened this up, I was somewhat scared by the number of files. However, I had a plan:

  1. Find the name of the SoftwareSerial instance. I'm guessing that they would use only one object to do this to prevent redundancy. In addition, I'm going to guess that most serial data is handles though a single file, but I'll have to confirm this.
  2. Remove instances of the SoftwareSerial Object.
  3. Clean up any remaining issues with the "port"

After more poking around, I noticed that the library actually uses a unique version of the SoftwareSerial library, so it will be even harder to do this. Yay us! On the upside, we know exactly what code to replace. Fasten your seatbelts, this is about to get pretty nasty.

Okay, take a step back here and figure out what we want to modify. First of all, it's a terrible idea to modify the libraries directly in the directory that the Arduino IDE uses.

So, if you were to make a copy from the src folder for the GSM library into the Arduino "libraries" folder, it wouldn't be enough. The Arduino IDE would get confused when there would be two identical header files to use. You will need to rename the main header file and use just that one instead to include into your projects. In addition, most of the files in the library probably reference that file, so make sure to rename any references within the library to that one file. A quick CTRL + SHIFT + F on the Atom text editor will search the entire project I'm working on. I'm not entirely sure if a folder name would mess up the Arduino IDE, but it'd be a good idea to rename the folder to whatever your library is called, at least for readability purposes.

Before we continue, let's make sure that the library still compiles without us playing with the code. Import the new library into a basic GSM example sketch and see if it compiles and works normally.

It compiles? Great. Let's go on!

The file that does serial is the GSM3SoftSerial.cpp file. Without actually porting it, I can't give you a ton of advice here. Look through and see when variables are created, used, and modified. If you're not sure, I'm in chat sometimes: feel free to ask there on minor questions and ping me with @AnnonomusPenguin or leave a comment on this post.

I'd also advise to leave everything in there that you're not sure of, make sure it works with hardware serial, and line by line start removing that variable and see if anything breaks. A great too for this would be to use a tool like Git/Github (or other version control software) to help you revert changes that broke something or to see, overall, what you've changed.

So, after replacing the code inside the functions (finalWrite, the ISRs, and the begin/clear seem like the ones you should touch), compile the code and see if it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.