Hi
I'm new to Arduino and i was playing around with setting up a temp monitor using a BME280 and an SD card logger with an RTC https://www.jaycar.com.au/arduino-compatible-data-logging-shield/p/XC4536
The problem i am having is getting the SD card to initialize, it will work sometimes but more often than not it wont work.
There seems to be no reason for it to work or not work, i plug it in it will say initialization failed, i unplug it then plug it back in and it works. Unplug it to check the data on the sd card, plug the sd card back in then plug the usb cable in and it will say initialization failed, unplug and replug several times and it still wont work, leave it for a while plug it in and will work again.
I have tried several sd cards and i have formatted them with the sd card formatting tool but it seems to make no difference. i have posted my code that i have pieced together from other peoples examples i have found.
#include <Adafruit_BME280.h>
#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC
// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 10;
// Define object bme280
Adafruit_BME280 BME280 = Adafruit_BME280();
// Create a file to store the data
File myFile;
// RTC
RTC_DS1307 rtc;
void setup() {
pinMode(10, OUTPUT); // change this to 53 if you are using arduino mega
digitalWrite(10, HIGH); // Add this line
// Initialize Temp & Humid Sensor
BME280.begin();
//initializing Serial monitor
Serial.begin(9600);
// setup for the RTC
while(!Serial); // for Leonardo/Micro/Zero
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
}
if(! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
// setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//open file
myFile=SD.open("DATA.txt", FILE_WRITE);
// if the file opened ok, write to it:
if (myFile) {
Serial.println("File opened ok");
// print the headings for our data
myFile.println("Date,Time,Temperature ºC");
}
myFile.close();
}
void loggingTime() {
DateTime now = rtc.now();
myFile = SD.open("DATA.txt", FILE_WRITE);
if (myFile) {
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(',');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(",");
}
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.println(now.day(), DEC);
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
myFile.close();
delay(1000);
}
void loggingTemperature() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius
float t = BME280.readTemperature();
// Read temperature as Fahrenheit
//float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(t) /*|| isnan(f)*/) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//debugging purposes
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
//Serial.print(f);
//Serial.println(" *F\t");
myFile = SD.open("DATA.txt", FILE_WRITE);
if (myFile) {
Serial.println("open with success");
myFile.print(t);
myFile.println(",");
}
myFile.close();
}
void loop() {
loggingTime();
loggingTemperature();
delay(900000);
}