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;*
}