learnelectronics

SGP30 Sensor

Jun 28th, 2020
1,423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. //*******************************************
  2. //*        SGP30 Sensor w/ 2x16 LCD         *
  3. //*                                         *
  4. //*      learnelectronic 26, June, 2020     *
  5. //*                                         *
  6. //*      www.youtube.com/learnelectronics   *
  7. //*                                         *
  8. //*      email: [email protected]       *
  9. //*******************************************
  10.  
  11. #include "rgb_lcd.h"                            //I2C Library for LCD
  12. #include <Wire.h>                               //I2C Library
  13. #include "Adafruit_SGP30.h"                     //SGP30 Sensor Library
  14.  
  15.  
  16. rgb_lcd lcd;                                    //assign name lcd to rgb_lcd
  17. Adafruit_SGP30 sgp;                             //assign name sgp to sensor
  18.  
  19.  
  20. // Function to read from sensor
  21.      uint32_t getAbsoluteHumidity(float temperature, float humidity) {
  22. // approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
  23.     const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
  24.     const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
  25.     return absoluteHumidityScaled;
  26. }
  27.  
  28. int counter = 0;                                //setup a counter variable(global)
  29.  
  30.  
  31. void setup() {
  32.     Serial.begin(9600);                         //start serial comms for debug
  33.    
  34.     lcd.begin(16, 2);                           // set up the LCD's number of columns and rows
  35.  
  36.  
  37.  if (! sgp.begin()){
  38.     Serial.println("Sensor not found :(");      //check for sensor on I2c bus
  39.     while (1);
  40.   }
  41.   Serial.print("Found SGP30 serial #");         //return serial number of sensor
  42.   Serial.print(sgp.serialnumber[0], HEX);
  43.   Serial.print(sgp.serialnumber[1], HEX);
  44.   Serial.println(sgp.serialnumber[2], HEX);
  45.    
  46. }
  47.  
  48. void loop()
  49. {
  50.    
  51.     lcd.setCursor(0, 0);                        // set the cursor to column 0, line 1
  52.                                                 // (note: line 1 is the second row, since counting begins with 0):
  53.  
  54.      if (! sgp.IAQmeasure()) {
  55.     Serial.println("Measurement failed");       //Get reading from sensor
  56.     return;
  57.   }
  58.  
  59.   if (! sgp.IAQmeasureRaw()) {
  60.     Serial.println("Raw Measurement failed");   //Get reading from sensor
  61.     return;
  62.   }
  63.  
  64. lcd.print("TVOC ");
  65. lcd.print(sgp.TVOC);                            //print total voc's on line 0
  66. lcd.print(" ppb");
  67.  
  68.  
  69.    lcd.setCursor(0, 1);
  70.    lcd.print("eCO2 ");
  71.    lcd.print(sgp.eCO2);                         //print total eCo2 on line 1
  72.    lcd.print(" ppm");
  73.    
  74.   delay(1000);                                  //wait one second
  75.  
  76.   counter++;                                    //increment counrt up to 30, at 30 reset
  77.   if (counter == 30) {
  78.     counter = 0;
  79.  
  80.  
  81.                                                 //create variables for sensor readings
  82.     uint16_t TVOC_base, eCO2_base;
  83.     if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
  84.       Serial.println("Failed to get baseline readings");
  85.       return;
  86.     }
  87.     Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
  88.     Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
  89.   }
  90.  
  91. lcd.setCursor(0, 0);                            //clear the screen
  92. lcd.print("                ");
  93. lcd.setCursor(0, 01);
  94. lcd.print("                ");
  95. }
Add Comment
Please, Sign In to add comment