Advertisement
CuriousScientist

Arduino code for air quality measurement

Mar 22nd, 2020
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found this code useful, please subscribe to my channel: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //It took several hours for me to prepare everything, but subscription is just a click for you. Thank you!
  3. //This code belongs to the following tutorial video: https://youtu.be/FahmF_p4xJc
  4.  
  5. //Info: GP2Y1010AU0F Dust sensor
  6. //The dust sensor reaches the maximum output pulse 0.28 ms after the LED was turned on
  7. //This sensor is based on voltage measurement
  8. //CCS811 is a thermometer-VOC meter and uses a premade library
  9. //Sharp GP2Y1010AU0F datasheet: https://pdf1.alldatasheet.com/datasheet-pdf/view/412700/SHARP/GP2Y1010AU0F.html
  10. //CCS811 datasheet: https://pdf1.alldatasheet.com/datasheet-pdf/view/1047395/AMSCO/CCS811.html
  11. //CCS811 library: https://github.com/adafruit/Adafruit_CCS811
  12.  
  13. //VOC sensor
  14. #include "Adafruit_CCS811.h" //We load the library for the gas sensor
  15. Adafruit_CCS811 ccs;
  16. float co2Amount; //Amount (mg/m^3) of CO2, later, it will be converted to ug/m^3
  17. float temperature; //temperature measured by the CCS811
  18.  
  19. //Dust sensor
  20. int dustmeasurePin = A6; //The output of the dust sensor is connected to A6 (AD converter pin)
  21. int dustLEDPin = 2; //The IR pin inside the dust sensor is connected to D2 (digital output pin)
  22. float outBits = 0; //AD-converter raw output
  23. float dustDensity = 0; //dust density, based on the formula (see later)
  24.  
  25. //String for storing the formatted output data
  26. String outputData;
  27.  
  28. void setup()
  29. {
  30.     Serial.begin(9600); //Start serial
  31.  
  32.     Serial.println("*Dust and VOC sensor"); //Print message. I use '*' to tell the receives software that this is not a measurement data
  33.  
  34.     if(!ccs.begin()) //if we are not able to start the VOC sensor, print the following message
  35.     {
  36.         Serial.println("* Failed to start sensor! Please check your wiring.");
  37.         while(1); //And hold the code here
  38.     }
  39.    
  40.     while(!ccs.available());
  41.  
  42.     float temp = ccs.calculateTemperature(); //calculate the temperature
  43.     ccs.setTempOffset(temp - 25.0); //set the offset using the
  44.  
  45.     pinMode(dustLEDPin,OUTPUT); //the pin for the dust sensor's LED is set as an output
  46. }
  47.  
  48.  
  49. void loop()
  50. {
  51.     if (Serial.available() > 0) //if there's something on the serial
  52.     {
  53.     char commandCharacter = Serial.read(); //we use characters (letters) for controlling the switch-case
  54.  
  55.         switch (commandCharacter)
  56.         {
  57.           case 'S': //S: start
  58.  
  59.           while(Serial.read() != 'N') //while we don't send N through the serial, the following functions are looping:
  60.           {
  61.              measureDust();
  62.              delay(500);
  63.              measureVOCs();
  64.              delay(500);
  65.              printFormattedData();        
  66.           }
  67.           break;
  68.  
  69.           default:
  70.               //
  71.           break;    
  72.         }    
  73.   }
  74. }
  75.  
  76. void measureDust()
  77. {
  78.   digitalWrite(dustLEDPin,LOW); //turn ON the LED
  79.  
  80.   delayMicroseconds(280); // wait 0.28 ms = 280 us
  81.  
  82.   outBits = analogRead(dustmeasurePin); //measure the peak of the output pulse  
  83.  
  84.   digitalWrite(dustLEDPin,HIGH); //turn OFF the LED    
  85.  
  86.   /*
  87.   If you want to get the converted data on the Arduino terminal,
  88.   //uncomment this part and replace the outbits to dustDensity in printFormattedData()
  89.  
  90.   dustDensity = 1000* ( 0.17 * ((5.0 / 1024) * outBits) - 0.1); //dust density in ug/m^3
  91.   */  
  92. }
  93.  
  94. void measureVOCs()
  95. {
  96.     if(ccs.available()) //If we can communicate with the VOC sensor
  97.     {
  98.         if(!ccs.readData()) //if we are not reading (i.e. the devide is available)
  99.         {
  100.             co2Amount = ccs.geteCO2(); //read CO2
  101.             ccs.getTVOC(); //read temperature
  102.             temperature = ccs.calculateTemperature(); //calculate temperature
  103.         }
  104.         else
  105.         {
  106.             Serial.println("ERROR!"); //Print error message
  107.             while(1); //wait here
  108.         }
  109.     }
  110. }
  111.  
  112. void printFormattedData() //Formatting the output so the receiver software can process it
  113. {  
  114.   //output:      T                     Dust              CO2
  115.   outputData = (String)temperature + '\t' + (String)outBits + '\t' + (String)co2Amount ; //for the output
  116.   Serial.println(outputData);
  117.  
  118.   //outBits can be replaced to dustDensity to print the converted data instead of the raw data.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement