|
| 1 | +/** |
| 2 | + * @file bme680_adjust_sensor_settings.c |
| 3 | + * |
| 4 | + * @author Matthew Matz |
| 5 | + * |
| 6 | + * @version 0.5 |
| 7 | + * |
| 8 | + * @copyright |
| 9 | + * Copyright (C) Parallax, Inc. 2019. All Rights MIT Licensed. |
| 10 | + * |
| 11 | + * @brief Example demonstrating how to adjust the sensor's settings |
| 12 | + * before taking a reading. |
| 13 | + */ |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +#include "simpletools.h" // Include simple tools |
| 18 | +#include "bme680.h" |
| 19 | + |
| 20 | + |
| 21 | +bme680 *mySensor; // Create a new sensor instance |
| 22 | + |
| 23 | + |
| 24 | +int main() { |
| 25 | + |
| 26 | + // Open the sensor using an SPI interface |
| 27 | + // SDO, CLK, SDI, CS |
| 28 | + mySensor = bme680_openSPI(20, 21, 22, 23); |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + // Set the ambient temperature used (in deg C) by the gas sensor's calculations. |
| 33 | + // A temperature measurement could be taken and fed back into this setting for increased |
| 34 | + // accuracy. |
| 35 | + bme680_configure(mySensor, BME680_SETTING_AMB_TEMP, 20); |
| 36 | + |
| 37 | + // Set the temperature of the gas sensor's heating element (in deg C). |
| 38 | + bme680_configure(mySensor, BME680_SETTING_HEATER_TEMP, 300); |
| 39 | + |
| 40 | + // Set how long the gas sensor's heating element is turned on |
| 41 | + // during a measurement (in milliseconds). |
| 42 | + bme680_configure(mySensor, BME680_SETTING_HEATER_DURATION, 120); |
| 43 | + |
| 44 | + // Set the low-pass filter used by the sensor. Higher values reduce jitter |
| 45 | + // but decrease accuracy. |
| 46 | + // Available settings are: BME680_FILTER_SIZE_1, BME680_FILTER_SIZE_3, |
| 47 | + // BME680_FILTER_SIZE_7, BME680_FILTER_SIZE_15, BME680_FILTER_SIZE_31. |
| 48 | + bme680_configure(mySensor, BME680_SETTING_FILTER_SIZE, BME680_FILTER_SIZE_15); |
| 49 | + |
| 50 | + // Set the oversample (number of samples taken per measurement) |
| 51 | + // for the temperature sensor. |
| 52 | + // Available settings are: BME680_OS_1X, BME680_OS_2X, BME680_OS_4X |
| 53 | + // BME680_OS_8X, BME680_OS_16X. |
| 54 | + bme680_configure(mySensor, BME680_SETTING_TEMP_SAMPLES, BME680_OS_16X); |
| 55 | + |
| 56 | + // Set the oversample (number of samples taken per measurement) |
| 57 | + // for the pressure sensor. |
| 58 | + // Available settings are: BME680_OS_1X, BME680_OS_2X, BME680_OS_4X |
| 59 | + // BME680_OS_8X, BME680_OS_16X. |
| 60 | + bme680_configure(mySensor, BME680_SETTING_PRESSURE_SAMPLES, BME680_OS_8X); |
| 61 | + |
| 62 | + // Set the oversample (number of samples taken per measurement) |
| 63 | + // for the humidity sensor. |
| 64 | + // Available settings are: BME680_OS_1X, BME680_OS_2X, BME680_OS_4X |
| 65 | + // BME680_OS_8X, BME680_OS_16X. |
| 66 | + bme680_configure(mySensor, BME680_SETTING_HUMIDITY_SAMPLES, BME680_OS_4X); |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + while(1) { |
| 71 | + |
| 72 | + // Read the sensor |
| 73 | + char rslt = bme680_readSensor(mySensor); |
| 74 | + |
| 75 | + // Make sure the reading is available and valid |
| 76 | + if (rslt >= 0) { |
| 77 | + // Print the last read temaperature in CELSIUS (FAHRENHEIT and KELVIN are also options) |
| 78 | + print("Temperature: %.2f deg C\r", bme680_temperature(mySensor, CELSIUS)); |
| 79 | + |
| 80 | + // Print the last read pressure in hPa (PASCALS divided by 100), (INHG, MMHG, and PSI are also options) |
| 81 | + print("Pressure: %.2f hPa\r", bme680_pressure(mySensor, PASCALS) / 100.0); |
| 82 | + |
| 83 | + // Print an estimate of the altitude, based on the last read pressure |
| 84 | + // in METERS, (FEET are also options) |
| 85 | + print("Altitude: %.2f m\r", bme680_altitude(mySensor, METERS)); |
| 86 | + |
| 87 | + // Print the last read relative humidity |
| 88 | + print("Humidity: %.2f%%\r", bme680_humidity(mySensor)); |
| 89 | + |
| 90 | + // Print the last read gas sensor resistance value in kOhm (returns Ohms, then divide by 100) |
| 91 | + print("Gas Resistance: %.2f kOhms\r\r", bme680_gasResistance(mySensor) / 1000.0); |
| 92 | + |
| 93 | + } else { |
| 94 | + print("No reading available\r\r"); |
| 95 | + } |
| 96 | + |
| 97 | + // Wait 3 seconds before taking another reading. |
| 98 | + // Reading the sensor more frequently with the gas |
| 99 | + // sensor's heating element ON can cause the |
| 100 | + // temperature and humidity readings to be skewed. |
| 101 | + pause(3000); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +/** |
| 107 | + * TERMS OF USE: MIT License |
| 108 | + * |
| 109 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 110 | + * copy of this software and associated documentation files (the "Software"), |
| 111 | + * to deal in the Software without restriction, including without limitation |
| 112 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 113 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 114 | + * Software is furnished to do so, subject to the following conditions: |
| 115 | + * |
| 116 | + * The above copyright notice and this permission notice shall be included in |
| 117 | + * all copies or substantial portions of the Software. |
| 118 | + * |
| 119 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 120 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 121 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 122 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 123 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 124 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 125 | + * DEALINGS IN THE SOFTWARE. |
| 126 | + */ |
0 commit comments