Not saving data on SD card and error

Hello all,

This is my code

/*
 * File:   main.c
 * Company: RIOU Glass
 * Author: Mateusz WINTER
 * Project: This project is to monitor temperature and humidity
 * in a beehive by logging and saving data on SD card
 * 
 */

// Configuration for DHT Sensor

 #include "DHT.h"

 #define DHTPIN 2

 #define DHTTYPE DHT11

 DHT dht(DHTPIN, DHTTYPE);

 // Configuration for RTC Module

// DS1302 RST  -> Arduino Digital 7
// DS1302 DATA -> Arduino Digital 6
// DS1302 CLK  -> Arduino Digital 5

//#include <DS1302.h>
//
//DS1302 rtc(7 , 6, 5);
//
//Time t;

// 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;

// Temperature sensors

float Temp_1;
float Temp_2;

// Delay

unsigned long seconds = 1000L; // 1000ms to 1s
unsigned long minutes = seconds * 1; // 900s in 15 minutes

void setup () {
  Serial.begin(9600); // Baud rate of 9600

//  rtc.halt(false);
//  rtc.writeProtect(false);
//
//  rtc.setDOW(THURSDAY);
//  rtc.setTime(12, 0, 0);
//  rtc.setDate(6, 8, 2010);
  
  Serial.println(F("Beehive data logger"));
  Serial.println();
  
  while (!Serial) {
  }
  Serial.print("Starting SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed to start or not present");
    while (1);
  }
  Serial.println("   Card is now operational!");
  Serial.println();
  dht.begin();
}

//}

void loop() {

  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  Temp_1 = analogRead(A0)*5/1023.0; // Read value from analog port A0 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_1 = Temp_1 - 0.5; 
  Temp_1 = Temp_1 / 0.01;
  Temp_2 = analogRead(A1)*5/1023.0; // Read value from analog port A1 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_2 = Temp_2 - 0.5;
  Temp_2 = Temp_2 / 0.01;
  
//  t = rtc.getTime();
  File dataFile = SD.open("testing.txt", FILE_WRITE);

  if (dataFile){

// Print to serial

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  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();

// Save to SD card

  dataFile.print(F("Humidity: "));
  dataFile.print(h);
  dataFile.print(F("%  Temperature: "));
  dataFile.print(t);
  dataFile.print("     \t"); // Create tab/space  
  dataFile.print(Temp_1); // Print temperature from temperature sensor 1
  dataFile.print("     \t"); // Create tab/space
  dataFile.print(Temp_2); // Print temperature from temperature sensor 2
  dataFile.print("     \t"); // Create tab/space
  dataFile.println();
  
  }

  else {
    Serial.println("There has been an error saving data");
  }

delay(2000);
  
}

This is the output I am getting

Beehive data logger

Starting SD card...   Card is now operational!

Humidity: 48.00%  Temperature: 26.60     	30.65     	30.16     	
Humidity: 48.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 48.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 49.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 51.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 51.00%  Temperature: 26.60     	29.67     	30.16     	
Humidity: 53.00%  Temperature: 26.60     	29.67     	30.16     	
Humidity: 53.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 52.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 51.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 51.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 50.00%  Temperature: 26.60     	29.67     	30.16     	
Humidity: 50.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 50.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 50.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 49.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 49.00%  Temperature: 26.60     	29.67     	30.16     	
Humidity: 49.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 49.00%  Temperature: 26.60     	29.67     	30.16     	
Humidity: 49.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 48.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 48.00%  Temperature: 26.60     	30.16     	30.16     	
Humidity: 48.00%  Temperature: 26.60     	29.67     	30.16     	
Humidity: 48.00%  Temperature: 26.60     	29.67     	30.16     	
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data
There has been an error saving data

It creates the file but does not save the data, it also gets this error after a while and not sure what is going on.

Any help would be appreciated.

Thanks

On every pass through loop(), you open a file. I might have missed it, but I don't see where you ever close the file.

1 Like

I see yes how could I do it that it opens once and then saves continously?

Woule I put the open at the setup?

Woule I put the open at the setup?

You could, but I don't recommend that. Before you can remove the SD card from the Arduino, you have to close the file. You need to add some mechanism to control, then, whether you can write to the file, or not.

I would open the file, write to it, and close the file. Then, you can power down the Arduino at any time, and risk losing only the last record.

do not rely on the library to set the pin mode for CS. change:

void setup () {
  Serial.begin(9600); // Baud rate of 9600

to

void setup () {
  Serial.begin(9600); // Baud rate of 9600
pinMode(chipSelect, OUTPUT);