M0 Adalogger and BME 280

Hello,
I´m trying to use a Feather M0 Adalogger to collect data from a BME280. So I´m using SPI, because the I2C doesn´t work.
For this Project I have this sketch:


#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK SCK
#define BME_MISO MISO
#define BME_MOSI MOSI
#define BME_CS 0
#define SEALEVELPRESSURE_HPA (1013.25)

//Adafruit_BME280 bme; //I2C
**//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI**
**Adafruit_BME280 bme(BME_CS); // hardware SPI**

File myFile;
String Temperature, Pressure, Altitude, Humidity;
String Data;

void setup() {
  Serial.begin(115200);
  //delay(2000);
  pinMode(0,OUTPUT);
  bool status;
  status = bme.begin();  
  if (!status) {
    Serial.println("BME280 not connected properly. Check circuit!");
    while (1); 
  }
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) // Num. = CS Pin 
  {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  myFile = SD.open("data.txt", FILE_WRITE);
  if (myFile) {
    myFile.println( "Temperature(°C), Pressure(hPa), Altitude(m), Humidity(%) \r\n");
    myFile.close();
  } 
  else {
    Serial.println("error opening data.txt");
  }
}


void loop() { 
  data_logging();                                                 
  delay(20000); 
}

void data_logging() {
  String Temperature = String(bme.readTemperature(),2);
  String Pressure = String(bme.readPressure()/ 100.0F,2);
  String Altitude = String(bme.readAltitude(SEALEVELPRESSURE_HPA),2);
  String Humidity = String(bme.readHumidity(),2);
  
  Data = Temperature + "," + Pressure + "," + Altitude + "," + Humidity ;
  Serial.print("Save data: ");
  Serial.println(Data);

  myFile = SD.open("data.txt", FILE_WRITE);
  if (myFile) {
    Serial.print("Writing to data.txt...");
    myFile.println(Data);
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening data.txt");
  }  
  Serial.println();
}

In the Line 15 and 16 at begining of the sketch: I use the "Adafruit_BME280 bme ..." (one Line for Software and one for Hardware.)
When I want to use the Line for Software, the Sensor give not the correct Data to the SD-Card.
When I use the Line for Hardware, I get the correct Data from the Sensor.
Where is the difference? Can someone explane?

pin 0 is reserved for serial comm ...

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