Cannot write PMS7003 to SD card [SOLVED]

I want to make a data logger with multiple sensors, but i have problem when try to write PMS7003 reading result to SD card.
When i'm tried to combine the pms7003 code to datalogging code, the value of pms7003 in sd card always 0.

this is pms7003 reading code:

#include "PMS.h"

PMS pms(Serial1);
PMS::DATA data;

void setup(){
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop(){
  if (pms.read(data)){
    Serial.print("PM 2.5 (ug/m3): ");
    Serial.println(data.PM_AE_UG_2_5);
    Serial.println();
  }
}

and this is data logging code after i'm combined:

#include <DFRobot_SHT20.h>
#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC
#include "PMS.h"

File myFile;
const int chipSelect = 10;

RTC_DS1307 RTC;

DFRobot_SHT20    sht20;

PMS pms(Serial1);
PMS::DATA data;

void setup() {
  Serial.begin(9600);
  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);
  sht20.checkSHT20();                                 // Check SHT20 Sensor
  
  Serial1.begin(9600);
    
  while(!Serial); 
  Serial.println("ArduinoAll DataLogger Shield Test");
  pinMode(SS, OUTPUT);
  
  if(!SD.begin(53,51,50,52)) {
    Serial.println("initialization failed!");
    return;
      // setup for the SD card
    }
    Serial.print("Initializing SD card...");
    RTC.begin();
      if(! RTC.isrunning()) {
      Serial.println("RTC is NOT running!");
       RTC.adjust(DateTime(__DATE__, __TIME__));
    }
      Serial.println("initialization done.");
    
  myFile=SD.open("DATA1.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("File opened ok");
    // print the headings for our data
    myFile.println("Date,Time,Temperature,Humidity,PM2.5,");
  }
  myFile.close();
}

void loggingTime() {
  DateTime now = RTC.now();
  myFile = SD.open("DATA1.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print(",");
  }
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.day(), DEC);
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  myFile.close();
  }

void loggingSensors() {
  float humd = sht20.readHumidity();                // Read Humidity
  float temp = sht20.readTemperature();               // Read Temperature
  pms.read(data);

  myFile=SD.open("DATA1.txt", FILE_WRITE);
  if (myFile){
    myFile.print(temp);
    myFile.print(",");
    myFile.print(humd);
    myFile.print(",");
    myFile.print(data.PM_AE_UG_2_5);
    myFile.println(",");
    myFile.close();
  }
}
void loop() {
  loggingTime();
  loggingSensors();
  delay(5000);
}

I suggest that i'm wrong at loggingSensors part.
The SD card result screenshot is attached below

I note that the first sketch checks whether data is available before using it but the second one does not

What data type is data.PM_AE_UG_2_5 ?

UKHeliBob:
I note that the first sketch checks whether data is available before using it but the second one does not

What data type is data.PM_AE_UG_2_5 ?

data.PM_AE_UG_2_5 is uint16_t value, i got this from PMS.h library

here is the pms library code:

#ifndef PMS_H
#define PMS_H

#include "Stream.h"

class PMS
{
public:
  static const uint16_t SINGLE_RESPONSE_TIME = 1000;
  static const uint16_t TOTAL_RESPONSE_TIME = 1000 * 10;
  static const uint16_t STEADY_RESPONSE_TIME = 1000 * 30;

  static const uint16_t BAUD_RATE = 9600;

  struct DATA {
    // Standard Particles, CF=1
    uint16_t PM_SP_UG_1_0;
    uint16_t PM_SP_UG_2_5;
    uint16_t PM_SP_UG_10_0;

    // Atmospheric environment
    uint16_t PM_AE_UG_1_0;
    uint16_t PM_AE_UG_2_5;
    uint16_t PM_AE_UG_10_0;
  };

  PMS(Stream&);
  void sleep();
  void wakeUp();
  void activeMode();
  void passiveMode();

  void requestRead();
  bool read(DATA& data);
  bool readUntil(DATA& data, uint16_t timeout = SINGLE_RESPONSE_TIME);

private:
  enum STATUS { STATUS_WAITING, STATUS_OK };
  enum MODE { MODE_ACTIVE, MODE_PASSIVE };

  uint8_t _payload[12];
  Stream* _stream;
  DATA* _data;
  STATUS _status;
  MODE _mode = MODE_ACTIVE;

  uint8_t _index = 0;
  uint16_t _frameLen;
  uint16_t _checksum;
  uint16_t _calculatedChecksum;

  void loop();
};

#endif

I'm sorry if my answer doesn't match of your question. It's first time for me making project with arduino for my school task.

Did you understand what I meant about checking whether data was available before reading it ?

Your first sketch only prints if

if (pms.read(data))

is true

Your second sketch blindly assumes that data is available before reading it

Add the saving of the pms data to SD to the first sketch and initially save nothing else. When you know that works expand it to read and save other data

Yes, i'm understand
thanks a lot and i was made change for the datalogging code as your suggestion. Now it's work but i have a little problem again..
Now the PMS7005 value is appear, but I suggest that the value appear in random time. Because it should appear in every 5 second (i put delay 5 sec in the last void loop) like the picture i was attached before.
If you please, can you see what's wrong again with my code?

Here's my new code:

#include <DFRobot_SHT20.h>
#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC
#include "PMS.h"

