Go Down

Topic: sd card stops working periodically (Read 286 times) previous topic - next topic

phsensor

Mar 18, 2018, 05:31 pm Last Edit: Mar 18, 2018, 06:07 pm by phsensor
I am switching a pump periodically by a relay with the help of a RTC ds1307 module  using a sd card module as a datalogger.
When I upload the code it works fine, but after sometime it stops writing to it.
I also have a lcd attached to it showing the time and status of sd card(writing to it or not).
The thing I have noticed is that when the pump is switched by the relay, sd card cant be accesed by the arduino. And sometines the time also jumps ahead 10 minutes.
To solve it I have to
 reupload the code,
or open the serial monitor port ,
 or switch off the power of arduino and then switch it on back again.

Doing any of the above steps corrects the time and arduino starts writing to the sd card.
Where lies the problem?

el_supremo

The problem is in your code.
Which you didn't post.
Post it in code tags.
Read How to post code properly.

Pete
Don't send me technical questions via Private Message.

GolamMostafa

Also, what type of LCD is connected? Parallel or I2C? 16x2 or 20x4?

phsensor

I am using parallel lcd
Code: [Select]

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

#include <SD.h>
/*** MOSI - pin 51
 ** MISO - pin 50
 ** CLK - pin 52
 ** CS - pin 53*/
 
long int c;

const int chipSelect = 53;

#include <SimpleDHT.h>
SimpleDHT11 dht;
int pin=A1;
int ph_pin=A2;
int buf[10],temp;
float avg=0;



int relay_1=7;
int relay_2=9;
int relay_3=8;
int p;


int sd;
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int chk;




void setup() {
  Serial.begin(9600);
  pinMode(53, OUTPUT);
  pinMode(relay_1, OUTPUT);
  pinMode(relay_2, OUTPUT);
  pinMode(relay_3, OUTPUT);
    pinMode(pin,INPUT);
     pinMode(ph_pin,INPUT);
    if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
return;
  }
  Serial.println("card initialized.");

lcd.begin(16, 2);
     digitalWrite(relay_1,HIGH);
    digitalWrite(relay_2,HIGH);
    digitalWrite(relay_3,HIGH);

}

void loop() {
  tmElements_t tm;
String dataString = "";
  if (RTC.read(tm)) {
   
    c=tm.Hour;
    dataString += String(c);
    dataString += ":";
    c=tm.Minute;
    dataString += String(c);
    dataString += ":";
    c=tm.Second;
    dataString += String(c);
    dataString += String("_");
    c=tm.Day;
    dataString += String(c);
    dataString += String("/");
    c=tm.Month;
    dataString += String(c);
    dataString += String("/");
    c=tmYearToCalendar(tm.Year);
    dataString += String(c);
 
   
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }

 
 
 
 
 
 
  float bi,bii;
    for(int i=0;i<10;i++)
 {
  buf[i]=analogRead(ph_pin);
  delay(10);
 }
 for(int i=0;i<9;i++)
 {
  for(int j=i+1;j<10;j++)
  {
   if(buf[i]>buf[j])
   {
    temp=buf[i];
    buf[i]=buf[j];
    buf[j]=temp;
   }
  }
 }
 avg=0;
    for(int i=2;i<8;i++)
 avg+=buf[i];
    bi=avg*5/1024/6;
    bii=18.57*bi-31.2;
   dataString += String("\n Reading pH____");
   dataString += String(bii);
   
   
   
   if ((tm.Minute==00)||(tm.Minute==10)||(tm.Minute==20)||(tm.Minute==30)||(tm.Minute==40)||(tm.Minute==50))
    {Serial.println("pump1");
      digitalWrite(relay_1,LOW);
      p=1;}
    else
    {
    Serial.println("pump0");
    digitalWrite(relay_1,HIGH);
    p=0;}
   
   if((tm.Hour==5)||(tm.Hour==6)||(tm.Hour==7)||(tm.Hour==8)||(tm.Hour==9)||(tm.Hour==10)||(tm.Hour==11)||(tm.Hour==12)||(tm.Hour==13)||(tm.Hour==14)||(tm.Hour==15)||(tm.Hour==16)||(tm.Hour==17)||(tm.Hour==18))
    {digitalWrite(relay_2,LOW);
    Serial.println("lght1");}
    else
    {digitalWrite(relay_2,HIGH);
    Serial.println("light0"); }

if((tm.Hour%3==0)&&(tm.Minute==30))
    {digitalWrite(relay_3,LOW);
    Serial.println("fan1");}
    else
    {digitalWrite(relay_3,HIGH);
    Serial.println("fan0");
    }
lcd.setCursor(0,0);
 
  lcd.print(tm.Hour);
  lcd.print(":");
  lcd.print(tm.Minute);
 
 
 
 lcd.setCursor(0,1);
 
  byte temp;
    byte hum;   
    dht.read(pin,&temp,&hum,NULL);
    dataString += String("\n Reading DHT____");
   dataString += String("Humidity=");
   dataString += String(hum);
   dataString += String("_Temperature=");
   dataString += String(temp);


lcd.print("H");
  lcd.print(hum);
  lcd.print("T");
  lcd.print(temp);
  lcd.print("pH");
  lcd.print(bii);


lcd.setCursor(10,0);
lcd.print("p");
  lcd.print(p);

 
   
   chk=0;
   
   
   
   File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:


 if(tm.Second%5==0)
  {
   
 
  if (dataFile)
  {
    sd=1;
   
    if(tm.Minute%3==0)
    {
   
    dataFile.println("dataString=");
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println("\n Printing to sd card ");
    sd=2;
    }
     
   
  } 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  sd=0;
  }
 
    }

   Serial.println(dataString);
 
  delay(1000);



