Inconsistent values MKR1400, bme280

Hi,
My first arduino project

Arduino MKR1400
BME280
Analog pressure sensor
powered by a 6.6Ah Li-ion

I am having some inconsistent sensor readings from the bme280.
The BME280 example code is working, so i think something in my code is not right(probably alot)

only the bme280 is connected at the moment.

this is some lines from the serial monitor:

00:56:26.596 → Temperature = nan °C
00:56:26.596 → Pressure = nan hPa
00:56:26.596 → Humidity = 100.00 %
-
00:56:35.093 → Temperature = -144.96 °C
00:56:35.093 → Pressure = 114683.02 hPa
00:56:35.093 → Humidity = nan %

Any help is appreciated :slight_smile:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL********"
#define BLYNK_DEVICE_NAME "Trykksensor 1"

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10


#include <MKRGSM.h>
#include <BlynkSimpleMKRGSM.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

Adafruit_BME280 bme; // I2C
float pressure;     //To store the barometric pressure (Pa)
float temperature;  //To store the temperature (oC)
float humidity;

GSMClient client;
GPRS gprs;
GSM gsmAccess;
BlynkTimer timer;

char auth[] = "********************";

char pin[] = "";
char apn[]  = "vpn.telia.no";
char user[] = "";
char pass[] = "";



void sendAnalog()
{
 // int sensorData = analogRead(A1); //reading the sensor on A1
float sensorData =(float)analogRead(A1)/1024.00*6.00;
  Blynk.virtualWrite(V5,sensorData); //sending to Blynk
  Serial.println(sensorData);
  Serial.println();
}


void ReadSensors(){
      pressure = bme.readPressure();
      temperature = bme.readTemperature();
      humidity = bme.readHumidity ();


      Blynk.virtualWrite(V1, pressure/100);
      Blynk.virtualWrite(V2, temperature);
      Blynk.virtualWrite(V6, humidity);
}     


void printValues() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" °C");

    Serial.print("Pressure = ");
    Serial.print(bme.readPressure());
    Serial.println(" hPa");


    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
}


void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, gsmAccess, gprs, client, pin, apn,  user, pass);
timer.setInterval(2000L, sendAnalog);
timer.setInterval(1003L, printValues);
timer.setInterval(1007L, ReadSensors); 
  

Serial.println(F("BME280 test"));

    unsigned status;
    
    // default settings
    status = bme.begin();  
    // You can also pass in a Wire library object like &Wire2
    // status = bme.begin(0x76, &Wire2)
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
        Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
        Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
        Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
        Serial.print("        ID of 0x60 represents a BME 280.\n");
        Serial.print("        ID of 0x61 represents a BME 680.\n");
        while (1) delay(10);
    }
    
    Serial.println("-- Default Test --");

    Serial.println();
}



void loop()
{
  //this start the blynk utilities and make run the iteration with the app
  Blynk.run();
  timer.run();
}

Your printValues function is taking one-off readings of temperature, pressure and humidity, and sending these directly to the serial monitor.

4ms later, you are taking the same readings, but this time storing the results in the global variables you’ve created.

In itself, this doesn’t explain the serial results you are seeing, unless you’re running into timing issues, but what you should be doing is this…

void ReadSensors()
{
  pressure = bme.readPressure();
  temperature = bme.readTemperature();
  humidity = bme.readHumidity ();

   Blynk.virtualWrite(V1, pressure/100);
   Blynk.virtualWrite(V2, temperature);
   Blynk.virtualWrite(V6, humidity);

   Serial.print("Temperature = ");
   Serial.print(temperature);
   Serial.println(" °C");

    Serial.print("Pressure = ");
    Serial.print(pressure/100);
    Serial.println(" kPa");

    Serial.print("Humidity = ");
    Serial.print(humidity);
    Serial.println(" %");

    Serial.println();
}

And removing the printValues function and it’s associated timer.

Also, is this hardware connected via a breadboard?

Pete.

Double check all your wiring connections.

The sensor is connected on a breadboard, so the wiring should be correct.
3,3V on VIN.
tested bme280test now. getting consistent values.

18:07:04.861 → Temperature = 25.97 °C
18:07:04.861 → Pressure = 990.67 hPa
18:07:04.861 → Approx. Altitude = 189.72 m
18:07:04.861 → Humidity = 40.50 %

Breadboards are very bad at giving consistent connections.
The contacts below each breadboard hole can easily be damaged, or become corroded or dirty, giving a high resistance connection.

If you’re getting this type of problem then I’d suggest soldering up the connections with patch wires rather than using the breadboard.

Pete.

I got everything soldered together, its much better now!

I am also having some problems with the analog sensor, the voltage from the sensor is unstable and output 0.14-0.25V at 0Psi

Used a voltage divider from 5V to 3.3V to A1 on the board
not sure if this is the most accurate.

The analog sensor has a 12V supply from a second li-ion battery
would it be best to run the sensor from the same battery as the arduino?
it needs at least 9V

im also having some issues with the widgeds on the web dashboard, but i will make another topic about that.

Thanks!

Well, that’s not going to work!

Using batteries isn’t going to work well with an analog sensor, as the values will most likely change as the supply voltage changes, but it does depend to a degree on your sensor and what internal electronics it has.

Pete.