Go Down

Topic: SD-Card dust sensor data (Read 231 times) previous topic - next topic

beyam78

Hello people!
I wanted to store the data that my dust sensor (PM2.5 laser dust sensor) give me in a MicroSD Card Adapter. I put the code I use to store te data, and the code for the PM dust sensor. The problem is : when I open the serial monitor I only see zeros instead of the real values. The PM works and the SD module also.  I got no errors when I upload. I have  an arduino uno.
Thx!

beyam78

I'm gonna put down the code

Code for the PM dust sensor

//******************************
 //*Abstract: Read value of PM1,PM2.5 and PM10 of air quality
 //
 //*Version:V3.1
 //*Author:Zuyang @ HUST
 //*Modified by Cain for Arduino Hardware Serial port compatibility
 //*Date:March.25.2016
 //******************************
#include <Arduino.h>
#define LENG 31   //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];

int PM01Value=0;          //define PM1.0 value of the air detector module
int PM2_5Value=0;         //define PM2.5 value of the air detector module
int PM10Value=0;         //define PM10 value of the air detector module


void setup()
{
  Serial.begin(9600);   //use serial0
  Serial.setTimeout(1500);    //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor

}

void loop()
{
  if(Serial.find(0x42)){    //start to read when detect 0x42
    Serial.readBytes(buf,LENG);

    if(buf[0] == 0x4d){
      if(checkValue(buf,LENG)){
        PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
        PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
        PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
      }           
    }
  }

  static unsigned long OledTimer=millis(); 
    if (millis() - OledTimer >=1000)
    {
      OledTimer=millis();
     
      Serial.print("PM1.0: "); 
      Serial.print(PM01Value);
      Serial.println("  ug/m3");           
   
      Serial.print("PM2.5: "); 
      Serial.print(PM2_5Value);
      Serial.println("  ug/m3");     
     
      Serial.print("PM1 0: "); 
      Serial.print(PM10Value);
      Serial.println("  ug/m3");   
      Serial.println();
    }
 
}
char checkValue(unsigned char *thebuf, char leng)

  char receiveflag=0;
  int receiveSum=0;

  for(int i=0; i<(leng-2); i++){
  receiveSum=receiveSum+thebuf;
  }
  receiveSum=receiveSum + 0x42;
 
  if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the serial data
  {
    receiveSum = 0;
    receiveflag = 1;
  }
  return receiveflag;
}

int transmitPM01(unsigned char *thebuf)
{
  int PM01Val;
  PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
  return PM01Val;
}

//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
  int PM2_5Val;
  PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
  return PM2_5Val;
  }

//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
  int PM10Val;
  PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module 
  return PM10Val;
}

And here is the code I wrote to store the data from the PM in the SD card


/**
 * Exemple de code Arduino pour un datalogger basique avec stockage sur carte SD.
 */

/* Dépendances */
#include <SPI.h> // Pour la communication SPI
#include <SD.h>  // Pour la communication avec la carte SD
#include <Arduino.h>
#define LENG 31   //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];



/** Broche CS de la carte SD */
const byte SDCARD_CS_PIN = 10; // A remplacer suivant votre shield SD

/** Nom du fichier de sortie */
const char* OUTPUT_FILENAME = "data.csv";

/** Delai entre deux prise de mesures */
const unsigned long DELAY_BETWEEN_MEASURES = 5000;


/** Fichier de sortie avec les mesures */
File file;

int PM01Value=0;          //define PM1.0 value of the air detector module
int PM2_5Value=0;         //define PM2.5 value of the air detector module
int PM10Value=0;         //define PM10 value of the air detector module


