Go Down

Topic: Arduino Mega Connection with GSM shield (Read 1 time) previous topic - next topic


Apr 17, 2018, 08:40 am Last Edit: Apr 17, 2018, 01:37 pm by Yiannis_And
I am running the code below to sent an SMS to a GSM Shield and control a Relay or get some values from some sensors. The code is running with no problems with the UNO but I cannot run it with a MEGA. The connections I am using with the MEGA are: D0 at Shield to RX1 (19) to Mega D1 at Shield to TX1 (18) to Mega Ground connection Between them

The GSM shiled is (SIM900 V2.2 Wireless Module GSM GPRS Shield Arduino (with SIM card holder) M91) bought in ebay. It is powered up with 2A power with no issues (It works with the UNO with no problems)

The Library that I am using is the ITEADLIB_Arduino_SIMCom-master

I am testing the hardware serial connection between the two boards with a simple sketch and the communicate properly (AT replies OK), but when I run my sketch they seem not to communicate. Can you please give me any input on that? I have made the changes needed for the Mega in the Libraries (GSM.h file to define Mega instead of UNO and HWSerial.h to define Mega).
What Am I missing please?

[#include <SIM900.h>
#include <sms.h>

int ThermistorPin = 5;
int Vo;
float R1 = 10000; //resistor 10K
float logR2, R2, T1, Tc;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

int rel1 = 10; // Pin 10 for relay 1
int ledrel1 = 4; // Pin 12 for relay 1 led
int ledgsm = 6; // Pin 6 for GSM status LED

int humidity_sensor1_pin = A0;
byte humidity_sensor1_vcc = 13;

int sensor1value;
boolean started=false;
char smsbuffer[160];
char Sender[20];

void setup()
 pinMode(rel1, OUTPUT);   // set pin 10 OUTPUT.
 pinMode(ledrel1, OUTPUT);   // set pin 4 OUTPUT.
 digitalWrite(rel1, HIGH); // initialise the rel
 digitalWrite(ledrel1, LOW); // set Relay 1 LED off
 pinMode(ledgsm, OUTPUT);   // set pin 6 OUTPUT for GSM led.
 pinMode(humidity_sensor1_vcc, OUTPUT);  // Initialize the humidity sensor board
 digitalWrite(humidity_sensor1_vcc, LOW);

 // Initiallise GSM connection by setting the baud rate, To use HTTP 4800 or less should be used
 if ( gsm.begin(2400) )
    Serial.println("STATUS of GSM Module = IDLE");
    digitalWrite(ledgsm, HIGH); //set GSM LED ON
    Serial.println("STATUS of GSM Module = INACTIVE");
} //end of void setup
 int read_humidity_sensor1() {
    digitalWrite(humidity_sensor1_vcc, HIGH);
    int value1 = analogRead(humidity_sensor1_pin);
    digitalWrite(humidity_sensor1_vcc, LOW);
    return value1;
  float read_temp(){
      Vo = analogRead(ThermistorPin);
      R2 = R1 * (1023.0 / (float)Vo - 1.0);
      logR2 = log(R2);
      T1 = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
      Tc = T1 - 273.15;
      return Tc;
void loop()
 char inSerial[50]; //for GSM
 char position;     //for SMS
 char relon [3]= "ON";
 char reloff [5]= "OFF";

   read_temp(); //read the thermistor
    Serial.print("Thermistor temperature: ");
    Serial.println(" C");

 if (started)  {
    position = sms.IsSMSPresent(SMS_ALL); // Check SMS in sim from 1..20
   if (position)
     // Read SMS and determine the sender
     sms.GetSMS(position, Sender, smsbuffer, 160);

     if ((strcmp(Sender,"+306932239171")==0)    //change the number to authorise to open
         || (strcmp(Sender,"+306908500553")==0))
     Serial.print("Command Received [tel. "+String(Sender)+String("]: ") + String(smsbuffer));
     if (strcmp(smsbuffer,"P1-ON")==0){
        digitalWrite(rel1, LOW);
        digitalWrite(ledrel1, HIGH); // set Relay 1 LED ON
        Serial.println(" => Pump 1 is ON");

     else if (strcmp(smsbuffer,"P1-OFF")==0){
      digitalWrite(rel1, HIGH);
      digitalWrite(ledrel1, LOW); // set Relay 1 LED OFF
      Serial.println(" => Pump 1 is OFF");
     else if (strcmp(smsbuffer,"STATUS")==0)
       int r1=digitalRead(10);
       char str[12] ={'\0'};
       char str1[20] ={'\0'};
       char str2[35] ={'\0'};
       char str4[5] ={'\0'};
       char str5[5] ={'\0'}; //array for temp
     if (r1==1)
         sprintf(str4,"%s ",reloff);
         sprintf(str4,"%s ",relon);  
     read_temp(); //read the thermistor
     sprintf(str, "Sensor1: %d", sensor1value);
     sprintf(str1, "%s, Relay is: %s",str, str4);
     dtostrf(Tc, 3, 1, str5);
     sprintf(str2, "%s, Temp now: %s",str1, str5);
       (sms.SendSMS("+306932239171", str2));
       Serial.println("\nThe reply SMS for the humidity sensors and Relay statuses was sent to the number");    
      Serial.println(" => The sender number was NOT Recognized!!!!!");
    sms.DeleteSMS(position); // Delete the SMS that was just analysed
   }  //if started
 }  //void loop]


Using an Uno shield with Serial on a Mega is a bad idea. Most probably the library expects to use one of the additional Serial ports of the Mega. I see 3 possible solutions to your problem:
- use an Uno
- change the library to use Serial even on a Mega
- change the shield to use the Serial port expected by the library


Thank you for the reply, I am really new on that and by looking on line I can see that there should be a possibility to run the  GSM shield with the Mega. I will try to "read" and understand the library but give a hint! Which one? Is there any library that can do that?

I really need to use a mega to expand my project, do you recommend any shield that can do that?

How can I change the shield to use the serial port expected by the library?

Thank you in advance for any help!


If the shield uses TX and RX (D0 and D1), then cut these pins and add wires to a different Serial port, as expected by the library.
In the library code look for a #define using a specific Serial, i.e. one of Serial1 to Serial3.


The shield is connected with the Mega with just 3 wires (Rx,TX and Ground), it is not plugged into the mega. Just to understand, the GSM library in some file defines the Rx and TX pins that the GSM board is using or the pins that the Mega uses? 


How can I change the shield to use the serial port expected by the library?
DON"T CUT ANY PINS before you look and understand exactly why you are doing it. I think it is fair to say that, in the world of Arduino shields, the only pins that get cut are by those that haven't, and don't, and will probably regret doing it later. Similarly, the only pins that get cut with good reason are on shields that are badly designed, usually by you, and should never have been there in the first place.

Most GSM shields are essentially the same design and offer a choice of serial connection. If you don't have the data sheet or a wiki, see if you can see a cluster of say six pins, probably clearly marked, with a couple of jumpers on them. In that case the central pair is the serial connection, and the jumpers determine where it goes. This arrangement will also offer the setup you need by removing both jumper blocks, and then running jumper wires from the central pair to Serial1 on Mega.

Further to this nonsense, using a Uno shield on a Mega is normal, reasonable, and commonplace. This to the point where I bet you won't find a GSM shield for a Mega, and the main reason for that is that nobody has been motivated to make one and, if there were more GSM shields on Megas than on Unos, it wouldn't surprise me - even if they started out on Unos in the first place.

I'm also betting you don't have to touch the library either, you only have to adjust your own code, i.e. the shield just wants serial data, and doesn't care where it comes from.


Apr 19, 2018, 02:51 am Last Edit: Apr 19, 2018, 03:02 am by Nick_Pyner
Looking at your first post ( at last) I see you have used the GSM on a Uno with no problems. In that case, I believe it should run an a Mega with no change to code or pins. I recognise that you have edited the library for a Mega, I have no idea why you need to do that - if indeed you did need to. I'm sure I never had to do that myself, or had any suggestion that I might need to.  I developed on a Mega for use on a Uno so, if there was a need for this malarkey, I guess it would have been apparent.

I think there is something else going on that is causing a problem, perhaps wiring.

The suggestion to use a Uno is plainly silly
Using another serial port is not a bad idea but has no bearing on your need to expand your project, perhaps quite the opposite. If you never had any intention yourself of changing the serial port, don't bother doing it. I believe it's just a red herring, and you might not need to change anything.

Picture shows a typical serial jumper, mine is more or less the same.


Why do you expect that the shield can work without power?


It is powered up with 2A power with no issues (It works with the UNO with no problems)
The shield is connected with the Mega with just 3 wires (Rx,TX and Ground),
I assume you know what you are doing here. The ground wire is the bit people forget.


Code: [Select]
Hi guys, my shield has exactly the jumper pinout that you have posted, so the jumpers are in the parts of D0 and D1. This works with the UNo, so I assume that this Serial Rx/TX from the GSM Shield are remaining the same. I have run a simple code on the Mega with the shield connected to the Mega as the pinout quoted above Shield D0 to Tx1(18) to Mega, D1 to Rx1(19) and Ground between them, 2A power straight on the shield, power on the Mega via my usb port, I have set the baud rate in the shield as auto and it runs with no issues. I have not included any library on the simple sketch. The problem starts when I run my sketch and include the gsm and sms libraries. So somewhere there is the issue that I cannot find! I am really new in programming and I cannot follow (understand) the libraries very well.
The library I am using is the ITEADLIB_Arduino_SIMCom-master.
If you have any sugestions, you are welcome!

[String outMessage = "Hello world!";
String destinationNumber = "+306932239171";

void setup()
  delay(20000); // give time to log on to network.
  Serial1.println("AT + CMGS = \"" + destinationNumber +"\"");
  Serial1.write((char)26); //ctrl+z

void loop()
  if (Serial1.available())              // if date is comming from softwareserial port ==> data is comming from gprs shield
  if (Serial.available())                // if data is available on hardwareserial port ==> data is comming from PC or notebook



I don't know what that code is, and I think it might be better if you stuck to the point.

Not being able to understand libraries very well should not be a problem. The whole purpose of libraries is that they are there to be used, not "understood", and the solution for any library that has to be understood is to get rid of it and use one that doesn't.

Since you have been fiddling with the libraries, I suggest you replace them with new ones that are virgo intacto. 

Since you have no problems using a Uno, and since I believe all this stuff about making special provision for a Mega is nonsense, and hoping you can get over your insistence in using Serial1, perhaps you can humour us by simply plugging the shield on top of the Mega as intended by the designer, and using the Uno code. If there really IS a problem with using a Mega, now is the time when you will find that out, although I would not accept it at first failure.

If you have joy, then try using Serial1 using jumpers as described.

I believe my libraries are from Telefonica, not Itead. That doesn't mean they are different. I guess they just came from Github. I'm not going to trawl all of them, but I see no reference to Mega and, since the  serial source is human selectable, there is no reason to expect any.

The only comment in the hardware notes about Mega is that the selectable software serial, pins D7,D8 do not work on a Mega. Mercifully, they make no comment about why anybody would want to use software serial on a Mega.


Hi Nick, thank you for the comments, unfortunatelly my shield does not have pins to be connected on top of the Mega. Is it possible to try to run my code with your libraries?


unfortunatelly my shield does not have pins to be connected on top of the Mega.
I find that hard to believe. You call it a shield, pins is what shields have. So what have you got? Please don't tell me you have clipped them already.... You can probably put new headers in.

I'm not sure this is all complete as I haven't used it for a long time.


The GSM shiled is (SIM900 V2.2 Wireless Module GSM GPRS Shield Arduino (with SIM card holder) M91), cannot be hooked on top of the Mega.

I tried the examples from the library smssender or the smsreceiver but no luck


Apr 20, 2018, 02:41 am Last Edit: Apr 20, 2018, 03:37 am by Nick_Pyner
The GSM shiled is (SIM900 V2.2 Wireless Module GSM GPRS Shield Arduino (with SIM card holder) M91), cannot be hooked on top of the Mega.
This is nonsense. It's is by definition a shield. If it fits on the Uno, it will fit on a Mega. If it doesn't, it can only be because it has been damaged, or you have been listening to DrDietrich, and it won't fit on a Uno either. But I'm not sure this matters and it certainly isn't worth quibbling about because I have have just realised that hardly any pins are used anyway. They are nearly all pass through.  ON MY DEVICE the only used pins are:

D0, D1     Hardware serial
D7, D8     Software serial - Uno only
D9           Software control of power up (after you have broken a bridge one the board!)

So indeed, if you use Mega's Serial1 with flying leads, the only pin you need is ground, and I'm sure you have that under control already.  Further, as so few pins are used, any rectification required is pretty painless, as the other pins are just there for location, stability, and pass through.
I tried the examples from the library smssender or the smsreceiver but no luck
I can't really comment. I don't really know what you are doing but, if you use my library, only use the examples that came with it. I recognise that, as far as Arduino is concerned, all it is doing is sending and receiving serial data, but there is no point in risking a mis-match until you know what you are doing.

Go Up