BMP280 pressure with if condition

Hi guys, i’m trying to use a BMP280 with LED and want to add if condition with pressure, but it is not working ..can you please suggest how to fix this.

My code is:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)
const int ledPin =  13;  
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);


  if (!bmp.begin(0x76)) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void loop() {


    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure()/100);
    Serial.println(" Pa");
    Serial.print(pressure); //  mm Mercury
float pressure = bmp.readPressure() * 0.00750061683;

  if ((pressure * 0.00750061683) >981.70)
  {
    // Pressure too high
    digitalWrite(ledPin, LOW);
  }
    
if ((pressure * 0.00750061683) <981.70)
  {  // turn LED off:
    digitalWrite(ledPin, HIGH);
  }
}

What does [quote="abhixs, post:1, topic:1043269"]but it is not working[/quote] mean?

What is this supposed to do float pressure = bmp.readPressure() * 0.00750061683; and why do this pressure * 0.00750061683 in the if?

Which Arduino board are you using ?
How precise do you expect a float to be when using that board ?

Why are you multiplying pressure by 0.00750061683 and why are you doing it twice ?

I am using Arduino nano.
it can be precise between 4-5 digit

by mistake i used 0.00750061683 twice, i was looking some examples on google and i copy pasted code from there. I don't have so much idea about this sensor.

So, post your updated code and tell us what you mean by "not working"

I don't suppose that you have tried printing the value that you are testing to see whether it looks reasonable, or have you ?

Hello
Run this tutorial to gain the knowledge.

If condition for LED is not working, i mean when pressure is <981.70 led is not getting on and when pressure is >981.70 led is not getting off.

Updated Code:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)
const int ledPin =  13;  
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);


  if (!bmp.begin(0x76)) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void loop() {


    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure()/100);
    Serial.println(" Pa");

float pressure = bmp.readPressure() * 0.00750061683;

  if ((pressure) >981.70)
  {
    // Pressure too high
    digitalWrite(ledPin, LOW);
  }
    
if ((pressure) <981.70)
  {  // turn LED off:
    digitalWrite(ledPin, HIGH);

}}

You are still not printing the value that you are testing and have not said what "not working" means

Please help us to help you

Why is the pressure reading being multiplied by 0.00750061683?

Have you done a

float pressure = bmp.readPressure() * 0.00750061683;
Serial.println( pressure );
  if ((pressure) >981.70)

to see what the result is?

this value i am getting printed

981.70*0.00750061683 = 7.363355542011, quite strange. Also, the LED will not come on. Try removing the *0.00750061683 part.

I've not ever experienced a BMP or BME giving such stable pressure readings as shown in post#10.

Thank you so much, this solved my problem ..thanks a lot again :slight_smile:

1 Like

What solved your problem?

This solved my problem :slight_smile:

float pressure = bmp.readPressure() * 0.00750061683;
Serial.println( pressure );
  if ((pressure) >981.70)

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