Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If you found this code useful, please subscribe to my channel: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
- //It took several hours for me to prepare everything, but subscription is just a click for you. Thank you!
- //This code belongs to the following tutorial video: https://youtu.be/FahmF_p4xJc
- //Info: GP2Y1010AU0F Dust sensor
- //The dust sensor reaches the maximum output pulse 0.28 ms after the LED was turned on
- //This sensor is based on voltage measurement
- //CCS811 is a thermometer-VOC meter and uses a premade library
- //Sharp GP2Y1010AU0F datasheet: https://pdf1.alldatasheet.com/datasheet-pdf/view/412700/SHARP/GP2Y1010AU0F.html
- //CCS811 datasheet: https://pdf1.alldatasheet.com/datasheet-pdf/view/1047395/AMSCO/CCS811.html
- //CCS811 library: https://github.com/adafruit/Adafruit_CCS811
- //VOC sensor
- #include "Adafruit_CCS811.h" //We load the library for the gas sensor
- Adafruit_CCS811 ccs;
- float co2Amount; //Amount (mg/m^3) of CO2, later, it will be converted to ug/m^3
- float temperature; //temperature measured by the CCS811
- //Dust sensor
- int dustmeasurePin = A6; //The output of the dust sensor is connected to A6 (AD converter pin)
- int dustLEDPin = 2; //The IR pin inside the dust sensor is connected to D2 (digital output pin)
- float outBits = 0; //AD-converter raw output
- float dustDensity = 0; //dust density, based on the formula (see later)
- //String for storing the formatted output data
- String outputData;
- void setup()
- {
- Serial.begin(9600); //Start serial
- Serial.println("*Dust and VOC sensor"); //Print message. I use '*' to tell the receives software that this is not a measurement data
- if(!ccs.begin()) //if we are not able to start the VOC sensor, print the following message
- {
- Serial.println("* Failed to start sensor! Please check your wiring.");
- while(1); //And hold the code here
- }
- while(!ccs.available());
- float temp = ccs.calculateTemperature(); //calculate the temperature
- ccs.setTempOffset(temp - 25.0); //set the offset using the
- pinMode(dustLEDPin,OUTPUT); //the pin for the dust sensor's LED is set as an output
- }
- void loop()
- {
- if (Serial.available() > 0) //if there's something on the serial
- {
- char commandCharacter = Serial.read(); //we use characters (letters) for controlling the switch-case
- switch (commandCharacter)
- {
- case 'S': //S: start
- while(Serial.read() != 'N') //while we don't send N through the serial, the following functions are looping:
- {
- measureDust();
- delay(500);
- measureVOCs();
- delay(500);
- printFormattedData();
- }
- break;
- default:
- //
- break;
- }
- }
- }
- void measureDust()
- {
- digitalWrite(dustLEDPin,LOW); //turn ON the LED
- delayMicroseconds(280); // wait 0.28 ms = 280 us
- outBits = analogRead(dustmeasurePin); //measure the peak of the output pulse
- digitalWrite(dustLEDPin,HIGH); //turn OFF the LED
- /*
- If you want to get the converted data on the Arduino terminal,
- //uncomment this part and replace the outbits to dustDensity in printFormattedData()
- dustDensity = 1000* ( 0.17 * ((5.0 / 1024) * outBits) - 0.1); //dust density in ug/m^3
- */
- }
- void measureVOCs()
- {
- if(ccs.available()) //If we can communicate with the VOC sensor
- {
- if(!ccs.readData()) //if we are not reading (i.e. the devide is available)
- {
- co2Amount = ccs.geteCO2(); //read CO2
- ccs.getTVOC(); //read temperature
- temperature = ccs.calculateTemperature(); //calculate temperature
- }
- else
- {
- Serial.println("ERROR!"); //Print error message
- while(1); //wait here
- }
- }
- }
- void printFormattedData() //Formatting the output so the receiver software can process it
- {
- //output: T Dust CO2
- outputData = (String)temperature + '\t' + (String)outBits + '\t' + (String)co2Amount ; //for the output
- Serial.println(outputData);
- //outBits can be replaced to dustDensity to print the converted data instead of the raw data.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement