Advertisement
T3N0N

bme.begin() issue

Jul 18th, 2025 (edited)
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.15 KB | Software | 0 0
  1. // Arduino BME680 & Waveshare 2.9 epaper via SPI - NOT WORKING?!
  2.  
  3. #include <SPI.h>
  4. #include <GxEPD2_BW.h>
  5. #include <Fonts/FreeMonoBold9pt7b.h>
  6. #include <Adafruit_Sensor.h>
  7. #include "Adafruit_BME680.h"
  8.  
  9.  
  10. // SPI pin mapping (ESP32)
  11. #define BME_SCK   18
  12. #define BME_MISO  19
  13. #define BME_MOSI  23
  14. #define BME_CS    21
  15. #define DISP_CS   5
  16. #define DISP_DC   17
  17. #define DISP_RST  16
  18. #define DISP_BUSY 4
  19.  
  20. #define SEALEVELPRESSURE_HPA (1013.25)
  21.  
  22. // Display driver
  23. GxEPD2_BW<GxEPD2_290_T94_V2, GxEPD2_290_T94_V2::HEIGHT> display(
  24.   GxEPD2_290_T94_V2(DISP_CS, DISP_DC, DISP_RST, DISP_BUSY)
  25. );
  26.  
  27.  
  28. // BME680 via SPI
  29. Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
  30.  
  31. bool sensor_error = false;
  32. float temperature, humidity, pressure, gas, alt;
  33.  
  34.  
  35. // Chip select switching
  36. void selectSensor() {
  37.   digitalWrite(DISP_CS, HIGH);
  38.   digitalWrite(BME_CS, LOW);
  39.   sensor_error = false;
  40. }
  41.  
  42. void selectDisplay() {
  43.   bme.endReading();
  44.   digitalWrite(BME_CS, HIGH);
  45.   digitalWrite(DISP_CS, LOW);
  46. }
  47.  
  48. // Read sensor 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.   //bme.endReading();
  58.   temperature = random(-15, 40);
  59.   humidity    = random(0, 100);
  60.   pressure    = random(0, 100);
  61.   alt         = random(100, 400);
  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;
  71.   selectDisplay();
  72.  
  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, 2); display.println(" hPa");
  86.     display.setCursor(x, y + sep*3);
  87.     display.print("Alt: "); display.print(alt, 1);        display.println(" m");
  88.   } while (display.nextPage());
  89.  
  90.   Serial.println("Display updated.");
  91. }
  92.  
  93.  
  94. // Setup
  95. void setup() {
  96.   Serial.begin(115200);
  97.   delay(100);
  98.  
  99.   // Start SPI manually
  100.   SPI.begin(BME_SCK, BME_MISO, BME_MOSI);
  101.  
  102.   // Configure CS pins
  103.   pinMode(BME_CS, OUTPUT);
  104.   pinMode(DISP_CS, OUTPUT);
  105.   digitalWrite(BME_CS, HIGH);
  106.   digitalWrite(DISP_CS, HIGH);
  107.  
  108.  
  109.   // Initialize display
  110.   selectDisplay();
  111.   display.init(115200, true, 2, false);
  112.  
  113.   // Clear display once
  114.   display.setRotation(1);
  115.   display.setFullWindow();
  116.   display.firstPage();
  117.   do {
  118.     display.fillScreen(GxEPD_WHITE);
  119.   } while (display.nextPage());
  120.  
  121. // Problems with initializing the sensor: as soon as it is enabled,
  122. // the display stops working. Without initializing the sensor, it doesn't provide any values
  123. // because it ends up in "Error reading from sensor!".
  124. // Maybe initialize the sensor before initializing the display? -> Tried it, not working...
  125.  
  126. // The problem is bme.begin(). I thought it was the combination of bme.begin() and bme.performReading()
  127. // because bme.performReading() is never stopped. Tried using bme.endReading(), but it doesn't help.
  128.  
  129. // The display only works if bme.begin() is commented out.
  130.  
  131.   // Initialize sensor
  132.   selectSensor();
  133.   //bme.begin(); // Thats the problem!!!  
  134.     //Serial.println("BME680 not found!");
  135.     //while (1);
  136.   //}
  137.  
  138.   //bme.setTemperatureOversampling(BME680_OS_4X);
  139.   //bme.setHumidityOversampling(BME680_OS_2X);
  140.   //bme.setPressureOversampling(BME680_OS_4X);
  141.   //bme.setIIRFilterSize(BME680_FILTER_SIZE_3);  
  142.  
  143.   Serial.println("Setup complete.");
  144.   delay(2000);
  145. }
  146.  
  147. // Loop
  148. void loop() {
  149.   Serial.println("\nNew cycle...");
  150.   readValues();
  151.   displayValues(20, 20, 20);
  152.   Serial.println("Waiting 30 seconds...\n");
  153.   delay(30000);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement