T3N0N

BME680 & Waveshare 2.9 - I2C & SPI

Jul 20th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.94 KB | Software | 0 0
  1. // Arduino BME680 & Waveshare 2.9 e-paper - SPI and I2C
  2.  
  3. //#include <SPI.h>
  4. #include <Wire.h>
  5. #include <GxEPD2_BW.h>
  6. #include <Fonts/FreeMonoBold9pt7b.h>
  7. #include <Adafruit_Sensor.h>
  8. #include "Adafruit_BME680.h"
  9.  
  10.  
  11.  
  12. // SPI pin mapping (ESP-WROOM-32)
  13. //#define SCK   18
  14. //#define MISO  19
  15. //#define MOSI  23
  16. #define BME_CS    0
  17. #define DISP_CS   5
  18. #define DISP_DC   17
  19. #define DISP_RST  16
  20. #define DISP_BUSY 4
  21.  
  22. #define SEALEVELPRESSURE_HPA (1013.25)
  23.  
  24. // Display driver
  25. GxEPD2_BW<GxEPD2_290_T94_V2, GxEPD2_290_T94_V2::HEIGHT> display(
  26.   GxEPD2_290_T94_V2(DISP_CS, DISP_DC, DISP_RST, DISP_BUSY)
  27. );
  28.  
  29.  
  30. Adafruit_BME680 bme; // I2C
  31. //Adafruit_BME680 bme(BME_CS, MOSI, MISO, SCK); // Software SPI
  32.  
  33. bool sensor_error = false, status;
  34. float temperature, humidity, pressure, gas, alt;
  35.  
  36.  
  37. void selectSensor() {
  38.   digitalWrite(DISP_CS, HIGH);
  39.   digitalWrite(BME_CS, LOW);
  40.   sensor_error = false;
  41. }
  42.  
  43. void selectDisplay() {
  44.   digitalWrite(BME_CS, HIGH);
  45.   digitalWrite(DISP_CS, LOW);
  46. }
  47.  
  48. // Read BME data
  49. void readValues() {
  50.   selectSensor();
  51.   if (!bme.performReading()) {
  52.     sensor_error = true;
  53.     Serial.println("Error reading from sensor!");
  54.     return;
  55.   }
  56.   delay(400);
  57.   temperature = bme.temperature;
  58.   humidity    = bme.humidity;
  59.   pressure    = bme.pressure;
  60.   alt         = bme.readAltitude(SEALEVELPRESSURE_HPA);
  61.  
  62.   Serial.print("Sensor values: ");
  63.   Serial.print(temperature); Serial.print(" °C, ");
  64.   Serial.print(humidity);    Serial.print(" %, ");
  65.   Serial.print(pressure);    Serial.println(" Pa");
  66. }
  67.  
  68. // Update display
  69. void displayValues(int16_t x, int16_t y, int16_t sep) {
  70.   //if (sensor_error) return; -- disabled for debugging
  71.   selectDisplay();
  72.   if (!sensor_error) {
  73.     display.setRotation(1);
  74.     display.setFont(&FreeMonoBold9pt7b);
  75.     display.setTextColor(GxEPD_BLACK);
  76.     display.setFullWindow();
  77.     display.firstPage();
  78.     do {
  79.       display.fillScreen(GxEPD_WHITE);
  80.       display.setCursor(x, y);
  81.       display.print("T: ");  display.print(temperature, 1); display.println(" °C");
  82.       display.setCursor(x, y + sep);
  83.       display.print("H: ");  display.print(humidity, 1);    display.println(" %");
  84.       display.setCursor(x, y + sep*2);
  85.       display.print("P: ");  display.print(pressure / 100.0, 1); display.println(" hPa");
  86.       display.setCursor(x, y + sep*3);
  87.       display.print("Alt: "); display.print(alt, 1);        display.println(" m");
  88.       display.setCursor(x, y + sep*4);
  89.     } while (display.nextPage());
  90.  
  91.     Serial.println("Display updated."); }
  92.   else {
  93.     display.setRotation(1);
  94.     display.setFont(&FreeMonoBold9pt7b);
  95.     display.setTextColor(GxEPD_BLACK);
  96.     display.setFullWindow();
  97.     display.firstPage();
  98.     do {
  99.       display.fillScreen(GxEPD_WHITE);
  100.       display.setCursor(x, y);
  101.       display.print("Communication error!");
  102.     } while (display.nextPage());
  103.   }
  104. }
  105.  
  106.  
  107. // Setup
  108. void setup() {
  109.   Serial.begin(115200);
  110.   delay(100);
  111.  
  112.   // Start SPI manually
  113.   SPI.begin();
  114.  
  115.  
  116.   // Configure CS pins
  117.   pinMode(BME_CS, OUTPUT);
  118.   pinMode(DISP_CS, OUTPUT);
  119.   digitalWrite(BME_CS, HIGH);
  120.   digitalWrite(DISP_CS, HIGH);
  121.  
  122.   // Initialize display
  123.   selectDisplay();
  124.   display.init(115200, true, 2, false);
  125.  
  126.   // Clear display once
  127.   display.setRotation(1);
  128.   display.setFullWindow();
  129.   display.firstPage();
  130.   do {
  131.     display.fillScreen(GxEPD_WHITE);
  132.   } while (display.nextPage());
  133.  
  134.   // Initialize sensor
  135.   selectSensor();
  136.   status = bme.begin();
  137.   if (!status) {
  138.     Serial.println("BME680 not found!");
  139.   }
  140.  
  141.   bme.setTemperatureOversampling(BME680_OS_4X);
  142.   bme.setHumidityOversampling(BME680_OS_2X);
  143.   bme.setPressureOversampling(BME680_OS_4X);
  144.   bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  145.  
  146.   Serial.println("Setup complete.");
  147.   delay(2000);
  148. }
  149.  
  150. void loop() {
  151.   Serial.println("\nNew cycle...");
  152.   readValues();
  153.   displayValues(20, 20, 20);
  154.   Serial.println("Waiting 15 seconds...\n");
  155.   delay(15000);
  156. }
Advertisement
Add Comment
Please, Sign In to add comment