0

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);
}
  • let the library handle the CS pin – Juraj Nov 13 at 8:29
  • Note from the SD Library documentation: "Note that even if you don't use the hardware SS pin, it must be left as an output or the SD library won't work." SS pin for nano is 10 as far as I know. You use the pin then as a chip select for LoRa first, Probable the RoRa lib does something with it, so the SD lib got issues with it. Perhaps you try to exchage the CS pins. I haven't found any problem reported with changing the SS pin for the LoRa Lib. And: As Juraj said: why do you write to the CS1 and CS2 if the SD and LoRa libraries have control over it? – Peter Paul Kiefer Nov 13 at 9:51
  • @PeterPaulKiefer, CS pin is output. this is the most common use of the SS pin in master mode. As CS pin for the first SPI device – Juraj Nov 13 at 10:43
  • The OP uses two CS pins: one for LoRa (10) and one for SD (8). Both are set to output, that's right. But, there is a note in the SD lib doc that, even if one changes the CS/SS pin, one has to set the pin 10 to output. For me this is a hint that pin 10 still influences the SD lib, even if it is not used. I don't know the kind of influence (think of the Lora sets the CS pin 10 to input until it needs it; for what reason ever). So there may be an impact the LoRa lib has to the SD Lib. It's just an idea. – Peter Paul Kiefer Nov 13 at 10:55
  • @PeterPaulKiefer The "influence" is in hardware. The direction register for pin 10 defines whether the SPI peripheral operates in Master or Slave mode (INPUT = slave, OUTPUT = master). If you change pin 10 to INPUT then SPI just won't work. – Majenko Nov 13 at 11:02
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.