lcd.setCursor(5,0);
   lcd.print(" sd");
    lcd.print(sd);
     Serial.println("\n sd=");
   Serial.println(sd);
 
   }





el_supremo

Quote
after sometime it stops writing to it
That is a common symptom of running out of memory when you use the String class.
Get rid of String and use C null-terminated character strings.

Pete
Don't send me technical questions via Private Message.

phsensor

Thanks for your reply.
Can you pleae show me how to use C null terminated string?
Do you want me to create strings like charc[100]; and add data to the end of it using strcat(a,b)?
The sd card usually stops when the pump is switched by the relay.

GolamMostafa

#6
Mar 19, 2018, 08:31 am Last Edit: Mar 19, 2018, 08:32 am by GolamMostafa
Quote
Can you pleae show me how to use C null terminated string?
OK! Please, follow these steps carefully to learn 'character string' manipulation. We will use few C functions and SD Card for this purpose.

1.  Connect the SD Card only with the UNO as per following diagram.

Fig-1:

2.  Open IDE and create/copy-paste the following source program:
This program writes the text GolamMostafa in a character array named char src[]; and also onto SD card. The program also stores AUST in the array named char saveRead[];. The program reads the string from SD card and appends it at the end of the string of saveRead[] using strcat(d, s); function.
Code: [Select]
#include<SD.h>
#include<SPI.h>

//C function prototype: char *strcat(char *dest, const char *src);
char src[] = "GolamMostafa";   //null charcater is automatically inserted by Compiler
char  saveRead[50] = "AUST";//data will be saved here that will be read from SD card
char x[50];
int i=0;

File myData;              //File pointer for the file to be opened in SD Card
const int chipSelect = 10; //CS-pin of SD Card is connected to DPin-10 of UNO

void setup()
{
  Serial.begin(9600);
  
  if (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");//SD Crad initialization.
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  //-----------------------------------------------------------------------------
  SD.remove("Testfile.txt");     //remove if any file named Testfile.txt exists
  myData = SD.open("Testfile.txt", FILE_WRITE);   //file named Testfile.txt opened for writing
  myData.println(src);     //GolamMostafa is written into SD Card
  myData.close();           //the file being pointed by pointer myData is closed
  //----------------------------------------------------------------------------
  
  myData = SD.open("Testfile.txt", FILE_READ);   //file named Testfile.txt opened for reading
  while (myData.available()!=0)
  {
    x[i] = (char)myData.read();   //x[] contains ASCII codes of characters coming from SD
    Serial.print(x[i]);              //show charctaers on SM (Serial Monitor)
    i++;
  }
  myData.close();                 //file closed
 strcat(saveRead, x);    //the content of an array can be appended at the end of another array
 
 Serial.println();
 Serial.print(saveRead);       //SM prints: ASUTGolamMostafa
}

void loop()
{
 
}


BTW:I have not seen any function in the C's string.h file which can append (concatenate) a single character at the end of a string -- a job that String supports. strcat(); function supports string concatenation as is evident from this function prototype: char *strcat(char *dest, const char *src);.

el_supremo

Quote
The sd card usually stops when the pump is switched by the relay.
OK, that can indicate a problem with the power supply. Draw a circuit diagram of how everything is wired together.

Pete
Don't send me technical questions via Private Message.

phsensor

#8
Mar 20, 2018, 03:38 pm Last Edit: Mar 20, 2018, 03:44 pm by phsensor
here is the circuit

coldrain

I do not see a flyback diode on the relay. Is there one?

Go Up