0

Recently I bought a SIM900A module and an Arduino Mega 2560 to run my old code for sending SMS and call, but now there is a problem as I am unable to send SMS or call. My code is attached below.

#include <SoftwareSerial.h>

SoftwareSerial gsm(2, 3);

void setup() {
  gsm.begin(9600); // Setting the baud rate of GSM Module
  Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);
}

void loop() {
  if (Serial.available() > 0)
    switch (Serial.read()) {
      case 's':
        Send();
        break;
      case 'r':
        Receive();
        break;
      case 'S':
        Send();
        break;
      case 'R':
        Receive();
        break;
    }
  if (gsm.available() > 0)
    Serial.write(gsm.read());
}

void Send() {
  Serial.print("Sending");
  gsm.println("AT+CMGF=1");
  delay(1000);
  gsm.println("AT+CMGS=\"+xxxxxxxxxxxx\"\r"); // Replace x with mobile number
  delay(1000);
  gsm.println("Hello I am GSM modem!!!");// The SMS text you want to send
  delay(100);
  gsm.println((char)26); // ASCII code of CTRL+Z
  delay(1000);
}

void Receive() {
  Serial.print("Receiving");
  gsm.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
}
svtag
  • 184
  • 4
  • 12
Vicky
  • 1
  • I have checked my mega and sim900a with some other codes and they are working fine. – Vicky Sep 11 '17 at 12:37
  • You need to learn to indent -- and press ctrl-T in the IDE to properly format your code! – dda Sep 11 '17 at 13:07
  • 1
    You don't need to, and shouldn't, use SoftwareSerial on pins 2/3 on a Mega. That's `Serial1`. – dda Sep 11 '17 at 13:11
  • You mean (2,3) will not work in software serial ?? – Vicky Sep 12 '17 at 06:08
  • I bought the mega from w11stop, is there any problem with my arduino mega? – Vicky Sep 12 '17 at 06:10
  • I just changed the software serial pins from (2,3) to (10,11) and my code runs successfully. thank you @dda – Vicky Sep 12 '17 at 06:20
  • You did it the wrong way. Leave the pins on 2/3 and instead of using SoftwareSerial, use Serial1... – dda Sep 12 '17 at 06:21
  • I used the software serial and its working fine now, the only thing i was doing wrong was that I was using pin 2 and pin 3 for software serial in arduino mega just like I was using it in arduino uno but the fact is UNO and MEGA have different specs. After reading the specs of arduino mega http://www.w11stop.com/arduino-mega-2560 I understood that mega does not support change interrupts on pin 2 and 3 For details read mega description in the given link. – Vicky Sep 25 '17 at 14:30
  • I meant that you could keep using pins 2 and 3 by using Serial1 instead of SoftwareSerial... – dda Sep 25 '17 at 14:40

0 Answers0