user60840: I have an Arduino code for logging sensor data, time and date. Then I store all the data in the SD card and then I will send the sensor value to another node by using Lora. but I can't write the data in the SD card. Please help me to solve this problem. The code is also attached.
#include <RHReliableDatagram.h>
#include <RH_RF95.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>

RTC_DS1307 rtc;
File myFile;

#define node_address 12
uint8_t gateway_address = 11;
const int sensor_pin = A1;
float level;

/* Chip selection */
const int CS1 = 8;/*SD Card*/ 
const int CS2 = 10;/*LoRa*/ 

/* Configure LoRa driver */
RH_RF95 driver(10,2);
RHReliableDatagram manager(driver,node_address);

void setup () {
  pinMode(sensor_pin, INPUT);
  pinMode(CS1,OUTPUT);
  pinMode(CS2,OUTPUT);
//  SPI.begin();`
  /*--------Serial port connection setup--------*/
  Serial.begin(9600);

  /*----------RTC Initialization----------*/
  Serial.println(F("Initializing RTC...!"));
  if (! rtc.begin()) {
    Serial.println(F("Couldn't find RTC"));
  }
  if (! rtc.isrunning()) {
    Serial.println(F("RTC is NOT running!"));
//    rtc.adjust(DateTime(2019, 11, 12, 12, 32, 0)); // (Year,Month,Date,Hour,Min,Sec)
  }

  /*----------SD CARD Initialization----------*/
  digitalWrite(CS2,HIGH);
  delay(500);
  digitalWrite(CS1,LOW);
  Serial.println(F("Initializing SD card..."));
  if (!SD.begin(CS1)) {
    Serial.println(F("initialization of SD Card is failed!"));
    return;
  }
  Serial.println(F("Initialization of SD Card is done."));
  /*----------LoRa Initialization----------*/
  digitalWrite(CS1,HIGH);
  delay(500);
  digitalWrite(CS2,LOW);
  if (manager.init())
  {
    Serial.println(F("LoRa Successfully Initialised "));
  } else {
    Serial.println(F("LoRa initialization failed"));
  }
  driver.setFrequency(433.0);
  driver.setTxPower(14,false);
  Serial.println();
}

void loop () {  
  DateTime now = rtc.now();
  /*----------Date----------*/
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(',');
  /*----------Time----------*/
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  /*----------SD card----------*/
  digitalWrite(CS2,HIGH);
  delay(500);
  digitalWrite(CS1,LOW);
  delay(100);
  myFile = SD.open("test.txt", FILE_WRITE); 
  if (myFile) {
    /*----------Saving Date----------*/
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    /*----------Saving Time----------*/
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.println(',');
    level = analogRead(sensor_pin);
    myFile.print(level);
    myFile.println();
    myFile.close();
  } else {
    Serial.println("Error opening test.txt");
  }
  delay(500);
  digitalWrite(CS1,HIGH);
  delay(500);
  digitalWrite(CS2,LOW);
  char data[RH_RF95_MAX_MESSAGE_LEN];
  sprintf(data,"$%d#%d",node_address,level);
  Serial.print("Level transferred is ");
  Serial.println(data);
  Serial.println(sizeof(data));
  if(!manager.sendtoWait((uint8_t*)data,sizeof(data),gateway_address))
  {
    Serial.println("Gateway offline");
  }
  delay(500);
}


user4143: You should not manually control the SD card's CS pin.  By doing so you are confusing both the library and the SD card.
There is a special operation with SD cards in "SPI" mode that isn't part of the SPI standard as part of the initialization routine whereby the master (the Arduino) must send at least 72 clock pulses to the card with the CS pin held HIGH. 
By manually setting the CS pin LOW you are breaking that.
There is no call for you to ever do anything for the CS pin other than tell the library what it is - the library does everything else for you. So remove all references to digitalWrite(CS1, ...) from your code.

60840: <p>I have an Arduino code for logging sensor data, time and date. Then I store all the data in the SD card and then I will send the sensor value to another node by using Lora. but I can't write the data in the SD card. Please help me to solve this problem. The code is also attached.</p>

