Last active
April 26, 2020 09:36
-
-
Save jcouyang/43dd60e94b79526cc2fdd6a69a125d9c to your computer and use it in GitHub Desktop.
Interface to Shinyei Model PPD42NS Particle Sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Interface to Shinyei Model PPD42NS Particle Sensor | |
Program by Christopher Nafis | |
Written April 2012 | |
http://www.seeedstudio.com/depot/grove-dust-sensor-p-1050.html | |
http://www.sca-shinyei.com/pdf/PPD42NS.pdf | |
JST Pin 1 (Black Wire) => Arduino GND | |
JST Pin 3 (Red wire) => Arduino 5VDC | |
JST Pin 4 (Yellow wire) => Arduino Digital Pin 8 | |
3000 + = VERY POOR | |
1050-3000 = POOR | |
300-1050 = FAIR | |
150-300 = GOOD | |
75-150 = VERY GOOD | |
0-75 = EXCELLENT | |
*/ | |
#define POOR 1050 | |
#define VERY_POOR 3000 | |
#define POOR_PIN 6 | |
#define FAIR_PIN 4 | |
#define GOOD_PIN 2 | |
#define GOOD 300 | |
int pin = 8; | |
unsigned long duration; | |
unsigned long starttime; | |
unsigned long timespend; | |
unsigned long sampletime_ms = 60000; | |
unsigned long lowpulseoccupancy = 0; | |
float ratio = 0; | |
float concentration = 0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(8,INPUT); | |
pinMode(POOR_PIN, OUTPUT); | |
pinMode(FAIR_PIN, OUTPUT); | |
pinMode(GOOD_PIN, OUTPUT); | |
starttime = millis(); | |
} | |
void loop() { | |
duration = pulseIn(pin, LOW); | |
lowpulseoccupancy = lowpulseoccupancy+duration; | |
timespend = millis()-starttime; | |
if (timespend > sampletime_ms) | |
{ | |
ratio = lowpulseoccupancy/(timespend*10.0); // Integer percentage 0=>100 | |
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve | |
Serial.print(lowpulseoccupancy); | |
Serial.print(","); | |
Serial.print(ratio); | |
Serial.print(","); | |
Serial.println(concentration); | |
if(concentration <= GOOD){ | |
digitalWrite(GOOD_PIN, HIGH); | |
digitalWrite(FAIR_PIN, LOW); | |
digitalWrite(POOR_PIN, LOW); | |
} else if (concentration <= POOR) { | |
digitalWrite(GOOD_PIN, LOW); | |
digitalWrite(FAIR_PIN, HIGH); | |
digitalWrite(POOR_PIN, LOW); | |
} else if (concentration <= 1500){ | |
digitalWrite(GOOD_PIN, LOW); | |
digitalWrite(FAIR_PIN, LOW); | |
digitalWrite(POOR_PIN, HIGH); | |
} else if (concentration <= 2000) { | |
digitalWrite(GOOD_PIN, HIGH); | |
digitalWrite(FAIR_PIN, LOW); | |
digitalWrite(POOR_PIN, HIGH); | |
} else if(concentration <= VERY_POOR) { | |
digitalWrite(GOOD_PIN, LOW); | |
digitalWrite(FAIR_PIN, HIGH); | |
digitalWrite(POOR_PIN, HIGH); | |
} else { | |
digitalWrite(GOOD_PIN, HIGH); | |
digitalWrite(FAIR_PIN, HIGH); | |
digitalWrite(POOR_PIN, HIGH); | |
} | |
lowpulseoccupancy = 0; | |
starttime = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment