Data logging issues with multiple sensors

Sorry for my bad english before, so i'm trying to make a voltage data logger with some environment parameters. I've done the program without data logging, all parameters appear well in serial monitor.
But, i have problem when i'm trying to write all parameters to SD card. that nothing appear in serial monitor and after I check the SD card, there nothing txt file.
Data logging shiled is work well, because i've test the RTC and SD card writing example, it's work well.
The code below is the comparisson from many codes, so i guess my problem is in the program not in the hardware..
FYI: i am newbie in arduino project, so I'm still not understand well about arduino, especially in IDE program
I'm very grateful if somebody help my issues..

Here's my devices i used:

  • arduino mega rev.3
  • datalogger shield
  • sht20 i2c sensor
  • zmpt101b sensor (4pcs)
  • pms7003 sensor
  • bh1750 sensor

Here is the code with data logging:

#include <DS1307RTC.h>
#include <TimeLib.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <BH1750.h>
#include <DFRobot_SHT20.h>
#include <Plantower_PMS7003.h>
#include <Filters.h>

File myFile;
const int chihpselect = 53;

String time;
tmElements_t tm;

DFRobot_SHT20    sht20;
BH1750 lightMeter;

char output[256];
Plantower_PMS7003 pms7003 = Plantower_PMS7003();

float testFrequency = 50;                     // test signal frequency (Hz)
float windowLength = 45.0/testFrequency;     // how long to average the signal, for statistist
  int Sensor1 = 0; //Sensor analog input, here it's A0
  int Sensor2 = 1;
  int Sensor3 = 2;
  int Sensor4 = 3;
float intercept1 = -0.04; // to be adjusted based on calibration testing
float intercept2 = -0.04;
float intercept3 = -0.04;
float intercept4 = -0.04;
float slope1 = 0.0405; // to be adjusted based on calibration testing
float slope2 = 0.0405;
float slope3 = 0.0405;
float slope4 = 0.0405; 
float current_Volts1; // Voltage
float current_Volts2;
float current_Volts3;
float current_Volts4;
unsigned long printPeriod = 1000; //Refresh rate
unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);
  
  while (!Serial);  //pms7003
  delay(200);
  pms7003.init(&Serial1);
  
  Wire.begin(9600); //bh1750
  lightMeter.begin();

  sht20.initSHT20();  //sht20

  delay(2000);
}

void loop() {
  pms7003.updateFrame();
    if(pms7003.hasNewData()) {
      Serial.print(output);
      sprintf(output,"\nPM2.5: %2d ug/m3", pms7003.getPM_2_5());

  float lux = lightMeter.readLightLevel(true);

  float humd = sht20.readHumidity()- 0.3;
  float temp = sht20.readTemperature()+ 0.3;

  Serial.print("\nLight: ");
  Serial.print(lux);
  Serial.println(" lx");

  Serial.print("Temperature:");
  Serial.print(temp, 1);
  Serial.print(" C");
  Serial.println();
  Serial.print("Humidity:");
  Serial.print(humd, 1);
  Serial.print(" %");
  Serial.print("\n");

  RunningStatistics inputStats1;                //Easy life lines, actual calculation of the RMS requires a load of coding
  RunningStatistics inputStats2;
  RunningStatistics inputStats3;
  RunningStatistics inputStats4;
  inputStats1.setWindowSecs( windowLength );
  inputStats2.setWindowSecs( windowLength );
  inputStats3.setWindowSecs( windowLength );
  inputStats4.setWindowSecs( windowLength );
    Sensor1 = analogRead(A0);  // read the analog in value:
    Sensor2 = analogRead(A1);
    Sensor3 = analogRead(A0);
    Sensor4 = analogRead(A1);
      inputStats1.input(Sensor1);  // log to Stats function
      inputStats2.input(Sensor2); 
      inputStats3.input(Sensor3);
      inputStats4.input(Sensor4); 
  if((unsigned long)(millis() - previousMillis) >= printPeriod) {
      previousMillis = millis();   // update time every second     
      current_Volts1 = intercept1 + slope1 * inputStats1.sigma(); //Calibartions for offset and amplitude
      current_Volts1= current_Volts1*(50.3231);                //50.3231 Further calibrations for the amplitude
      Serial.print( "Voltage1: " );
      Serial.print( current_Volts1 ); //Calculation and Value display is done the rest is if you're using an OLED display
      Serial.print("\n");
      }{
      previousMillis = millis();   // update time every second
      current_Volts2 = intercept2 + slope2 * inputStats2.sigma(); //Calibartions for offset and amplitude
      current_Volts2= current_Volts2*(50.3231);                //50.3231 Further calibrations for the amplitude
      Serial.print( "Voltage2: " );
      Serial.print( current_Volts2 ); //Calculation and Value display is done the rest is if you're using an OLED display
      Serial.print("\n");
      }{
      previousMillis = millis();   // update time every second
      current_Volts3 = intercept3 + slope3 * inputStats3.sigma(); //Calibartions for offset and amplitude
      current_Volts3= current_Volts3*(50.3231);                //50.3231 Further calibrations for the amplitude
      Serial.print( "Voltage3: " );
      Serial.print( current_Volts3 ); //Calculation and Value display is done the rest is if you're using an OLED display
      Serial.print("\n");
      }{
      previousMillis = millis();   // update time every second
      current_Volts4 = intercept4 + slope4 * inputStats4.sigma(); //Calibartions for offset and amplitude
      current_Volts4= current_Volts4*(50.3231);                //50.3231 Further calibrations for the amplitude
      Serial.print( "Voltage4: " );
      Serial.print( current_Volts4 ); //Calculation and Value display is done the rest is if you're using an OLED display
      Serial.print("\n");
      }
      delay(3000);}
}

