Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BMP280 gives wrong pressure and temperature data

    BMP280 gives wrong pressure and temperature data

    Detour
    New Poster

    Hello, I'm working on a project that requires measuring humidity, temperature, and air pressure and writing the data to an SD card.
    I'm new to Arduino, so please forgive me if I make any mistakes or my code looks sloppy.


    The data from the BMP280 sensor is incorrect, displaying negative pressures and quite warm temperatures (186 degrees Celsius!).
    When I run the example code from the Adafruit BMP280 library on the same circuit, it produces correct readings, leading me to believe that this is a software issue.


    Could it be due to having to share the SCK, MISO, and MOSI pins?


    Thank you ahead of time!

     

    #include <SD.h> //Library for SD card reader.
    
    #include "DHT.h"  //Libraries for the DHT22 sensor.
    #define DHTPIN 2
    #define DHTTYPE DHT22
    
    DHT dht(DHTPIN, DHTTYPE);
    
    #include <Wire.h>  //Libraries for the BMP280 sensor.
    #include <SPI.h>
    #include <Adafruit_BMP280.h>
    
    #define BMP_SCK  (13)
    #define BMP_MISO (12)
    #define BMP_MOSI (11)
    #define BMP_CS   (5) //BMP280 CS pin.
    
    Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK); 
    
    
    
    File myFile;
    const int chipSelect = 4;  //SD card reader CS pin.
    
    void setup()
    {
    pinMode(7, OUTPUT); //For LED and buzzer.
      Serial.begin(9600);
      digitalWrite(7, LOW);
      Serial.println(F("BMP280 test"));
      if (!bmp.begin(5)) {
        Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                         "try a different address!"));
        while (1) delay(10);
      }
    
      /* 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. */
    
    
    
      Serial.print("Initializing SD card...");
      // Note that even if it's not used as the CS pin, the hardware SS pin
      // (10 on most Arduino boards, 53 on the Mega) must be left as an output
      // or the SD library functions will not work.
      pinMode(10, OUTPUT);
      dht.begin();
    
      if (!SD.begin(4)) {
        Serial.println("initialization failed!");
        digitalWrite(7, HIGH);
        return;
      }
      Serial.println("initialization done.");
    
      // open the file. note that only one file can be open at a time,
      // so you have to close this one before opening another.
      myFile = SD.open("humi.txt", FILE_WRITE);
    
      // if the file opened okay, write to it:
      if (myFile) {
        Serial.print("Writing to humi.txt...");
        float humi  = dht.readHumidity();
        // read temperature as Celsius
        float tempC = dht.readTemperature();
        // read temperature as Fahrenheit
        float tempF = dht.readTemperature(true);
    
        // check if any reads failed
        if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
          Serial.println("Failed to read from DHT sensor!");
          digitalWrite(7, HIGH);
        } else {
          myFile.print("Humidity: "); //Write data from DHT22 sensor.
          myFile.print(humi);
          myFile.print("%");
    
          myFile.print("  |  ");
    
          myFile.print("Temperature: ");
          myFile.print(tempC);
          myFile.print("°C ~ ");
          myFile.print(tempF);
          myFile.println("°F");
    
          delay(1000);
          myFile.print(F("Temperature = "));  //Write data from BMP280
          myFile.print(bmp.readTemperature());
          myFile.println(" *C");
    
          myFile.print(F("Pressure = "));
          myFile.print(bmp.readPressure());
          myFile.println(" Pa");
    
          myFile.print(F("Approx altitude = "));
          myFile.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
          myFile.println(" m");
    
          myFile.println();
        }
    
        // close the file:
        myFile.close();
        Serial.println("done.");
      } else {
        // if the file didn't open, print an error:
        Serial.println("error opening humi.txt");
      }
    
      // re-open the file for reading:
      myFile = SD.open("humi.txt");
      if (myFile) {
        Serial.println("humi.txt:");
    
        // read from the file until there's nothing else in it:
        while (myFile.available()) {
          Serial.write(myFile.read());
        }
        // close the file:
        myFile.close();
      } else {
        // if the file didn't open, print an error:
        Serial.println("error opening humi.txt");
      }
    }
    
    
    void loop()  //Flashes LED and turns buzzer on/off.
    {
      digitalWrite(7, HIGH);   
      delay(1000);                      
      digitalWrite(7, LOW);    
      delay(1000);                      
    }

     

    3 REPLIES 3

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Detour,

    Sorry, I know little about Arduino.
    Our reference code was written by C language on github: https://github.com/BoschSensortec/BMP280_driver/tree/master/examples
    You could refer sensor API and exmaple how to read sensor data. referring the procedure of example, you could migrate it your platform.
    SPI pins can be shared and used without affecting data reading.

    I see. Thank you!

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Detour,

    You could refer to the process of sensor API on Github, and check whether the Arduino code is correct?

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist