BMP280 gives wrong pressure and temperature data

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