Skip to content

Instantly share code, notes, and snippets.

@fxprime
Last active February 3, 2021 02:15
Show Gist options
  • Save fxprime/febb45709a7e92ceda8536a04882b6c9 to your computer and use it in GitHub Desktop.
Save fxprime/febb45709a7e92ceda8536a04882b6c9 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
/* -------------------------------------------------------------------------- */
/* PM2.5 Variable */
/* -------------------------------------------------------------------------- */
// Choose program options.
//#define PRINT_RAW_DATA //uncomet if you wont raw data displayed in serial monitor
#define USE_AVG //calculate average value in N mesared values
// Arduino pin numbers.
const int sharpLEDPin = 7; // Arduino digital pin 7 connect to sensor LED.
const int sharpVoPin = A0; // Arduino analog pin 5 connect to sensor Vo.
// For averaging last N raw voltage readings.
#ifdef USE_AVG
#define N 100
static unsigned long VoRawTotal = 0;
static int VoRawCount = 0;
#endif // USE_AVG
// Set the typical output voltage in Volts when there is zero dust.
static float Voc = 0.6; //Voc is 0.6 Volts for dust free acordind sensor spec
static float VocT = 0.6; //Self calibration overvrite the Coc with minimum measered value - this variable you need to have the treshold for the sensor (facturi calibration)
// Use the typical sensitivity in units of V per 100ug/m3.
const float K = 0.5;
/////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
/* ---------------------------- Start sharp pm2.5 --------------------------- */
pinMode(sharpLEDPin, OUTPUT);
VocT = Voc; //corect the VocT (to have alwas data for the initioal Voc
/* ------------------------------ Start system ------------------------------ */
Serial.println("Dust(ug/m3)");
}
void loop()
{
/* --------------------------- Correct data PM2.5 --------------------------- */
digitalWrite(sharpLEDPin, LOW); // Turn on the dust sensor LED by setting digital pin LOW.
delayMicroseconds(280); // Wait 0.28ms before taking a reading of the output voltage as per spec.
int VoRaw = analogRead(sharpVoPin); // Record the output voltage. This operation takes around 100 microseconds.
digitalWrite(sharpLEDPin, HIGH); // Turn the dust sensor LED off by setting digital pin HIGH.
delayMicroseconds(9620); // Wait for remainder of the 10ms cycle = 10000 - 280 - 100 microseconds.
// Use averaging if needed.
float Vo = VoRaw;
VoRawTotal += VoRaw;
VoRawCount++;
if (VoRawCount >= N)
{
Vo = 1.0 * VoRawTotal / N;
VoRawCount = 0;
VoRawTotal = 0;
}
else
{
return;
}
// Compute the output voltage in Volts.
Vo = Vo / 1024.0 * 5.0;
//------------ Dust density calculated by Voc set in the begining - like in sensor spec ------------
// Calculate Dust Density in units of ug/m3 for the initial Voc (as in sensor specs)
float dV = Vo - VocT;
if (dV < 0)
{
dV = 0;
}
float dustDensity = dV / K * 100.0;
dV = 0; // Reset the dV to 0 for the next calculation
dustDensity = 0; // Reset the dustDensity to 0 for the next calculation
//------------ Dust density calculated by lowest output voltage during runtime ------------
// Convert to Dust Density in units of ug/m3.
// During runtime, the Voc value is updated dynamically whenever a lower output voltage is sense.
// this cover the dust sensing below the sensors specefied range from 0 to Voc (0,6V) as specified in the beginin
dV = Vo - Voc;
if (dV < 0)
{
dV = 0;
Voc = Vo;
}
dustDensity = dV / K * 100.0;
/* --------------------------------- Output --------------------------------- */
String dataPack="";
dataPack = String(dustDensity)+"\n";
Serial.print(dataPack);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment