Skip to content

Instantly share code, notes, and snippets.

@izumogeiger
Created August 27, 2016 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izumogeiger/0136c538cb220f283a1cbbd841611706 to your computer and use it in GitHub Desktop.
Save izumogeiger/0136c538cb220f283a1cbbd841611706 to your computer and use it in GitHub Desktop.
int measurePin = 0; //Connect dust sensor to Arduino A0 pin
int ledPower = 3; //Connect 3 led driver pins of dust sensor to Arduino D2
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
}
void loop(){
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(sleepTime);
// 0 - 5V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (5.0 / 1024.0);
// linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
// Chris Nafis (c) 2012
dustDensity = 0.17 * calcVoltage - 0.1;
Serial.print("Raw Signal Value (0-1023): ");
Serial.print(voMeasured);
Serial.print(" - Voltage: ");
Serial.print(calcVoltage);
Serial.print(" - Dust Density: ");
Serial.print(dustDensity * 1000); // 這裡將數值呈現改成較常用的單位( ug/m3 )
Serial.println(" ug/m3 ");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment