Problems writing data to SD card

The title is pretty self-explanatory, I've written code to take pressure and temperature data from a bmp280 pressure sensor, however with the code I've written, the serial monitor doesn't show either initialisation succeeding or failing, and it doesn't create a file either, I can't see a problem. Here is the code :

#include <SD.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
File myFile;

int ChipSelect = 10;

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)
Adafruit_BMP280 bmp;

void setup() {
  Serial.begin(9600);
  Serial.println(F("BMP280 test"));
  
  if (!bmp.begin()) {
    Serial.println(F("BMP280 not found"));
    while (1) delay(10);

    while (!Serial) {
}

   myFile = SD.open("test.txt", FILE_WRITE);
   if (myFile) {
    Serial.println("File is go");
   }
    pinMode (10, OUTPUT);
   if (SD.begin(10)){
    Serial.println("SD is go");
   }else{
    Serial.println("SD is a no-go");
    return;}
}
  /* 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("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");

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

    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1013.25));
    Serial.println(" m");
    
    Serial.println();
    
    myFile = SD.open("test.txt");
    if (myFile) {
      myFile.print(F("Pressure = "));
      myFile.print(bmp.readPressure());
      myFile.println(" Pa");
      
      myFile.print(F("Approx altitude = "));
      myFile.print(bmp.readAltitude(1013.25));
      myFile.println(" Pa");

      myFile.print(F("Temperature = "));
      myFile.print(bmp.readTemperature());
      myFile.println(" *C");
    }
    delay(2000);
}

If someone could help that would be most appreciated

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's unreadable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)


closing the file would help

I've done that and highlighted the code, I'm new to the forum so please forgive the clumsiness of the previous format.

I still can't find the problem.

What do you get on Serial?

Rearrange this:

Serial.println(F("BMP280 test"));
  
  if (!bmp.begin()) {
    Serial.println(F("BMP280 not found"));
    while (1) delay(10);

    while (!Serial) {
}

To this (otherwise you may not see any useful serial messages):

  Serial.begin(9600);
  while (!Serial);  //wait for serial connection to be established

  Serial.println(F("BMP280 test"));
  
  if (!bmp.begin()) {
    Serial.println(F("BMP280 not found"));
    while (1) delay(10);

As said already in #2 you keep opening files but never close…

Two items here
#1 you need to decide when you will close the file. For example at the end of the loop. (There are debates on how often and when to close a file)

   myFile.println(" *C");  
   myFile.close();
}

#2 You need to change the following line since the default mode is Read mode.
myFile = SD.open("test.txt");

Since your in a loop you will want to append the file instead of overwriting it

myFile = SD.open("test.txt", FILE_WRITE);

[note some libraries require FILE_APPEND instead of FILE_WRITE]

With those two changes, you will at least have a file show up hopefully.

I've amended that problem now, thanks

I've made that change and I still have nothing showing up related to the SD card on the serial monitor, or any new file showing up on the SD card.

@jremington @wildbill Thank you, I've made that change now but still the only thing showing up on the serial monitor is the ("BMP280 test") message

post the modified code... we don't know what you did

Which SD card adapter are you using? There is a problem with a large number of these adapters, particularly with the design labeled "Catalex", where the MISO line from the SD card is not released properly. This interferes with the use of SPI for any other device, which may be causing the BME280 library to lock up since you never see any of the Serial output after the initial "BME280 test" message.

Here is the modified code :

#include <SD.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
File myFile;

int ChipSelect = 10;

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)
Adafruit_BMP280 bmp;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println(F("BMP280 test"));

  if (!bmp.begin()) {
    Serial.println(F("BMP280 not found"));
    while (1) delay(10);

    myFile = SD.open("test.txt");
    if (myFile) {
      Serial.println("File is go");
    }
    pinMode (10, OUTPUT);
    if (SD.begin(10)) {
      Serial.println("SD is go");
    } else {
      Serial.println("SD is a no-go");
      return;
    }
  }
  /* 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("Temperature = "));
  Serial.print(bmp.readTemperature());
  Serial.println(" *C");

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

  Serial.print(F("Approx altitude = "));
  Serial.print(bmp.readAltitude(1013.25));
  Serial.println(" m");

  Serial.println();

  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(F("Pressure = "));
    myFile.print(bmp.readPressure());
    myFile.println(" Pa");

    myFile.print(F("Approx altitude = "));
    myFile.print(bmp.readAltitude(1013.25));
    myFile.println(" Pa");

    myFile.print(F("Temperature = "));
    myFile.print(bmp.readTemperature());
    myFile.println(" *C");
    myFile.close();
  }
  delay(2000);
}

In the setup you try to play with a file before the if (SD.begin(10)) {

    myFile = SD.open("test.txt");
    if (myFile) {
      Serial.println("File is go");
    }

and you did not close the file.

doing a return upon an error in the setup will get you to the loop. you don't wan't that probably

OK so on that advice I've changed that part of the code so it now looks like this:

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println(F("BMP280 test"));

  if (!bmp.begin()) {
    Serial.println(F("BMP280 not found"));
    while (1) delay(10);

    pinMode (10, OUTPUT);
    if (SD.begin(10)) {
      Serial.println("SD is go");
    } else {
      Serial.println("SD is a no-go");
      return;
    }

    myFile = SD.open("test.txt");
    if (myFile) {
      Serial.println("File is go");
    } else {
      Serial.println("File has Failed");
    }
    myFile.close();

  }

I have now closed the file at the end of the setup and the loop, however I'm still not getting anything

The setup code for the SD is within this if… :roll_eyes:

Thank you for pointing that out, that was really stupid of me and I can't believe I didn't spot that before, Now everything is working perfectly

Thank you for your patience.

We all have done stupid mistakes, don’t feel bad about it!

Have fun

1 Like

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