Problem Interfacing GSM module with Arduino Mega 2560 using UARTS

Hey guys, I am trying to interface my SIM800 gsm module with my arduino MEGA to send or receive a text using hardware serial ports. Connected Tx of modem to Rx1 and Rx of modem to Tx1. checked my serial monitor and tried to send a text to my phone number but then it wouldnt send it. i don't want to use SoftwareSerial since I am already using a fingerprint sensor to interface that. What should I do?

here's my code:

void setup()


{


 Serial.begin(9600);    // Setting the baud rate of Serial1Monitor (Arduino)


 Serial1.begin(38400);   // Setting the baud rate of GSM Module 


 delay(100);


}








void loop()


{


 if (Serial.available()>0)


  switch(Serial.read())


 {


   case 's':


     SendMessage();


     break;


   case 'r':


     RecieveMessage();


     break;


 }





if (Serial1.available()>0)


  Serial.write(Serial1.read());


}








void SendMessage()


{


 Serial1.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode


 delay(1000);  // Delay of 1000 milli seconds or 1 second


 Serial1.println("AT+CMGS=\"+91xxxxxxxxxx\"\r"); // Replace x with mobile number


 delay(1000);


 Serial1.println("I am SMS from GSM");// The SMS text you want to send


 delay(100);


  Serial1.println((char)26);// ASCII code of CTRL+Z


 delay(1000);


}








void RecieveMessage()


{


 Serial1.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS


 delay(1000);


}

Remove the delay()s from your code and try again. Waiting 1 second is a very long time in the microcontroller world.

I'd start with a simple forwarding sketch:

void setup() {
  Serial.begin(38400);
  Serial1.begin(38400);
}

void loop() {
  if (Serial.available()) {
    Serial1.write(Serial.read());
  }
  if (Serial1.available()) {
    Serial.write(Serial1.read());
  }
}

With that you can simply send the commands more or less directly to the GSM modem and see the results, whether they are errors or OKs.

Should I remove the delays in the void SendMessage() function too?

Should I remove the delays in the void SendMessage() function too?

Yes. But try first with above sketch and typing the commands on the Serial Monitor to see if you get errors from your GSM modem.

pylon:
Yes. But try first with above sketch and typing the commands on the Serial Monitor to see if you get errors from your GSM modem.

You mean the AT commands?
I tried that but there is no response in the serial window.
Should I type the commands as
AT+CMGF=1
"AT+CMGS="+91xxxxxxxxxx"\r" ?

You mean the AT commands?

Yes.

Should I type the commands as
AT+CMGF=1
"AT+CMGS="+91xxxxxxxxxx"\r" ?

Yes, and the modem should answer with OK or something similar. Keep in mind that I chose 38400 for the baud rate to the PC too. So you have to configure your Serial Monitor to that.

pylon:
Yes.

Yes, and the modem should answer with OK or something similar. Keep in mind that I chose 38400 for the baud rate to the PC too. So you have to configure your Serial Monitor to that.

I tried it. there was no OK or error but whatever I typed appeared on the serial monitor

Seems to be a wiring problem. Please post a wiring diagram or a sharp photo with all connections visible of your setup.