Welcome to EnviroDIY, a community for do-it-yourself environmental science and monitoring. EnviroDIY is part of WikiWatershed, an initiative of Stroud Water Research Center designed to help people advance knowledge and stewardship of fresh water.
New to EnviroDIY? Start here

SD Card Recording Data Issue

Home Forums Mayfly Data Logger SD Card Recording Data Issue

Viewing 4 reply threads
  • Author
    Posts
    • #13113
      Gera
      Participant

        Hello everybody, I hope everything is well on your behalf,

        So I am currently trying to record data in an SD card for 4 environmental sensors. I succesfully manage to record the data in just 1 environmental sensor which is the DHT11 for humidity and temperature, but when I try to record it along with turbidity, pH, and conductivity, it does not work at all. I do not know if this is a memory problem exactly or it is just it can’t record data from 4 different sensors at a time, I will upload my codes below:

        The one that succesfully records data from the DHT sensor:

        #include <Arduino.h>
        #include <DHT.h>
        #include <math.h>
        #include <SD.h>
        #include <SPI.h>

        #define DHTPIN 10
        #define DHTTYPE DHT11
        #define SD_SS_PIN 12
        #define FILE_NAME “DHT.txt”
        #define LOGGERNAME “Mayfly microSD Card Tester”
        DHT dht(DHTPIN,DHTTYPE);
        int pin12 = 12;
        File SensorDHT;

        void setup()
        {
        pinMode(22,OUTPUT);
        pinMode(pin12,OUTPUT);
        digitalWrite(22,HIGH);
        delay(200);
        Serial.begin(9600);
        Serial.println(“”);
        Serial.println(“Digital Humidity/Temperature”);
        Serial.println(“”);
        dht.begin();
        if (SD.begin(SD_SS_PIN)) {
        Serial.println(“SD card activated”);
        }
        else
        {
        Serial.println(“SD card activation failed. Turning off”);
        return 0;
        }
        }

        void loop()
        {
        float h = dht.readHumidity();
        float t = dht.readTemperature();
        SensorDHT= SD.open(FILE_NAME,FILE_WRITE);

        if (isnan(t) || isnan(h))
        {
        SensorDHT.println(“Failed to read from DHT”);
        Serial.println(“Failed to read from DHT”);
        }
        else
        {
        SensorDHT.println(“Humidity: “);
        SensorDHT.print(h);
        SensorDHT.print(” %”);
        SensorDHT.println(“”);
        SensorDHT.println(“Temperature “);
        SensorDHT.print(“”);
        SensorDHT.print(t);
        SensorDHT.print(” *C”);
        SensorDHT.println(“”);
        SensorDHT.close();
        Serial.println(“Humidity: “);
        Serial.print(h);
        Serial.print(” %”);
        Serial.println(“”);
        Serial.println(“Temperature “);
        Serial.print(“”);
        Serial.print(t);
        Serial.print(” *C”);
        Serial.println(“”);

        }
        delay(1000);

        }

        Now if you ask why I have serial.print and the other print code is just because I want to see the data on the serial monitor, I think there may be better ways to do it, if you can share it would be awesome. With my current knowledge this is the only way I found to make it work.

        The code that does not work:

        #include <Arduino.h>
        #include <DHT.h>
        #include <math.h>
        #include <SD.h>
        #include <SdFat.h>
        #include <SPI.h>
        #define DHTPIN 10
        #define DHTTYPE DHT11
        #define SD_SS_PIN 12
        #define FILE_NAME “EnviroSensors.txt”
        #define LOGGERNAME “Mayfly microSD Card Tester”
        File sensorsdata;
        float voltage1;
        float voltage2;
        float turbidity;
        float Readingpin;
        float phValue;
        float pHVol;
        const int analogInPin = A0;
        int sensorValue = 0;
        unsigned long int avgValue;
        float b;
        int buf[10],temp;
        int PinSD = 12;
        int PinTurbidity = A1;
        int PinConductivity = A2;

        DHT dht(DHTPIN,DHTTYPE);

        void setup()
        {

        pinMode(22,OUTPUT);
        pinMode(PinSD,OUTPUT);
        digitalWrite(22,HIGH);
        Serial.begin(9600);
        Serial.println(“”);
        if (SD.begin(SD_SS_PIN))
        {
        Serial.println(“SD Card Reading Activated”);
        }
        else
        {
        Serial.println(“SD Card Reading Failed, turning off the program”);
        return 0;
        }
        dht.begin();
        }

        void loop()
        {
        sensorsdata = SD.open(FILE_NAME,FILE_WRITE);

        float h = dht.readHumidity();
        float t = dht.readTemperature();

        voltage1 = 0.00488*analogRead(PinTurbidity); //in V
        turbidity = -1120.4*voltage1*voltage1 + 5742.3*voltage1 – 4352.8; //in NTU
        voltage2 = (analogRead(PinConductivity))*(5.0/1023);
        for(int i=0;i<10;i++)
        {
        buf[i]=analogRead(analogInPin);
        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;
        }
        }
        }
        avgValue=0;
        for(int i=2;i<8;i++)
        {
        avgValue+=buf[i];
        pHVol=(float)avgValue*3.3/1023/6;
        phValue = -5.70 * pHVol + 19.95;
        }
        if (isnan(t) || isnan(h))
        {
        sensorsdata.println(“Failed to read from DHT”);
        }
        else
        {
        sensorsdata.println(“Humidity: Temperature: “);
        sensorsdata.println(“[” + String(h) + “] %”+” [“+String(t)+”] *C” );
        }
        sensorsdata.println(“”);
        sensorsdata.println(“Turbidity: Conductivity: “);
        sensorsdata.println(“[“+String(turbidity)+”] NTU [“+String(voltage2)+”]” );
        sensorsdata.println(“”);
        sensorsdata.println(“pH”);
        sensorsdata.println(“[“+String(phValue)+”]” );
        sensorsdata.println(“”);
        sensorsdata.close();
        delay(1000);
        }
        The code itself works to display the data in the serial monitor, but it does not works when recording it into the SD card. If you ask why am I using a strange space order in the print lines it is just to make it look pretty. One more thing, the platform I am using is platformIO with Atom. I am not using the Arduino IDE for this. And something happens with the serial monitor, it has been acting weird recently and it does not displays everything. I use the PIO monitor instead to observe the print lines. It works like it should with the PIO monitor. I hope to hear from anybody soon!

      • #13117
        Sara Damiano
        Moderator

          What do you mean by “does not work?” What do you see on the serial monitor and on you SD card’s file? When you say “The code itself works to display the data in the serial monitor, but it does not works when recording it into the SD card.” do you mean that you can see data on the Serial Port monitor do you mean that if you replace all instances of “sensorsdata” with “Serial” then the data is printed to the serial port monitor, or do you mean that when running the code as is you don’t see any error messages? There are nothing really obvious errors jumping out at me in your code.

          Please, in the future, use the “Add Code Snippet” button to post code.

          Also, I would always recommend PlatformIO over the Arduino IDE.

        • #13122
          Gera
          Participant

            I am trying to get a txt file of the data from all four sensors to the SD card. It works when I use the first code for the DHT sensor only, the data is recorded inside the SD card as a txt file and I can open it when I connect the SD card into my laptop to see the contents of it. I use this
            to record all the data from the four sensors which is the second code I uploaded here and it does not work. A txt file is not created, and no data is collected. The code itself works. I can replace the “sensorsdata” with “Serial” instead, and it does display the data of the sensors in the monitor. But the code to make the txt file and place all the data of the four sensors in the SD card does not work.

          • #13124
            Sara Damiano
            Moderator

              You’ve included both “SD.h” and “SdFat.h”. Did you try including only one of those? They might not conflict for the compiler, but they have very similar functionality and shouldn’t be included together. Your code is written for SD. SdFat is better, but for what you’re doing it shouldn’t matter.

            • #13127
              Gera
              Participant

                I removed Sdfat from it and it did not work either. Should I try with sdFat library only?

            Viewing 4 reply threads
            • You must be logged in to reply to this topic.