Go Down

Topic: Using standard serial and NewSoftSerial togeather (Read 3745 times) previous topic - next topic


Does anyone know if I can use the Arduino standard serials pins 1 and 2, and use a NewSoftSerial connection at the same time.  

I have a Telit GSM module connected to the serial pins 1 and 2 and I have a GPS module connected to other pis that use the NewSoftSerial.  I would like to have them both operate at the same time (at differents speeds) if that is possible.  Please, can anyone advise on this?  Thank you.


Arduino standard serials pins 1 and 2

Zero and one?
Per Arduino ad Astra


Yes, sorry, zero and one.  

I get both modules working seperately but not togeather.  The gsm is working at 19200, and the GPS at 4800.  I doubt that would be a problem, but I think the problem might lie when trying toprint the date to the USB/serial, I initally get some AT commands showing up but when the GPS starts, I dont get any AT commands or text from the GPS, just a series of Square symbols.  

Could the different serial speeds be conflicting? And if so, can the serial speed of the GPS me made to match the GSM module or vice versa?


I have used them at the same time with no interaction.

Might be best to post your code and a schematic or good hw description.


Mar 18, 2010, 04:28 pm Last Edit: Mar 18, 2010, 04:29 pm by terrorhertz Reason: 1
Thanks,  I hope to get time on friday or the weekend to tidy up the code and add comments etc.


Terror, there should be no inherent difficulty in those two serial libraries coexisting at different speeds.  I use that exact model all the time.



Mar 24, 2010, 11:41 am Last Edit: Mar 24, 2010, 02:15 pm by terrorhertz Reason: 1
Thats good news.  Here is my code, I hope its inserted correctly!  Some of it is from the GPS shield and some from the GSM playground.
Code: [Select]

*  Monitor signals from Accelerometer and send SMS if movement limit is reached
*  turn on GPS module so that it has time to aquire signal.  User may then switch
*  off GPS or attempt requesting a location.
*  Comment out GPIO if not needed.  
*  after an event.
* Functions:
* checkGPS()
* getGPS()
* readline()
* turnOnGPS()
* turnOffGPS()

#include "GSM.h"    // Include GSM library
#include <NewSoftSerial.h>  // NewSoftSerial for GPS receiver

NewSoftSerial GPSSerial =  NewSoftSerial(15, 14);    //Pins 14 and 15 for GPS seria
#define powerpin 16
#define GPSRATE 4800  

// Difinitions for GPS parser for 406a
#define BUFFSIZ 90 // plenty big
char buffer[BUFFSIZ];
char *parseptr;
char buffidx;
uint8_t hour, minute, second, year, month, date;
uint32_t latitude, longitude;
uint8_t groundspeed, trackangle;
char latdir, longdir;
char status;
int check;

GSM gsm;            // Definition of instance of GSM class

#define xPin 5      // Define analog pins for ADXL330 accelerometer
#define yPin 4
#define zPin 3

int x = 0;          // Initialise current value
int y = 0;
int z  = 0;    
int xinit = 0;      // Initialise initial value
int yinit = 0;
int zinit  = 0;    
int deltax = 0;     // Initialise difference between initial and current value
int deltay = 0;
int deltaz = 0;    
int flag = 0;       // flag=0 no sms sent, flag=1, send sms

int ledpin = 13;    // for GSM Module
char string[30];    // Character array for string to send via SMS

unsigned long      previous_timer;
byte timer100msec;
int val;

char position;          
char phone_number[20];  // array for the phone number string
char *sms_text;         // pointer to the SMS text
char *ch;               // Character pointer for received SMS message

void setup()
 //For GSM module
 pinMode(ledpin, OUTPUT);
 // set direction for GPIO pisn
 // GPS Switch on from powerup
 Serial.println("GPS turned on...");
 // initialization of periodic timer
 timer100msec = 0;
 previous_timer = millis();
 // we are not registerd => disable button
 xinit = analogRead(xPin);
 yinit = analogRead(yPin);
 zinit = analogRead(zPin);

