SD.begin() freeze my code

Please i need your help, i am developing tracking system, which i want to store NMEA sentences to my SD card, and upload it to my server using GSM module.

My code is freezing if i put the SD card code and GSM module code together, but if i separate SD card code and run it separately everything is working fine. What i observed it stop execution in SD.begin();

I need your help please.

Thanks

wiring and setup() please

Am using Arduino uno

The wiring are:

MOSI-> 11
MISO-> 12
SCK-> 13
CS-10

main.cpp file code

#include <Arduino.h>
#include<TinyGPS.h>
#include<SoftwareSerial.h>
#include <Adafruit_FONA.h>
#include “sd_card.h”

#define FONA_RX A5
#define FONA_TX A4
#define FONA_RST A3

#define GPS_TX 8
#define GPS_RX 9
#define D_STATUS 4

String deviceID =“199DHH34”;

char http_cmd[128];
boolean gprs_on = false;
boolean tcp_on = false;
int net_status;

uint16_t statuscode;
int16_t length;
String response = “”;
char buffer[512];

SoftwareSerial SIM800ss = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA SIM800 = Adafruit_FONA(&SIM800ss, FONA_RST);

//SoftwareSerial mGsm(2 ,3);
SoftwareSerial mGps(GPS_RX, GPS_TX);

String mDEVICE_= “AX-88FUEJ90-JE”;
float tmpSpd,tmpLat,tmpLng=0;
#define MAX_DISTANCE_COVERED 0

float flon,flat,spd,dstBtw;
boolean newdata=false;
boolean isStart;

TinyGPS mGps_lib;
unsigned long age, date, time, chars = 0;

void blink_status()
{
digitalWrite(D_STATUS,HIGH);
delay(1000);
digitalWrite(D_STATUS,LOW);
delay(1000);
}

void __status()
{
digitalWrite(D_STATUS,LOW);
delay(500);
digitalWrite(D_STATUS,HIGH);

}

static bool feedgps(long timeOut)
{
unsigned long timeout = millis();
mGps.listen();
while(!mGps.available())
if (millis() - timeout > timeOut)
{
Serial.println(“Gps read time out”);
return false;
}

mGps.listen();
while (mGps.available())
{
char rx=mGps.read();
// Serial.print(rx);
if (mGps_lib.encode(rx))
{
blink_status();
return true ;
}
}
__status();
return false;
}

void printSerialData()
{
unsigned long timeout = millis();
while(!SIM800ss.available())
if (millis() - timeout > 50000)return;

String cnt="";
while(SIM800ss.available())
{
char rx=SIM800ss.read();
cnt=cnt + rx;
Serial.write(rx);

}

}

void sync_data_to_server(String latitude,String longitude,String spd)
{
SIM800ss.listen();
SIM800ss.println(“ATE0”);
delay(3000);

SIM800ss.println(“AT+CGATT=1”);
printSerialData();
delay(3000);

SIM800ss.println(“AT+SAPBR=3,1,“CONTYPE”,“GPRS””);//setting the SAPBR,connection type is GPRS
printSerialData();
delay(5000);

SIM800ss.println(“AT+SAPBR=3,1,“APN”,”"");//setting the APN,2nd parameter empty works for all networks
printSerialData();

delay(3000);
SIM800ss.println(“AT+SAPBR=1,1”);
printSerialData();
delay(1000);

SIM800ss.println(“AT+HTTPINIT”); //init the HTTP request
printSerialData();

delay(10000);
String url=“AT+HTTPPARA=“URL”,“http://kapichet-edu.com/track/index.php?tractorid="+deviceID+"&latitude="+latitude+"&longitude="+longitude+"&speed="+spd+"””;
SIM800ss.println(url);// setting the httppara,
Serial.println(url ) ;
//the second parameter is the website from where you want to access data
printSerialData();

delay(3000);
SIM800ss.println();
SIM800ss.println(“AT+HTTPACTION=0”);//submit the GET request
printSerialData();

delay(10000);
SIM800ss.println(“AT+HTTPREAD”);// read the data from the website you access
printSerialData();

delay(2000);
SIM800ss.println(“AT+HTTPTERM”);// terminate HTTP service
printSerialData();

}

void no_gps_network()
{ int latitude=1; int longitude=0; int spd=0;
SIM800ss.listen();
SIM800ss.println(“ATE0”);
delay(3000);

SIM800ss.println(“AT+CGATT=1”);
printSerialData();
delay(3000);

SIM800ss.println(“AT+SAPBR=3,1,“CONTYPE”,“GPRS””);//setting the SAPBR,connection type is GPRS
printSerialData();
delay(5000);

SIM800ss.println(“AT+SAPBR=3,1,“APN”,”"");//setting the APN,2nd parameter empty works for all networks
printSerialData();

delay(3000);
SIM800ss.println(“AT+SAPBR=1,1”);
printSerialData();
delay(1000);

SIM800ss.println(“AT+HTTPINIT”); //init the HTTP request
printSerialData();

delay(10000);
String url=“AT+HTTPPARA=“URL”,“http://kapichet-edu.com/track/no_gps_network.php?tractorid="+deviceID+"&latitude="+latitude+"&longitude="+longitude+"&speed="+spd+"””;
SIM800ss.println(url);// setting the httppara,
Serial.println(url) ;
//the second parameter is the website from where you want to access data
//printSerialData();
printSerialData();

delay(3000);
SIM800ss.println();
SIM800ss.println(“AT+HTTPACTION=0”);//submit the GET request
printSerialData();

delay(10000);
SIM800ss.println(“AT+HTTPREAD”);// read the data from the website you access
printSerialData();

delay(2000);
SIM800ss.println(“AT+HTTPTERM”);// terminate HTTP service

printSerialData();

}