/** Fonction setup() */
void setup() {

  /* Initialisation du port série (debug) */
  Serial.begin(9600);
  Serial.setTimeout(1500);    //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor

  /* Initialisation du port SPI */
  pinMode(10, OUTPUT); // Arduino UNO
  //pinMode(53, OUTPUT); // Arduino Mega

  /* Initialisation de la carte SD */
  Serial.println(F("Initialisation de la carte SD ... "));
  if (!SD.begin(SDCARD_CS_PIN)) {
    Serial.println(F("Erreur : Impossible d'initialiser la carte SD"));
    Serial.println(F("Verifiez la carte SD et appuyez sur le bouton RESET"));
    for (;;); // Attend appui sur bouton RESET
  }

  /* Ouvre le fichier de sortie en écriture */
  Serial.println(F("Ouverture du fichier de sortie ... "));
  file = SD.open(OUTPUT_FILENAME, FILE_WRITE);
  if (!file) {
    Serial.println(F("Erreur : Impossible d'ouvrir le fichier de sortie"));
    Serial.println(F("Verifiez la carte SD et appuyez sur le bouton RESET"));
    for (;;); // Attend appui sur bouton RESET
  }
 
  /* Ajoute l'entête CSV si le fichier est vide */
  if (file.size() == 0) {
    Serial.println(F("Ecriture de l'entete CSV ..."));
    file.println(F("PM1.0- PM2.5- PM10"));
    file.flush();
  }
}


/** Fonction loop() */
void loop() {
  // Temps de la précédente mesure et actuel
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();

  /* Réalise une prise de mesure toutes les DELAY_BETWEEN_MEASURES millisecondes */
  if (currentMillis - previousMillis >= DELAY_BETWEEN_MEASURES) {
    previousMillis = currentMillis;
    measure();
  }
 if(Serial.find(0x42)){    //start to read when detect 0x42
    Serial.readBytes(buf,LENG);

    if(buf[0] == 0x4d){
      if(checkValue(buf,LENG)){
        PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
        PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
        PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
      }           
    }
  }
 
}


/** Fonction de mesure - à personnaliser selon ses besoins */
void measure() {

  /* Réalise la mesure */
  float PM01 = PM01Value;
  float PM25 = PM2_5Value;
  float PM10 = PM10Value;
 
 
  /* Affiche les données sur le port série pour debug */
  Serial.print(PM01);
  Serial.print(F("-    "));
  Serial.print(PM25);
  Serial.print(F("-    "));
  Serial.println(PM10);
   
  /* Enregistre les données sur la carte SD */
  file.print(PM01);
  file.print(F("-   "));
  file.print(PM25);
  file.print(F("-   "));
  file.println(PM10);
  file.flush();
}
char checkValue(unsigned char *thebuf, char leng)

  char receiveflag=0;
  int receiveSum=0;

  for(int i=0; i<(leng-2); i++){
  receiveSum=receiveSum+thebuf;
  }
  receiveSum=receiveSum + 0x42;
 
  if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the serial data
  {
    receiveSum = 0;
    receiveflag = 1;
  }
  return receiveflag;
}

int transmitPM01(unsigned char *thebuf)
{
  int PM01Val;
  PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
  return PM01Val;
}

//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
  int PM2_5Val;
  PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
  return PM2_5Val;
  }

//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
  int PM10Val;
  PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module 
  return PM10Val;
}


beyam78

For those who downloaded the file ''codePM.ino" I'm sorry it's a mistake, the good one (sdpm) is in the attachements

PaulS

Are you using Serial to talk to the dust sensor or the PC? Yes is the wrong answer. So is both.
The art of getting good answers lies in asking good questions.

beyam78

PaulS, I don't understand your question, I'm new to arduino  :D  , can you explain me please?

PaulS

It's a simple question. How are you communicating with your dust sensor? It looks to me like you use Serial.find() and Serial.read() to get data from it.

How are you communication with your PC? It looks to me like you are using Serial.print() to talk to it. What is the dust sensor supposed to do with that information?

What you are doing is like using a conference line to talk to your wife and your girl friend at the same time. You wouldn't do THAT, would you?
The art of getting good answers lies in asking good questions.

beyam78

Okay I got it! So I got to use another way to communicate with my PC like...what function please?

PaulS

Quote
So I got to use another way to communicate with my PC like...what function please?
No, you need to use another way to communicate with the dust sensor. Like SoftwareSerial.
The art of getting good answers lies in asking good questions.

Go Up