<pre><code>#include &lt;RHReliableDatagram.h&gt;
#include &lt;RH_RF95.h&gt;
#include "RTClib.h"
#include &lt;SPI.h&gt;
#include &lt;SD.h&gt;

RTC_DS1307 rtc;
File myFile;

#define node_address 12
uint8_t gateway_address = 11;
const int sensor_pin = A1;
float level;

/* Chip selection */
const int CS1 = 8;/*SD Card*/ 
const int CS2 = 10;/*LoRa*/ 

/* Configure LoRa driver */
RH_RF95 driver(10,2);
RHReliableDatagram manager(driver,node_address);

void setup () {
  pinMode(sensor_pin, INPUT);
  pinMode(CS1,OUTPUT);
  pinMode(CS2,OUTPUT);
//  SPI.begin();`
  /*--------Serial port connection setup--------*/
  Serial.begin(9600);

  /*----------RTC Initialization----------*/
  Serial.println(F("Initializing RTC...!"));
  if (! rtc.begin()) {
    Serial.println(F("Couldn't find RTC"));
  }
  if (! rtc.isrunning()) {
    Serial.println(F("RTC is NOT running!"));
//    rtc.adjust(DateTime(2019, 11, 12, 12, 32, 0)); // (Year,Month,Date,Hour,Min,Sec)
  }

  /*----------SD CARD Initialization----------*/
  digitalWrite(CS2,HIGH);
  delay(500);
  digitalWrite(CS1,LOW);
  Serial.println(F("Initializing SD card..."));
  if (!SD.begin(CS1)) {
    Serial.println(F("initialization of SD Card is failed!"));
    return;
  }
  Serial.println(F("Initialization of SD Card is done."));
  /*----------LoRa Initialization----------*/
  digitalWrite(CS1,HIGH);
  delay(500);
  digitalWrite(CS2,LOW);
  if (manager.init())
  {
    Serial.println(F("LoRa Successfully Initialised "));
  } else {
    Serial.println(F("LoRa initialization failed"));
  }
  driver.setFrequency(433.0);
  driver.setTxPower(14,false);
  Serial.println();
}

void loop () {  
  DateTime now = rtc.now();
  /*----------Date----------*/
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(',');
  /*----------Time----------*/
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  /*----------SD card----------*/
  digitalWrite(CS2,HIGH);
  delay(500);
  digitalWrite(CS1,LOW);
  delay(100);
  myFile = SD.open("test.txt", FILE_WRITE); 
  if (myFile) {
    /*----------Saving Date----------*/
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    /*----------Saving Time----------*/
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.println(',');
    level = analogRead(sensor_pin);
    myFile.print(level);
    myFile.println();
    myFile.close();
  } else {
    Serial.println("Error opening test.txt");
  }
  delay(500);
  digitalWrite(CS1,HIGH);
  delay(500);
  digitalWrite(CS2,LOW);
  char data[RH_RF95_MAX_MESSAGE_LEN];
  sprintf(data,"$%d#%d",node_address,level);
  Serial.print("Level transferred is ");
  Serial.println(data);
  Serial.println(sizeof(data));
  if(!manager.sendtoWait((uint8_t*)data,sizeof(data),gateway_address))
  {
    Serial.println("Gateway offline");
  }
  delay(500);
}
</code></pre>
 4143: <p>You should not manually control the SD card's CS pin.  By doing so you are confusing both the library and the SD card.</p>

<p>There is a special operation with SD cards in "SPI" mode that isn't part of the SPI standard as part of the initialization routine whereby the master (the Arduino) must send at least 72 clock pulses to the card <em>with the CS pin held HIGH</em>. </p>

<p>By manually setting the CS pin LOW you are breaking that.</p>

<p>There is no call for you to ever do anything for the CS pin other than tell the library what it is - the library does everything else for you. So remove all references to <code>digitalWrite(CS1, ...)</code> from your code.</p>
