I recently tried to store data from a BMP280 sensor to an SD card but I seem to keep running into the same issue. My SD card initializes with(chipSelect is 10):
if (!SD.begin(chipSelect))
{
Serial.println("SD Card Init Failed. Womp Womp D:.");
while(1);
}
It also manages to open a file:
dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println("File Opened:");
Serial.println("SD card accessed(the file)!");
}
else
{
Serial.println("Error opening the sd card file :/");
}
dataFile.close();
Serial.println("Init SD card closed.");
Although, when I get to my loop function in order to open a file again(to store sensor data), the file fails to open:
dataFile = SD.open("data.txt", FILE_WRITE); // Opens the file
//Checks if file has opened
if (dataFile)
{
Serial.println("File has been opened sucessfully :D.");
//Write to file:
dataFile.println("---------------------------------------------------------------\n");
//Temperature
dataFile.print("Temperature = ");
dataFile.print(temperature);
dataFile.println(" C");
//Pressure
dataFile.print("Pressure = ");
dataFile.print(pressure);
dataFile.println(" hPa");
dataFile.close(); //CLOSES THE FILEPATH
Serial.println("Closed File(loop)");
}
else
{
Serial.println("Something is off... I can smell it in the air.");
}
What could this be? I am using a WaveShare microSD card, and an Arduino Uno.
Here is my full sketch(as well as wiring at the end):
//BMP280 Code
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
//SD Card Reader
#include <SPI.h>
#include <SD.h>
//BMP 280
Adafruit_BMP280 bmp;
//SD card readed
const int chipSelect = 10; //Pin num connected to CS
File dataFile;
//Buzzer
const int buzzerPin = 9;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Serial began");
//Buzzer
pinMode(buzzerPin, OUTPUT);
//BMP280
if (!bmp.begin(0x76)) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
//while (1);
}
//SD card reader
//pinMode(chipSelect, OUTPUT);
//SD card init
if (!SD.begin(chipSelect))
{
Serial.println("SD Card Init Failed. Womp Womp D:.");
while(1);
}
Serial.println("SD card initialized YAYAYAYYAYAYAYA :D");
//OPEN FIILLEEEE
dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println("File Opened:");
Serial.println("SD card accessed(the file)!");
}
else
{
Serial.println("Error opening the sd card file :/");
}
dataFile.close();
Serial.println("Init SD card closed.");
}
void loop() {
//BMP280 start
Serial.println("BMP280 Sensor Readings:");
float temperature = bmp.readTemperature();
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" °C");
float pressure = bmp.readPressure() / 100.0F; // hPa
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.println();
//BMP280 END
//SD Card Reader
dataFile = SD.open("data.txt", FILE_WRITE); // Opens the file
//Checks if file has opened
if (dataFile)
{
Serial.println("File has been opened sucessfully :D.");
//Write to file:
dataFile.println("---------------------------------------------------------------\n");
//Temperature
dataFile.print("Temperature = ");
dataFile.print(temperature);
dataFile.println(" C");
//Pressure
dataFile.print("Pressure = ");
dataFile.print(pressure);
dataFile.println(" hPa");
dataFile.close(); //CLOSES THE FILEPATH
Serial.println("Closed File(loop)");
}
else
{
Serial.println("Something is off... I can smell it in the air.");
}
//Buzzer sounds every 5 seconds
//tone(buzzerPin, 1000);
//delay(1000);
//noTone(buzzerPin);
delay(5000);
}
(The circuit also has a BMP280 and a buzzer)
Is there something wrong with the wiring?