float latitude, longitude;
uint16_t res_code,max_len=64;

sd_cardcon sd_cardcon(true); // Constructor

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mGps.begin(9600);
pinMode(D_STATUS,OUTPUT);

if (! SIM800.begin(4800)) {
Serial.println(“Couldn’t find SIM800L”);
while (1);
}

Serial.println(“SIM800L is OK”);
delay(1000);

Serial.println(“Registering to home network!”);
net_status = SIM800.getNetworkStatus();
while(net_status != 1){
net_status = SIM800.getNetworkStatus();
delay(2000);
}

char ccid;
Serial.println(“Registered to home network!”);
//if(SIM800.get(&ccid))
// Serial.println(ccid);

delay(2000);

Serial.println(“Initiating…”);

}
String jas=“12346799 gas”;
void loop() {

sd_cardcon.getstring(jas);
// put your main code here, to run repeatedly:
tmpLat=flat;
tmpLng=flon;
dstBtw=0;

unsigned long start = millis();
String str="";

while (millis() - start < 2000)if (feedgps(2000))newdata = true;

mGps_lib.f_get_position(&flat, &flon, &age);
spd=mGps_lib.f_speed_kmph();

if((flat==TinyGPS::GPS_INVALID_F_ANGLE)|| (flon==TinyGPS::GPS_INVALID_F_ANGLE) ||(spd==TinyGPS::GPS_INVALID_F_SPEED)){

no_gps_network();

}
else
{
// if(tmpLat!=0 && tmpLng!=0) dstBtw=TinyGPS::distance_between(flat, flon, tmpLat, tmpLng);

// str="{“lat”:""+ String (flat,6)+"",“lng”:""+ String (flon,6)+"", “speed”:""+String(spd,2)+" kmph"}";
// Serial.println(str);
// Serial.print("Distance: "+ String(dstBtw));

//if((dstBtw>=MAX_DISTANCE_COVERED)|| (isStart==true))
//{
sync_data_to_server(String (flat,7),String (flon,7),String (spd,4));
tmpLat=flat;
tmpLng=flon;
//nas_sdcard.sd_card_send_data(spd);

// isStart=false;
//}

//newdata=false;

}

}

I don't see SD begin in your sketch.
the SD library default CS pins is 4 (for the Ethernet shield). you have something else on pin 4.
use SD.begin(10) if you wired CS to pin 10

Am sorry

my header file code sd_card.h:

#ifndef sdrd
#define sdrd

#if (ARDUINO >=100)
#include “Arduino.h”
#else

#include “WProgram.h”

#endif

class sd_cardcon
{
private:
/* data /
public:
// sd_card(/
args */);
sd_cardcon(bool letsee=false);

//Method
void getstring(String datasend);

};

//extern sd_cardcon sd_card;

#endif

my sd_card.cpp

#include <SD.h>
#include <SPI.h>
#include “sd_card.h”
File sdcard_file;
int CS_pin = 10;

sd_cardcon::sd_cardcon(bool letsee){

}

void sd_cardcon::getstring(String dataincome){
pinMode(CS_pin, OUTPUT);//declaring CS pin as output pin
if (SD.begin())
{
Serial.println(“SD card is initialized and it is ready to use”);
} else
{
Serial.println(“SD card is not initialized”);
return;
}

sdcard_file = SD.open(“data.txt”, FILE_WRITE); //Looking for the data.txt in SD card

if (sdcard_file) { //If the file is found
Serial.println(“Writing to file is under process”);
sdcard_file.println(dataincome); //Writing to file
sdcard_file.close(); //Closing the file
Serial.println(“Done”);
}
else {
Serial.println(“Failed to open the file”);
}

}

Juraj:
the SD library default CS pins is 4

Not as far as I can see, looking through the SD library source. It explicitly says it’s SS which is 10.

Then the SD card examples all explicitly use 4 (presumably to accommodate the Ethernet shield using 10 for itself and giving 4 to the SD), and they wouldn’t need to do that if it was already 4 in the source.

@neiklot thank you for correcting me

@Soja call SD.begin() in setup()

Juraj:
@neiklot thank you for correcting me

I think I read the source right, right? :wink:

Juraj:
@neiklot thank you for correcting me

@Soja call SD.begin() in setup()

Ok, let me try it.

Thanks

Juraj:
@neiklot thank you for correcting me

@Soja call SD.begin() in setup()

Is still freezing…

This is what is displaying to me nothing else.

Executing task: C:\Users\user.platformio\penv\Scripts\platformio.exe device monitor <

— Miniterm on COM17 9600,8,N,1 —
— Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H —

when I had this problem the sketch I was running was too big for the UNO. it ran out of memory and got twitchy when I added SD. it had nothing top do with SsD, it just happened when I tried to add it.

Soja, please read How to use this forum

pay particular attention to 7. If you are posting code or error messages, use "code" tags

I am new here, that is why.

Thank you very much.

what memory usage prints IDE after compiling the sketch?

You know am using Arduino Uno which is

Flash 32k bytes
SRAM 2k bytes
EEPROM 1k byte

Juraj:
what memory usage prints IDE after compiling the sketch?

Juraj means what does the IDE say after compiling?

This info here:

neiklot:
Juraj means what does the IDE say after compiling?

This info here:

Ok, am using Visual Studio Code

am coming...