BMP280 is giving wrong pressure and temperature data

Hey there, I'm creating a project that needs to measure humidity, temperature, and air pressure, and write that data to an SD card. I'm pretty new to Arduino so forgive me if I make some mistakes, or my code looks bad.

The data I'm getting for the BMP280 sensor is incorrect, giving me negative pressures and quite toasty temperatures (186 degrees Celcius!). When I run the example code from the Adafruit BMP280 library on the same circuit, it gives me correct readings, which makes me think this is a software problem.

Could this be from having to share the SCK, MISO, MOSI pins?

Thanks in advance!



#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);                      
}

I had an issue with an SD card and another device on the same bus. The issue is the SD card(s). I found (even the Adafruit) does not put the MISO (data output) of the SD card in a high impedance condition when SD device is not enabled. (also called Tri-State mode).

On my board I had to make a modification.

  1. unsolder pin 13
  2. lift it off the copper pad it was soldered to
  3. place a jumper from pin 13 to pin 8 (leave 8 soldered to its pad).
    Not for the person just learning how to solder. Need steady hands and a magnifying glass.

What this does is connect the enable pin that was originally grounded (meaning it would always set MISO as an output) to the Chip Select pin resulting in MISO going (open) when you are not writing or reading the SD card.

1 Like

Thanks so much for the fast reply!

I'm using the Sparkfun microSD Transflash Breakout, and I don't think I see the pins you are referring to.

If this ends up being a hardware limit, I will get an OpenLog.

I had a similar card holder. It has no buffer so you are connecting directly to the SD card contacts. The SD card DOES NOT set MISO open when it is not selected.

You have to add a buffer or use something else.

If you get and use the OpenLog board successfully please post your results I would be interested.

John

1 Like

If the pictures you posted are correct then you appear to be trying to use 3.3V logic level devices, the SD card and the BMP280 direct on a 5V Arduino.

So things might not work at all well.

Easiest to use a 3.3V logic level Arduino.

As for 'sharing' the SPI bus, then the SD Card would be using hardware SPI on the appropriate pins but this line of code for the BMP280;

Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK); 

Suggests that the Adafruit librqary is using software SPI on the same pins as the SD card is using hardware SPI, not good.

1 Like

Hey John,

I ended up getting the Sparkfun OpenLog, as it seems like it wasn't worth the headaches of trying to figure out how to "share" the SPI bus (thanks srnet, this saved me a lot of time!). The OpenLog works great, all you need to do is power it and connect it to the TX pin (1) on the Arduino, and it will log characters sent to the serial monitor while powered on!

Heres the circut I ended up with:

Heres the data I get:
image

1 Like

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