Temp sensor inquiry for code

So I have connected the BME680 Sensor to my 2560 Mega and have been wondering about certain sensor readings. Im fairly new to coding and I know there are formulas but im not sure how to go about converting certain values to different values.

Lets say I wanted to change the temperature reading im getting from Celsuis to Kelvin, and the pressure reading from hPa to kPa.

This is what I have in my code which is a basic BME680 library. Any help would be very much appreciated!

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"

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

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME680 bme; // I2C
//Adafruit_BME680 bme(BME_CS); // hardware SPI
//Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);


void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println(F("BME680 Readings"));

  if (!bme.begin()) {
    Serial.println("UNABLE TO FIND BME680, RECHECK CONNECTIONS PLEASE!");
    while (1);
  }


  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms


 
}

void loop() {
  if (! bme.performReading()) {
    Serial.println("Reading Failure :(");
    return;
  }
 
  Serial.print("Temperature = ");
  Serial.print(bme.temperature);
  Serial.println(" *C");
  
   
  Serial.print("Pressure = ");
  Serial.print(bme.pressure / 100.0);
  Serial.println(" hPa");

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

  Serial.print("Gas = ");
  Serial.print(bme.gas_resistance / 1000.0);
  Serial.println(" KOhms");

  Serial.print("Approx. Altitude = ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");

  Serial.println();
  delay(2000);
}

Just read basic physics. Any technical handbook has the answer. Subtract 272.15 from Celsius and You have Kelvin.......

Here is how I do temperature conversion. In floating point for F to C subtract 32 then multiply by 1.8. To do it the other way again in floating point. Take the C temperature and multiply it by 1.8 then add 32. for the pressure conversion it is a ratio of 10, either multiply or divide by 10 depending on which way you want to convert. 1 hPa = 0.1 kPa or 10 kPa = 1 hPa. the difference in C to absolute zero is 272.15 so 0C = 272.15 K.

this part I understand. It's putting it into code that I'm lost.

  Serial.print("Temperature = ");
  Serial.print(bme.temperature + 273.15);
  Serial.println(" *K");
  Serial.print("Pressure = ");
  Serial.print(bme.pressure / 1000.0);
  Serial.println(" kPa");

Leo..

this worked great! thank you! Im new to this so I was playing around with numbers but Im glad to see how simple it really is! thank you!!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.