I have a problem with my SD card module.
Sometimes when I read serial print I get an error and I need to reset the arduino but even then when I reset it does not want to operate. It prints " Card failed to start or not present ". What I do is I want to remove the card when I like and then insert it back to continue reading data. Code is below:
// Configuration for DHT Sensor
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11 // Select DHT sensor type
DHT dht(DHTPIN, DHTTYPE);
// Read the humidity of the DHT11
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Configuration for RTC Module
// DS1302 RST -> Arduino Digital 7
// DS1302 DATA -> Arduino Digital 6
// DS1302 CLK -> Arduino Digital 5
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(6, 7, 8);
// Configuration SD card module
// SD card attached to SPI bus as follows:
// MOSI - pin 11
// MISO - pin 12
// CLK - pin 13
// CS - pin 4
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4; // Chip select pin for selecting SD card module
// Temperature sensors
float Temp_1;
float Temp_2;
// Delay
const unsigned long interval = 60000 ; // 15 minutes is 900000 milliseconds for testing purposes 1 min interval
unsigned long previous = 0 ;
const unsigned long intervala = 1000 ;
unsigned long previousa = 0 ;
// Fan and heater
const int HEATER = 10; // Heater to pin 10
const int FAN = 9; // Fan to pin 9
void setup () {
Serial.begin(9600); // Baud rate of 9600
pinMode(chipSelect, OUTPUT); // Set chip select as output
// Heater and fan
pinMode(HEATER,OUTPUT); // Set heater as output
pinMode(FAN,OUTPUT); // Set fan as output
digitalWrite(HEATER,LOW); // Initial stage to heater OFF
digitalWrite(FAN,LOW); // Initial stage to fan OFF
// Setting the time of the RTC module
// myRTC.setDS1302Time(00, 38, 12, 6, 28, 06, 2019); // Initial set
Serial.println(F("Beehive data logger"));
Serial.println();
while (!Serial) {
}
Serial.print("Starting SD card...");
if (!SD.begin(chipSelect)) { // If SD module not working
Serial.println("Card failed to start or not present");
while (1);
}
Serial.println(" Card is now operational!"); // If SD module working
Serial.println();
dht.begin(); // Begin the DHT function
File dataFile = SD.open("beehive.txt", FILE_WRITE); // Being to write in beehive.txt
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on serial terminal
delay(100);
Serial.print("Date "); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Time "); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Humidity in %"); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Temp_DHT in °C"); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Temp_1 in °C"); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Temp_2 in °C"); // Print header name for column
Serial.println(); // Insert a new line
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on SD card
dataFile.print("Date "); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print(",");
dataFile.print("Time "); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print(",");
dataFile.print("Humidity in %"); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print(",");
dataFile.print("Temp_DHT in °C"); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print(",");
dataFile.print("Temp_1 in °C"); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print(",");
dataFile.print("Temp_2 in °C"); // Print header name for column
dataFile.println(); // Insert a new line
dataFile.close();
}
void loop() {
unsigned long current = millis(); // Setting delay for saving data on SD card
unsigned long currenta = millis(); // Setting delay for reading data for serial monitor
if(current - previous >= interval) {
File dataFile = SD.open("beehive.txt", FILE_WRITE);
// This allows for the update of variables for time or accessing the individual elements. //
myRTC.updateTime();
// Read the humidity of the DHT11
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
Temp_1 = analogRead(A0)*5/1023.0;
Temp_1 = Temp_1 - 0.5;
Temp_1 = Temp_1 / 0.01;
Temp_1 = Temp_1 - 5.0;
Temp_2 = analogRead(A1)*5/1023.0;
Temp_2 = Temp_2 - 0.5;
Temp_2 = Temp_2 / 0.01;
Temp_2 = Temp_2 - 5.0;
if (dataFile){ // If data is now ready to be written
// Save to SD card
dataFile.print("");
dataFile.print(myRTC.dayofmonth);
dataFile.print("/");
dataFile.print(myRTC.month);
dataFile.print("/");
dataFile.print(myRTC.year);
dataFile.print(",");
dataFile.print(" ");
dataFile.print(myRTC.hours);
dataFile.print(":");
dataFile.print(myRTC.minutes);
dataFile.print(":");
dataFile.print(myRTC.seconds);
dataFile.print(",");
dataFile.print(" \t");
dataFile.print(F(""));
dataFile.print(h);
dataFile.print(F("% "));
dataFile.print(",");
dataFile.print(t);
dataFile.print(" \t");
dataFile.print(",");
dataFile.print(Temp_1);
dataFile.print(" \t");
dataFile.print(",");
dataFile.print(Temp_2);
dataFile.print(" \t");
dataFile.println();
dataFile.close();
// Print to serial
Serial.print("");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.print(myRTC.seconds);
Serial.print(" \t");
Serial.print(F(""));
Serial.print(h);
Serial.print(F("% \t"));
Serial.print(t);
Serial.print(" \t"); // Create tab/space
Serial.print(Temp_1); // Print temperature from temperature sensor 1
Serial.print(" \t"); // Create tab/space
Serial.print(Temp_2); // Print temperature from temperature sensor 2
Serial.print(" \t"); // Create tab/space
Serial.println();
}
else {
Serial.println("There has been an error saving data");
}
previous = current ; // Delay
}
// Temperature and humidity control
if ( currenta - previousa >= intervala) {
// Read the humidity of the DHT11
float h = dht.readHumidity();
Temp_1 = analogRead(A0)*5/1023.0;
Temp_1 = Temp_1 - 0.5;
Temp_1 = Temp_1 / 0.01;
Temp_1 = Temp_1 - 5.0;
Serial.print(h); // Print to serial the current humidity
Serial.print(F("% \t"));
}