void ReadText(){
  // re-open the file for reading:
  myFile = SD.open("LoggerCD.txt");
  if (myFile) {
    Serial.println("LoggerCD.txt");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } 
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening LoggerCD.txt");
  }
}

String Now(){
  String time = "";
  if (RTC.read(tm)) {
    //    time = String(tm.Hour+":"+tm.Minute+":"+tm.Secnd+" DAY : "+tm.Day+"/"+tm.Month+"/"+tmYearToCalendar(tm.Year));
    time+=tm.Hour;
    time+=":";

    time+=tm.Minute;
    time+=":";

    time+=tm.Second;
    time+=" DAY : ";

    time+=tm.Day;
    time+="/";

    time+=tm.Month;
    time+="/";

    time+=tmYearToCalendar(tm.Year);
    
  } 
  else {
    time = "NO";
    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();
    }
  }
  return time;
}

void Write_SDcard() {
   // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File myFile = SD.open("LoggerCD.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (myFile) {
    myFile.print(RTC.read(tm)); //Store date on SD card
    myFile.print(","); //Move to next column using a ","

    myFile.print(current_Volts1); //Store date on SD card
    myFile.print(","); //Move to next column using a ","

    myFile.print(sht20.readTemperature()); //Store date on SD card
    myFile.print(","); //Move to next column using a ","

    myFile.print(sht20.readHumidity()); //Store date on SD card
    myFile.print(","); //Move to next column using a ","

    myFile.print(lightMeter.readLightLevel(true)); //Store date on SD card
    myFile.print(","); //Move to next column using a ","

    myFile.print(pms7003.getPM_2_5()); //Store date on SD card
    myFile.print(","); //Move to next column using a ","

    myFile.println(); //End of Row move to next row
    myFile.close(); //Close the file
  }
  else
  Serial.println("OOPS!! SD card writing failed");
}

both code with and without data logger files i'm attached below.

with_datlog.txt (7.04 KB)

without_datlog.txt (4.79 KB)

Hi and welcome to the forum,

tap on your shoulder for posting the code as a code-section.

If you are very new to Arduino-programming this project is quite challenging.
There is a very common thing that most newbees do and professionals avoid:

Writing a lot of code an then start testing. In 99% of all cases this "trying to be fast" turns out to be "slowing down"

With each line you add to your code before you start testing you multiply possible sources of errors. And it will take much longer to check through all these possabilities than adding one thing - test until it works reliable then
add one thing - test until it works reliable then

add one thing - test until it works reliable then

add one thing - test until it works reliable then ...

If you are very new to programming by this way you will learn more about programming.

You are using delay and non-blocking timing based on millis(). How should your time gets updated every second if you delay the whole programm for 3 seconds? Simply not possible.

You should avoid the command delay() completely. The function delay() ist such a DELAY in writing more complex programs than blinking an LED because delay() blocks the execution of everything.

So my recommendation is to write a small testprogram that does nothing more than write a few lines into a textfile on SD-card and then read it in again.

If you think "oh I have no idea how to do that!" this indicates that your knowledge-level is far too low for modifying code.

You can ask thousands of questions here. As long as your postings show that you put some own effort into solving you will get support here

Of course you can ask here in the forum. But then you have to wait for the answers. If you start to learn how coding works you become more and more independent of waiting for answers.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

I'm a somehow advanced programmer and this has the effect of beeing partially blind about beginners difficulties. But I would like to become an "expert" about beginners difficulties and therefore I appreciate feedback about that and to receive questions what is still hard to understand.

best regards Stefan

StefanL38:
Hi and welcome to the forum,

tap on your shoulder for posting the code as a code-section.

If you are very new to Arduino-programming this project is quite challenging.
There is a very common thing that most newbees do and professionals avoid:

Writing a lot of code an then start testing. In 99% of all cases this "trying to be fast" turns out to be "slowing down"

With each line you add to your code before you start testing you multiply possible sources of errors. And it will take much longer to check through all these possabilities than adding one thing - test until it works reliable then
add one thing - test until it works reliable then

add one thing - test until it works reliable then

add one thing - test until it works reliable then ...

If you are very new to programming by this way you will learn more about programming.

You are using delay and non-blocking timing based on millis(). How should your time gets updated every second if you delay the whole programm for 3 seconds? Simply not possible.

You should avoid the command delay() completely. The function delay() ist such a DELAY in writing more complex programs than blinking an LED because delay() blocks the execution of everything.

So my recommendation is to write a small testprogram that does nothing more than write a few lines into a textfile on SD-card and then read it in again.

If you think "oh I have no idea how to do that!" this indicates that your knowledge-level is far too low for modifying code.

You can ask thousands of questions here. As long as your postings show that you put some own effort into solving you will get support here

Of course you can ask here in the forum. But then you have to wait for the answers. If you start to learn how coding works you become more and more independent of waiting for answers.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

I'm a somehow advanced programmer and this has the effect of beeing partially blind about beginners difficulties. But I would like to become an "expert" about beginners difficulties and therefore I appreciate feedback about that and to receive questions what is still hard to understand.

best regards Stefan

You right that i'm very new in programming
ok i'll try to test one by one of the command program
and thank you for your suggestion and the course link, i'll learn from there too