BME680 gas temperature pressure sensor

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME680 bme; // I2C
//Adafruit_BME680 bme(BME_CS); // hardware SPI
//Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

float hum_weighting = 0.25; // so hum effect is 25% of the total air quality score
float gas_weighting = 0.75; // so gas effect is 75% of the total air quality score

float hum_score, gas_score;
float gas_reference = 250000;
float hum_reference = 40;
int   getgasreference_count = 0;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println(F("BME680 test"));

  
  Wire.begin();
  if (!bme.begin()) {
	Serial.println("Could not find a valid BME680 sensor, check wiring!");
	while (1);
  } else Serial.println("Found a sensor");

  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_2X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_2X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320°C for 150 ms
  // Now run the sensor for a burn-in period, then use combination of relative humidity and gas resistance to estimate indoor air quality as a percentage.
  GetGasReference();
}

void loop() {
  Serial.print("Temperature = ");

  Serial.print(bme.readTemperature());
  Serial.println("°C");

  Serial.print("   Pressure = ");

  Serial.print(bme.readPressure() / 100.0F);
  Serial.println(" hPa");

  Serial.print("   Humidity = ");
  Serial.print(bme.readHumidity());
  Serial.println("%");

  Serial.print("		Gas = ");
  Serial.print(bme.readGas());
  Serial.println("R\n");
/*
 This software, the ideas and concepts is Copyright (c) David Bird 2018. All rights to this software are reserved.
 
 Any redistribution or reproduction of any part or all of the contents in any form is prohibited other than the following:
 1. You may print or download to a local hard disk extracts for your personal and non-commercial use only.
 2. You may copy the content to individual third parties for their personal use, but only if you acknowledge the author David Bird as the source of the material.
 3. You may not, except with my express written permission, distribute or commercially exploit the content.
 4. You may not transmit it or store it in any other website or other form of electronic retrieval system for commercial purposes.

 The above copyright ('as annotated') notice and this permission notice shall be included in all copies or substantial portions of the Software and where the
 software use is visible to an end-user.
 
 THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT. FOR PERSONAL USE IT IS SUPPLIED WITHOUT WARRANTY 
 OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 See more at http://www.dsbird.org.uk
*/  
  //Calculate humidity contribution to IAQ index
  float current_humidity = bme.readHumidity();
  if (current_humidity >= 38 && current_humidity <= 42)
	hum_score = 0.25*100; // Humidity +/-5% around optimum 
  else
  { //sub-optimal
	if (current_humidity < 38) 
	  hum_score = 0.25/hum_reference*current_humidity*100;
	else
	{
	  hum_score = ((-0.25/(100-hum_reference)*current_humidity)+0.416666)*100;
	}
  }
  
  //Calculate gas contribution to IAQ index
  float gas_lower_limit = 5000;   // Bad air quality limit
  float gas_upper_limit = 50000;  // Good air quality limit 
  if (gas_reference > gas_upper_limit) gas_reference = gas_upper_limit; 
  if (gas_reference < gas_lower_limit) gas_reference = gas_lower_limit;
  gas_score = (0.75/(gas_upper_limit-gas_lower_limit)*gas_reference -(gas_lower_limit*(0.75/(gas_upper_limit-gas_lower_limit))))*100;
  
  //Combine results for the final IAQ index value (0-100% where 100% is good quality air)
  float air_quality_score = hum_score + gas_score;

  Serial.println("Air Quality = "+String(air_quality_score,1)+"% derived from 25% of Humidity reading and 75% of Gas reading - 100% is good quality air");
  Serial.println("Humidity element was : "+String(hum_score/100)+" of 0.25");
  Serial.println("	 Gas element was : "+String(gas_score/100)+" of 0.75");
  if (bme.readGas() < 120000) Serial.println("***** Poor air quality *****");
  Serial.println();
  if ((getgasreference_count++)%10==0) GetGasReference(); 
  Serial.println(CalculateIAQ(air_quality_score));
  Serial.println("------------------------------------------------");
  delay(2000);
}