File myFile;
const int chipSelect = 10;

RTC_DS1307 RTC;

DFRobot_SHT20    sht20;

PMS pms(Serial1);
PMS::DATA data;

void setup() {
  Serial.begin(9600);
  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);
  sht20.checkSHT20();                                 // Check SHT20 Sensor
  
  Serial1.begin(9600);
    
  while(!Serial); 
  Serial.println("ArduinoAll DataLogger Shield Test");
  pinMode(SS, OUTPUT);
  
  if(!SD.begin(53,51,50,52)) {
    Serial.println("initialization failed!");
    return;
      // setup for the SD card
    }
    Serial.print("Initializing SD card...");
    RTC.begin();
      if(! RTC.isrunning()) {
      Serial.println("RTC is NOT running!");
       RTC.adjust(DateTime(__DATE__, __TIME__));
    }
      Serial.println("initialization done.");
    
  myFile=SD.open("DATA1.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("File opened ok");
    // print the headings for our data
    myFile.println("Date,Time,Temperature,Humidity,PM2.5,");
  }
  myFile.close();
}

void loggingTime() {
  DateTime now = RTC.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.day(), DEC);
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  myFile.close();
  }
  
void loggingSensors() {
  float humd = sht20.readHumidity();                // Read Humidity
  float temp = sht20.readTemperature();               // Read Temperature
  DateTime now = RTC.now();
  if (pms.read(data)){
      myFile=SD.open("DATA1.txt", FILE_WRITE);
      if(myFile){
      myFile.print(now.year(), DEC);
      myFile.print('/');
      myFile.print(now.month(), DEC);
      myFile.print('/');
      myFile.print(now.day(), DEC);
      myFile.print(',');
      myFile.print(now.hour(), DEC);
      myFile.print(':');
      myFile.print(now.minute(), DEC);
      myFile.print(':');
      myFile.print(now.second(), DEC);
      myFile.print(",");
      myFile.print(temp);
      myFile.print(",");
      myFile.print(humd);
      myFile.print(",");
      myFile.print(data.PM_AE_UG_2_5);
      myFile.println(",");
      }
   myFile.close();
   }
}
void loop() {
  loggingTime();
  loggingSensors();
  delay(5000);
}

and this is the result:

The first thing that I would do is not to use delay() for timing and switch to using the RTC instead. delay() blocks the running of the program which prevents sensor reading from being taken whereas using the RTC you can do the timing and take readings continuously

The program would do something like this

save current time from RTC as start time
start of loop()
  if current time - start time >= 5 seconds
    log the most recent readings
    save current time from RTC as start time
  end if
  read sensors and save values in variables
end of loop()

Note that you do not need to read the sensors each time through loop() and it could be done every second, for instance

Sorry for long time response because i need time for learning to using millis, this is the new code with millis, the PMS result is appear but the delay is not working. It just write once, i think i'm wrong with millis code. if you please, where is my fault?

here's new code:

#include <DFRobot_SHT20.h>
#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC
#include "PMS.h"

File myFile;
const int chipSelect = 10;

RTC_DS1307 RTC;

DFRobot_SHT20    sht20;

PMS pms(Serial1);
PMS::DATA data;

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  startMillis = millis();
  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);
  sht20.checkSHT20();                                 // Check SHT20 Sensor
 
  while(!Serial);
  Serial.println("ArduinoAll DataLogger Shield Test");
  pinMode(SS, OUTPUT);
 
  if(!SD.begin(53,51,50,52)) {
    Serial.println("initialization failed!");
    return;
      // setup for the SD card
    }
    Serial.print("Initializing SD card...");
    RTC.begin();
      if(! RTC.isrunning()) {
      Serial.println("RTC is NOT running!");
       RTC.adjust(DateTime(__DATE__, __TIME__));
    }
      Serial.println("initialization done.");
   
  myFile=SD.open("DATA1.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("File opened ok");
    // print the headings for our data
    myFile.println("Date,Time,Temperature,Humidity,PM2.5,");
  }
  myFile.close();
}

void loop() {
  float humd = sht20.readHumidity();                // Read Humidity
  float temp = sht20.readTemperature();               // Read Temperature
  DateTime now = RTC.now(); 
   if (pms.read(data)){
    currentMillis = millis();
    if (currentMillis - startMillis >= period){
      myFile=SD.open("DATA1.txt", FILE_WRITE);
      if(myFile){
      myFile.print(now.year(), DEC);
      myFile.print('/');
      myFile.print(now.month(), DEC);
      myFile.print('/');
      myFile.print(now.day(), DEC);
      myFile.print(',');
      myFile.print(now.hour(), DEC);
      myFile.print(':');
      myFile.print(now.minute(), DEC);
      myFile.print(':');
      myFile.print(now.second(), DEC);
      myFile.print(",");
      myFile.print(temp);
      myFile.print(",");
      myFile.print(humd);
      myFile.print(",");
      myFile.print(data.PM_AE_UG_2_5);
      myFile.println(",");
      }
   myFile.close();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.day(), DEC);
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  startMillis = currentMillis;
   }
  }
}

and after several minutes waiting, i'm check the result and it just write once also in the serial monitor.
this is the result from sd card

Date,Time,Temperature,Humidity,PM2.5,
2020/9/22,10:39:4,29.48,60.62,145,