Problems communicating with SIM800L module and Arduino Mega

Hi,

I am using this sim800L module:

And a Arduino Mega 2560, I'm connecting them this way since the module's logic is 3.3v and not 5v tolerant:

And I'm just trying to connect with the module via AT commands, I saw a lot of tutorials and they use the SoftwareSerial library even if they are using the Mega with 4 UARTs and if I try it like that it works but I want to connect it in the UART pins.

This is the code I'm using:

void setup() {
  Serial.begin(9600);
  Serial2.begin(9600);
}

void loop() {
  if (Serial2.available()) 
  {
    Serial.write(Serial2.read());//Forward what Serial received to Software Serial Port
  }
  if(Serial.available()) 
  {
    Serial2.write(Serial.read());//Forward what Software Serial received to Serial Port
  }
}

But when I run it and send "AT" I read this:

Any ideas what may be wrong?

Thank you!

Please post a simple sketch using the SoftwareSerial library that makes it work as expected.

Hi, I can't make it work now with the SoftwareSerial library but I tried this code and it's working but I've also heard that the String class is not good either.

#define gsm Serial2
String Arsp, Grsp;

void setup() {
Serial.begin(9600);
Serial2.begin(9600);
}

void loop() {
if(gsm.available())
{
Grsp = gsm.readString();
Serial.println(Grsp);
}
if(Serial.available())
{
Arsp = Serial.readString();
gsm.println(Arsp);
}
}
[/code]

That's not a complete sketch.

Yes it is.

No it's not. Please copy the code from your previous reply and try to compile it.

Done. I mean it compiles and if I upload it to the Mega it communicates with the module