Advertisement
T3N0N

Arduino - BME680 SPI Test Example

Jul 17th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.39 KB | Software | 0 0
  1. // Arduino - BME680 SPI Test Example
  2.  
  3. /***************************************************************************
  4.   This is a library for the BME680 gas, humidity, temperature & pressure sensor
  5.  
  6.   Designed specifically to work with the Adafruit BME680 Breakout
  7.   ----> http://www.adafruit.com/products/3660
  8.  
  9.   These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  10.   to interface.
  11.  
  12.   Adafruit invests time and resources providing this open source code,
  13.   please support Adafruit and open-source hardware by purchasing products
  14.   from Adafruit!
  15.  
  16.   Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  17.   BSD license, all text above must be included in any redistribution
  18.  ***************************************************************************/
  19.  
  20. #include <Wire.h>
  21. #include <SPI.h>
  22. #include <Adafruit_Sensor.h>
  23. #include "Adafruit_BME680.h"
  24.  
  25. #define BME_SCK 18
  26. #define BME_MISO 19
  27. #define BME_MOSI 23
  28. #define BME_CS 21
  29.  
  30. #define SEALEVELPRESSURE_HPA (1013.25)
  31.  
  32. //Adafruit_BME680 bme(&Wire); // I2C
  33. //Adafruit_BME680 bme(&Wire1); // example of I2C on another bus
  34. //Adafruit_BME680 bme(BME_CS); // hardware SPI
  35. Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);
  36.  
  37. void setup() {
  38.   Serial.begin(9600);
  39.   while (!Serial);
  40.   Serial.println(F("BME680 test"));
  41.   Serial.println();
  42.  
  43.   if (!bme.begin()) {
  44.     Serial.println("Could not find a valid BME680 sensor, check wiring!");
  45.     while (1);
  46.   }
  47.  
  48.   // Set up oversampling and filter initialization
  49.   bme.setTemperatureOversampling(BME680_OS_8X);
  50.   bme.setHumidityOversampling(BME680_OS_8X);
  51.   bme.setPressureOversampling(BME680_OS_8X);
  52.   bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  53.   bme.setGasHeater(320, 150); // 320*C for 150 ms
  54. }
  55.  
  56. void loop() {
  57.   if (! bme.performReading()) {
  58.     Serial.println("Failed to perform reading :(");
  59.     return;
  60.   }
  61.   Serial.print("Temperature = ");
  62.   Serial.print(bme.temperature);
  63.   Serial.println(" *C");
  64.  
  65.   Serial.print("Pressure = ");
  66.   Serial.print(bme.pressure / 100.0);
  67.   Serial.println(" hPa");
  68.  
  69.   Serial.print("Humidity = ");
  70.   Serial.print(bme.humidity);
  71.   Serial.println(" %");
  72.  
  73.   Serial.print("Gas = ");
  74.   Serial.print(bme.gas_resistance / 1000.0);
  75.   Serial.println(" KOhms");
  76.  
  77.   Serial.print("Approx. Altitude = ");
  78.   Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  79.   Serial.println(" m");
  80.  
  81.   Serial.println();
  82.   delay(10000);
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement