Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*******************************************
- //* SGP30 Sensor w/ 2x16 LCD *
- //* *
- //* learnelectronic 26, June, 2020 *
- //* *
- //* www.youtube.com/learnelectronics *
- //* *
- //* email: [email protected] *
- //*******************************************
- #include "rgb_lcd.h" //I2C Library for LCD
- #include <Wire.h> //I2C Library
- #include "Adafruit_SGP30.h" //SGP30 Sensor Library
- rgb_lcd lcd; //assign name lcd to rgb_lcd
- Adafruit_SGP30 sgp; //assign name sgp to sensor
- // Function to read from sensor
- uint32_t getAbsoluteHumidity(float temperature, float humidity) {
- // approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
- const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
- const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
- return absoluteHumidityScaled;
- }
- int counter = 0; //setup a counter variable(global)
- void setup() {
- Serial.begin(9600); //start serial comms for debug
- lcd.begin(16, 2); // set up the LCD's number of columns and rows
- if (! sgp.begin()){
- Serial.println("Sensor not found :("); //check for sensor on I2c bus
- while (1);
- }
- Serial.print("Found SGP30 serial #"); //return serial number of sensor
- Serial.print(sgp.serialnumber[0], HEX);
- Serial.print(sgp.serialnumber[1], HEX);
- Serial.println(sgp.serialnumber[2], HEX);
- }
- void loop()
- {
- lcd.setCursor(0, 0); // set the cursor to column 0, line 1
- // (note: line 1 is the second row, since counting begins with 0):
- if (! sgp.IAQmeasure()) {
- Serial.println("Measurement failed"); //Get reading from sensor
- return;
- }
- if (! sgp.IAQmeasureRaw()) {
- Serial.println("Raw Measurement failed"); //Get reading from sensor
- return;
- }
- lcd.print("TVOC ");
- lcd.print(sgp.TVOC); //print total voc's on line 0
- lcd.print(" ppb");
- lcd.setCursor(0, 1);
- lcd.print("eCO2 ");
- lcd.print(sgp.eCO2); //print total eCo2 on line 1
- lcd.print(" ppm");
- delay(1000); //wait one second
- counter++; //increment counrt up to 30, at 30 reset
- if (counter == 30) {
- counter = 0;
- //create variables for sensor readings
- uint16_t TVOC_base, eCO2_base;
- if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
- Serial.println("Failed to get baseline readings");
- return;
- }
- Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
- Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
- }
- lcd.setCursor(0, 0); //clear the screen
- lcd.print(" ");
- lcd.setCursor(0, 01);
- lcd.print(" ");
- }
Add Comment
Please, Sign In to add comment