void loop()
 // -------------------
 // timing of main loop
 // -------------------

 if ((unsigned long)(millis() - previous_timer) >= 100) {
   previous_timer = millis();  

   //****** EVERY 100msec. *********
      //read current value from accelerometer
 x = analogRead(xPin);
 y = analogRead(yPin);
 z = analogRead(zPin);
 //compare that value with initial value when powered
 deltax = xinit - x;
 deltax = abs(deltax);
 deltay = yinit - y;
 deltay = abs(deltay);
 deltaz = zinit - z;
 deltaz = abs(deltaz);

 if((deltax > 15) || (deltay > 15) || (deltaz > 15))
   flag = 1;
   sprintf(string, "MOVEMENT!");
   //gsm.SendSMS("07851103223", string);
   deltax = 0;
   deltay = 0;
   deltaz = 0;
   flag = 0;
   //****** EVERY 500msec. *********
   // +1 means - 100msec. "before" a previous 100msec. action
   if ((timer100msec+1) % 5 == 0) {
     // here it is possible to place your code which will be executed
     // each 500 msec.
     // ---------------------------------------------------------
           digitalWrite(ledpin, LOW);

   //****** EVERY 1 sec. ***********
   if ((timer100msec+2) % 10 == 0) {

     // is the GSM module registered?
     // -----------------------------
     if (gsm.IsRegistered()) {

       // GSM modul is still registered
       // -----------------------------
     else {
       // not registered - so disable button
       // ----------------------------------

   //****** EVERY 3 sec. ***********
   if ((timer100msec+3) % 30 == 0) {

     // is there a new UNREAD SMS ?
     // if YES - SIM position > 0 is returned
     // -------------------------------------
     if (gsm.IsRegistered()) {
       // GSM module is registered

       // Note: if there is new SMS before IsSMSPresent() is executed
       // this SMS has a status UNREAD
       // after calling IsSMSPresent() method status of SMS
       // is automatically changed to READ

       position = gsm.IsSMSPresent(SMS_ALL);
       if (position > 0) {
         // we have new SMS
         // ---------------
         gsm.GetSMS(position, phone_number, &sms_text);

         // check SMS text
         // --------------

         // 1) e.g. text "Temp?"
         // --------------------
         ch = strstr(sms_text, "Temp?");
         if (ch != NULL) {
           // there is text Temp? => sends SMS with temperature back
           // read temperature

           // !!!Note: now we dont need the sms_text
           // !!!but it is good to know that by the following command sms_text will be overwritten
           // !!!see explanation in the library
           val = gsm.GetTemp();
           sprintf(string, "Temperature: %i C", val/10);
           gsm.SendSMS(phone_number, string);

         // 2) e.g. text "GPIO10 ON"
         // -------------------------
         ch = strstr(sms_text, "GPIO10 ON");
         if (ch != NULL) {
           // turn on the GPIO10
           // !!!Note: now we dont need the sms_text
           // !!!but it is good to know that by the following command sms_text will be overwritten
           // !!!see explanation in the library
           gsm.SetGPIOVal(GPIO10, 1);

           // and sends confirmation back
           strcpy(string, "GPIO10 turned on");
           gsm.SendSMS(phone_number, string);

         // 3) e.g. text "GPIO10 OFF"
         // -------------------------
         ch = strstr(sms_text, "GPIO10 OFF");
         if (ch != NULL) {
           // turn off the GPIO10
           // !!!Note: now we dont need the sms_text
           // !!!but it is good to know that by the following command sms_text will be overwritten
           // !!!see explanation in the library
           gsm.SetGPIOVal(GPIO10, 0);

           // and sends confirmation back
           strcpy(string, "GPIO10 turned off");
           gsm.SendSMS(phone_number, string);

         // and delete received SMS
         // -----------------------

   //********WRAP AROUN COUNTER 10 sec. *********
   timer100msec = (timer100msec + 1) % 100;

// Functions for GPS

int checkGPS()
 check = 0;
 for(int i=0; i<20; i++){
 // check if $GPRMC (global positioning fixed data)
 if ((strncmp(buffer, "$GPRMC",6) == 0) && (buffer[18] == 65))
   check = 1;
 return check;

void getGPS()
 uint32_t tmp;
 Serial.print("\n\rread: ");
   for(int i=0; i<20; i++){
      //check if $GPRMC (global positioning fixed data)
     if ((strncmp(buffer, "$GPRMC",6) == 0))
   // hhmmss time data
   parseptr = buffer+7;
   tmp = parsedecimal(parseptr);
   hour = tmp / 10000;
   minute = (tmp / 100) % 100;
   second = tmp % 100;
   parseptr = strchr(parseptr, ',') + 1;
   status = parseptr[0];
   parseptr += 2;
   // grab latitude & long data
   // latitude
   latitude = parsedecimal(parseptr);
   if (latitude != 0) {
     latitude *= 10000;
     parseptr = strchr(parseptr, '.')+1;
     latitude += parsedecimal(parseptr);
   parseptr = strchr(parseptr, ',') + 1;
   // read latitude N/S data
   if (parseptr[0] != ',') {
     latdir = parseptr[0];
   // longitude
   parseptr = strchr(parseptr, ',')+1;
   longitude = parsedecimal(parseptr);
   if (longitude != 0) {
     longitude *= 10000;
     parseptr = strchr(parseptr, '.')+1;
     longitude += parsedecimal(parseptr);
   parseptr = strchr(parseptr, ',')+1;
   // read longitude E/W data
   if (parseptr[0] != ',') {
     longdir = parseptr[0];

   // groundspeed
   parseptr = strchr(parseptr, ',')+1;
   groundspeed = parsedecimal(parseptr);

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

   // date
   parseptr = strchr(parseptr, ',')+1;
   tmp = parsedecimal(parseptr);
   date = tmp / 10000;
   month = (tmp / 100) % 100;
   year = tmp % 100;
   Serial.print("\nTime: ");
   Serial.print(hour, DEC); Serial.print(':');
   Serial.print(minute, DEC); Serial.print(':');
   Serial.println(second, DEC);


AH!!!  It didnt display properly.  Can anyone advise how to show the code in a small window!  Some of it was cut off but the important stuff is at the top.  You should be able to see how Im using the serial ports.

Both devices are talking to the microcontroller at different speeds but I think each are trying to the talk to the serial/usb at different speeds.  


Post it using bracket [ code end bracket ]

Insert your code

Then use bracket [ backslash / end bracket ] - per below, just remove the spaces.

[ code ]

Insert your code

[ /code ]


Thanks for the tip, I did that but could not fit in all the code.  It doesnt matter as the serial setup and important part is at the top.

I hope you can make sense of and see what how what Im doing.  The GSM library sets the serial speed to 19200 but GPS is 4800.  Thanks.


If you are using the hardware serial port to communicate with the GSM module, you can't use it to communicate with the Serial Monitor window, too.

Does the GSM module need to use the hardware serial port?
The art of getting good answers lies in asking good questions.


It doesnt have to use pins 0 and 1 but its hard wired that way on the shield.  I suppose I could mount the GSM shield onto a prototyping board and connect wires accordingly.

When I just use the GSM module, I can see the data via the serial monitor.  I dont know if that helps.


Good thing I did a search on this problem before posting I was having pretty much the same problem using a Telit and EM-408 at the same time with NSS.  What I found was that I needed to drop the baud rate on the Telit from 115200 down to 9600 (hardware serial) with the GPS running at 4800 (NSS).  I was finding that when I polled the GSM it was consistently dropping characters, my guess is (I'm probably wrong though) because the data is continuous (1HZ) from the GPS and frequent interrupts (more time moving the data in because the rate is slower maybe?) was causing the hardware serial to miss data.  Once I dropped the rate to 9600 all my problems went away.


@wayneft, Thats useful to know.  However, were you able to view the serial data on the Arduino monitor from the two devices at the same time? TI thought the serial monitor works at only one serial speed.

I still have not been able to get to the bottom of this:(


Just match the serial speed on the serial monitor display (bottom right hand corner pull down) to the serial speed of the GSM (in your case 19200).  I don't have any problems using serial port monitor to display data at the same time the hardware serial port is connected to the GSM.  Basically all I am doing, for now, is I issue an AT command to poll the GSM every couple of seconds or so to see if there are any unread messages (the return info is stored in a buffer) and I dump the buffer onto the screen to make sure it collected data and then I grab the GPRMC using NSS and dump that buffer onto the screen to make sure I'm receiving that data correctly.  If you want to print text out to the screen my advice is to try and be careful of your choice of words because you could inadvertently issue more AT commands to the GSM module.  Case in point I try not to print the word "DATA" because of the AT in the middle of the word.  I've seen some people automatically issue a simple "AT" after printing anything to SPM to, in essence, "reset" the communication to the GSM.  I've also used Serial.begin() after printing which seems to work well also.


Go Up