void GetGasReference(){
  // Now run the sensor for a burn-in period, then use combination of relative humidity and gas resistance to estimate indoor air quality as a percentage.
  Serial.println("Getting a new gas reference value");
  int readings = 10;
  for (int i = 1; i <= readings; i++){ // read gas for 10 x 0.150mS = 1.5secs
	gas_reference += bme.readGas();
  }
  gas_reference = gas_reference / readings;
}

String CalculateIAQ(float score){
  String IAQ_text = "Air quality is ";
  score = (100-score)*5;
  if	  (score >= 301)				  IAQ_text += "Hazardous";
  else if (score >= 201 && score <= 300 ) IAQ_text += "Very Unhealthy";
  else if (score >= 176 && score <= 200 ) IAQ_text += "Unhealthy";
  else if (score >= 151 && score <= 175 ) IAQ_text += "Unhealthy for Sensitive Groups";
  else if (score >=  51 && score <= 150 ) IAQ_text += "Moderate";
  else if (score >=  00 && score <=  50 ) IAQ_text += "Good";
  return IAQ_text;
}

hello i have a problem , this code work on SPI ( 5wire )
but on 2 wire ic2 (sda-scl) pinnumbers on my wemos lolin are 21 and 22
it not work , can somebody explain whats wrong.

Wemos Lolin what? ESP8266 board? ESP32 board? Which one? Include a link to your specific board please.

board = ESP32 WeMos LOLIN32 high resolution pinout and specs – Renzo Mischianti

Please explain what this means, what you expected to happen, and what happens instead. Post error messages, if any.

Did you check the schematic to see if your board included pullups on the I2C lines? I didn't see any in the brief look I took. Did you check to see if there are pullups on your BME680 breakout board? Did you check to see your library to see if it set the pins to INPUT_PULLUP (if that's even a feature for that board)? And if you did check, why did you not mention it in your original message? That's the kind of basic detail someone trying to help you diagnose your problem would need.

get this message

Could not find a valid BME680 sensor, check wiring!

if i do the spi conversion with all the wires the sensor works,
but i need those pins on the bord for the project

so it must be working also on ic2
with the pins 21 en 22 , but not work

Run the Arduino I2C address scanner program to see if communications are working.

I2C scanner. Scanning ...

Found 0 device(s).

0 devices :grinning: :rofl:

and now ? what can i do

Do you have another, known working I2C device, you could use to check if the I2C scanner is working ?

Did you check for pullups?

And for the I2C scanner try this change for Wire.begin();

Wire.begin(21, 22); //format is Wire.begin(SDA,SCL);

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println(F("BME680 test"));

  
  Wire.begin(21, 22); //format is Wire.begin(SDA,SCL); // I2C
  if (!bme.begin()) {
	Serial.println("Could not find a valid BME680 sensor, check wiring!");
	while (1);
  } else Serial.println("Found a sensor");

yesss yesss it works thank you very much srnet
now i must try to convert this to send to blynk-server with vertual pins

thank you very much

Temperature = 23.12°C
Pressure = 1022.00 hPa
Humidity = 30.08%
Gas = 173935R

Air Quality = 93.8% derived from 25% of Humidity reading and 75% of Gas reading - 100% is good quality air
Humidity element was : 0.19 of 0.25
Gas element was : 0.75 of 0.75

Air quality is Good

I have been testing the Adafruit BME680 sensor board with a SEEED XIAO ESP32S3 with a LoRa module attached.

The sleep current of the setup, which also uses an INA219 to monitor battery voltage and current is 18uA.

The BME680 board has a sleep current of about 120uA with the boards power LED running and 40uA without. The BME680 itself claims a sleep current of circa 0.15uA, so the Adafruit board appear to use a bit more current. The Adafruit library does not appear to have any power down or sleep commands.

I have asked Adfruit if there is a resolution to the apparent high sleep current.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.