Testing out the PPD42 Air Quality Sensor, with an MSP430 Launchpad and graphing the data with GNUplot.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #include <math.h>
  2. int DustSensePin = 13;
  3. unsigned long duration;
  4. unsigned long starttime;
  5. unsigned long sampletime_ms = 30000;//sample 30s ;
  6. unsigned long lowpulseoccupancy = 0;
  7. float ratio = 0;
  8. float concentration = 0;
  9. void setup()
  10. {
  11. Serial.begin(9600);
  12. Serial.println("Starting Air Sensor, please wait for readings...");
  13. pinMode(DustSensePin,INPUT);
  14. starttime = millis();//get the current time;
  15. }
  16. void loop()
  17. {
  18. duration = pulseIn(DustSensePin, LOW);
  19. lowpulseoccupancy = lowpulseoccupancy+duration;
  20. if ((millis()-starttime) > sampletime_ms)//if the sample time == 30s
  21. {
  22. ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
  23. concentration = 1.1*powf(ratio,3)-3.8*powf(ratio,2)+520*ratio+0.62; // using spec sheet curve
  24. Serial.print(lowpulseoccupancy);
  25. Serial.print(",");
  26. Serial.print(ratio);
  27. Serial.print(",");
  28. Serial.println(concentration);
  29. lowpulseoccupancy = 0;
  30. starttime = millis();
  31. }
  32. }