I didn’t post my complete code because it’s over 1000 lines and the most of it is unrelevant to my problem.
I removed everything unrelevant to temp measurements from my code and tried this minimized code which appears to have the same issue.
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <EEPROM.h>
#define ON 1
#define OFF 0
float temperature = 0;
float target = 22.0;
int error = 0;
int heating = OFF;
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long previousLCDMillis;
Adafruit_BME280 bme;
const int relay = 5;
const int backlightPin = 6;
U8G2_ST7565_64128N_F_4W_SW_SPI u8g2(
U8G2_MIRROR,
/* clock=*/ 13,
/* data=*/ 11,
/* cs=*/ 10,
/* dc=*/ 9,
/* reset=*/ 8);
void setup(void) {
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
pinMode(backlightPin, OUTPUT);
digitalWrite(backlightPin, HIGH);
u8g2.begin();
u8g2.setContrast(170);
bool status;
status = bme.begin(0x76); //The I2C address of the sensor I use is 0x76
bme.setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF,
Adafruit_BME280::STANDBY_MS_1000);
currentMillis = millis();
}
void loop() {
currentMillis = millis();
if (!error){
if ( ! heating ){
if (target > temperature){
heatingON();
}
}
else{
if (target < temperature){
heatingOFF();
}
}
if ((unsigned long)(currentMillis - previousMillis) >= 2000) {
previousMillis = currentMillis;
readBME280();
}
}
else heatingOFF();
if ((unsigned long)(currentMillis - previousLCDMillis) >= 200) {
previousLCDMillis = currentMillis;
updateLCD();
}
}
void heatingON(){
heating = ON;
digitalWrite(relay, LOW);
}
void heatingOFF(){
heating = OFF;
digitalWrite(relay, HIGH);
}
void readBME280(){
bme.takeForcedMeasurement();
temperature = bme.readTemperature();
if (!((temperature >= 0) && (temperature <= 35))) error = 1;
}
void updateLCD(){
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_freedoomr25_tn);
u8g2.setFontMode(1);
u8g2.setDrawColor(1);
if (!error){
u8g2.setCursor(2,29);
u8g2.print(temperature,1);
u8g2.setCursor(2,63);
u8g2.print(target,1);
}
else{
u8g2.setCursor(2,29);
u8g2.print(temperature,1);
u8g2.drawStr(2,63,"ERROR!");
}
u8g2.sendBuffer();
}
I’m pretty sure that -214748368.0 is a minned out value but I don’t know how the Adafruit_BME280 library reads data from my sensor and why it returns that minned out value.
I’d like to know if it could be a hardware related issue. Could this mean that it doesn’t find a connected sensor?