Reading from SD Card then Reading from GSM Module not working

I have written a sketch that is supposed to load some default values from a database in an SD Card then proceed to read incoming messages from a GSM Module. I have tested the Database sketch separately and it works brilliant and the message sketch also works brilliantly. However when I combine and read from the SD Card then try to read from the GSM, it hangs on the line trying to attempt to read from the GSM. Any help please?
Here is my entire code,

#include <Sim800l.h>
#include <SoftwareSerial.h>
#include <EDB.h>

#include <SPI.h>
#include <SD.h>

#define SD_PIN 4
#define TABLE_SIZE 3686793216
Sim800l Sim800l; 
String text;     // to storage the text of the sms
int index = 1;   // to indicate the message to read.
String phone_no;

//database initialization
char* sys_var = "/db/sys_var.db";
File dbFile;

struct logEventgetsysvar{
  int id;
  char username[11];
  char password[5];
  float liquid_price;
  bool delivered;

void writer(unsigned long address, byte data){
  digitalWrite(13, HIGH);;
  digitalWrite(13, LOW);
byte reader(unsigned long address){
  digitalWrite(13, HIGH);;
  byte b =;
  digitalWrite(13, LOW);
  return b;
EDB db(&writer, &reader);

void setup(){
Serial.begin(9600); // only for debug the results .

get_sys_var();  //  Initialization of System Variable Database and read of liquid price from the SD Card

Sim800l.begin(); // initialize the library.
Serial.println("Initialization Complete");

void loop(){
  Serial.println("Starting to Read");

  text=Sim800l.readSms(index);   //Code hangs here and does not read the SMS.
  Serial.println("Read Message");
} //end of loop

void get_sys_var()
    if (!SD.begin(SD_PIN)){
    Serial.println("No SD-card.");
  if (!SD.exists("/db")){
    Serial.println("Error: System Variable Database Does not Exist...");

  if (SD.exists(sys_var)){
    dbFile =, FILE_WRITE);

    if (!dbFile){
      Serial.println("Error: System Variable Database Does not Exist...");
    if (dbFile){
      Serial.print("Opening current table... ");
      EDB_Status result =;
      if (result == EDB_OK){
    }else {
        Serial.println("Did not find database in the file " + String(sys_var));
    }else {
            Serial.println("Could not open file " + String(sys_var));
    }else {
      Serial.println("Error: System Variable Database Does not Exist...");
//   Read function of liquid price

Serial.println("Reading Liquid Price: ");

EDB_Status result = db.readRec(1, EDB_REC logEventgetsysvar);
if (result == EDB_OK)
    memcpy(session_username, logEventgetsysvar.username, 11);
    memcpy(session_password, logEventgetsysvar.password, 5);
    session_liquid_price = logEventgetsysvar.liquid_price;     
Serial.print("Username: ");

Serial.print("Password: ");
Serial.print("Liquid Price: ");

In the void setup I read from the SD Card using the function


. And then in the void loop I try to read from the GSM Module. My arduino Mega hangs here.

Please help.

My arduino Mega hangs here.

You have 4 hardware serial ports. You are not using three of them. What possible reason is there for using SoftwareSerial while ignoring the three hardware serial ports?

Sim800l Sim800l;

It is STUPID to name the instance the same as the class.

Where is the link to that library, so we can see which pins it uses?

I changed the name of instance, thank you.

The GSM library I'm using is this one: Redirecting to new site. It uses SoftwareSerial and is configured for pins 10,11 as default RX,TX but I changed this to 12,13.

Thanks in advance

It uses SoftwareSerial and is configured for pins 10,11 as default RX,TX but I changed this to 12,13.

I'd do several things:

  1. look for a different library that let you control the instance that the classes uses to read from the device, so you can use SoftwareSerial or a hardware serial instance

  2. try some different pins (A8 and A9 maybe).

  3. add some debug prints to the library, to see exactly what causes the problem.