Created
July 12, 2020 21:20
-
-
Save morimorihoge/0e5827289c0c76427c94efccce815588 to your computer and use it in GitHub Desktop.
M5Stack+CCS811 with LCD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
Compensating the CCS811 with humidity readings from the BME280 | |
Marshall Taylor @ SparkFun Electronics | |
April 4, 2017 | |
https://github.com/sparkfun/CCS811_Air_Quality_Breakout | |
https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library | |
This example uses a BME280 to gather environmental data that is then used | |
to compensate the CCS811. | |
Hardware Connections (Breakoutboard to Arduino): | |
3.3V to 3.3V pin | |
GND to GND pin | |
SDA to A4 | |
SCL to A5 | |
Resources: | |
Uses Wire.h for i2c operation | |
Development environment specifics: | |
Arduino IDE 1.8.1 | |
This code is released under the [MIT License](http://opensource.org/licenses/MIT). | |
Please review the LICENSE.md file included with this example. If you have any questions | |
or concerns with licensing, please contact techsupport@sparkfun.com. | |
Distributed as-is; no warranty is given. | |
******************************************************************************/ | |
/** | |
* This is morimorihoge customized version for M5Stack + CCS811 with LCD output. | |
*/ | |
#include <M5Stack.h> | |
#include <Wire.h> | |
#include <SparkFunBME280.h> //Click here to get the library: http://librarymanager/All#SparkFun_BME280 | |
#include <SparkFunCCS811.h> //Click here to get the library: http://librarymanager/All#SparkFun_CCS811 | |
#define CCS811_ADDR 0x5B //Default I2C Address | |
//#define CCS811_ADDR 0x5A //Alternate I2C Address | |
#define PIN_NOT_WAKE 5 | |
//Global sensor objects | |
CCS811 myCCS811(CCS811_ADDR); | |
BME280 myBME280; | |
void setup() | |
{ | |
M5.begin(); | |
M5.Power.begin(); | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.println("Apply BME280 data to CCS811 for compensation."); | |
M5.Lcd.println(); | |
M5.Lcd.println("Apply BME280 data to CCS811 for compensation."); | |
Wire.begin(); | |
//This begins the CCS811 sensor and prints error status of .beginWithStatus() | |
CCS811Core::CCS811_Status_e returnCode = myCCS811.beginWithStatus(); | |
Serial.print("CCS811 begin exited with: "); | |
Serial.println(myCCS811.statusString(returnCode)); | |
M5.Lcd.print("CCS811 begin exited with: "); | |
M5.Lcd.println(myCCS811.statusString(returnCode)); | |
//For I2C, enable the following and disable the SPI section | |
myBME280.settings.commInterface = I2C_MODE; | |
myBME280.settings.I2CAddress = 0x77; | |
//Initialize BME280 | |
//For I2C, enable the following and disable the SPI section | |
myBME280.settings.commInterface = I2C_MODE; | |
myBME280.settings.I2CAddress = 0x77; | |
myBME280.settings.runMode = 3; //Normal mode | |
myBME280.settings.tStandby = 0; | |
myBME280.settings.filter = 4; | |
myBME280.settings.tempOverSample = 5; | |
myBME280.settings.pressOverSample = 5; | |
myBME280.settings.humidOverSample = 5; | |
//Calling .begin() causes the settings to be loaded | |
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. | |
myBME280.begin(); | |
} | |
//--------------------------------------------------------------- | |
void loop() | |
{ | |
//Check to see if data is available | |
if (myCCS811.dataAvailable()) | |
{ | |
M5.Lcd.clear(); | |
M5.Lcd.setCursor(0, 0); | |
//Calling this function updates the global tVOC and eCO2 variables | |
myCCS811.readAlgorithmResults(); | |
//printInfoSerial fetches the values of tVOC and eCO2 | |
printInfoSerial(); | |
float BMEtempC = myBME280.readTempC(); | |
float BMEhumid = myBME280.readFloatHumidity(); | |
Serial.print("Applying new values (deg C, %): "); | |
Serial.print(BMEtempC); | |
Serial.print(","); | |
Serial.println(BMEhumid); | |
Serial.println(); | |
//This sends the temperature data to the CCS811 | |
myCCS811.setEnvironmentalData(BMEhumid, BMEtempC); | |
} | |
else if (myCCS811.checkForStatusError()) | |
{ | |
//If the CCS811 found an internal error, print it. | |
printSensorError(); | |
} | |
delay(2000); //Wait for next reading | |
} | |
//--------------------------------------------------------------- | |
void printInfoSerial() | |
{ | |
uint16_t co2 = myCCS811.getCO2(); | |
uint16_t tvoc = myCCS811.getTVOC(); | |
//getCO2() gets the previously read data from the library | |
Serial.println("CCS811 data:"); | |
Serial.print(" CO2 concentration : "); | |
Serial.print(co2); | |
Serial.println(" ppm"); | |
M5.Lcd.println("CCS811 data:"); | |
M5.Lcd.print(" CO2 concentration : "); | |
M5.Lcd.print(co2); | |
M5.Lcd.println(" ppm"); | |
//getTVOC() gets the previously read data from the library | |
Serial.print(" TVOC concentration : "); | |
Serial.print(myCCS811.getTVOC()); | |
Serial.println(" ppb"); | |
Serial.println("BME280 data:"); | |
Serial.print(" Temperature: "); | |
Serial.print(myBME280.readTempC(), 2); | |
Serial.println(" degrees C"); | |
Serial.print(" Temperature: "); | |
Serial.print(myBME280.readTempF(), 2); | |
Serial.println(" degrees F"); | |
Serial.print(" Altitude: "); | |
Serial.print(myBME280.readFloatAltitudeMeters(), 2); | |
Serial.println("m"); | |
Serial.print(" Altitude: "); | |
Serial.print(myBME280.readFloatAltitudeFeet(), 2); | |
Serial.println("ft"); | |
Serial.print(" %RH: "); | |
Serial.print(myBME280.readFloatHumidity(), 2); | |
Serial.println(" %"); | |
Serial.println(); | |
} | |
//printSensorError gets, clears, then prints the errors | |
//saved within the error register. | |
void printSensorError() | |
{ | |
uint8_t error = myCCS811.getErrorRegister(); | |
if (error == 0xFF) //comm error | |
{ | |
Serial.println("Failed to get ERROR_ID register."); | |
} | |
else | |
{ | |
Serial.print("Error: "); | |
if (error & 1 << 5) | |
Serial.print("HeaterSupply"); | |
if (error & 1 << 4) | |
Serial.print("HeaterFault"); | |
if (error & 1 << 3) | |
Serial.print("MaxResistance"); | |
if (error & 1 << 2) | |
Serial.print("MeasModeInvalid"); | |
if (error & 1 << 1) | |
Serial.print("ReadRegInvalid"); | |
if (error & 1 << 0) | |
Serial.print("MsgInvalid"); | |
Serial.println(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment