Why Arduino writes at most 150-180 entries on SD card? (I need at least 720)

Hello there :slight_smile:

I wanted to create a little weather station lately, it works pretty fine for the moment except one thing.

The setup is Arduino UNO, Ethernet Shield with SD slot, SD card, Barometer, Humiture Sensor, and Photosensor (wires and breadboard :))), I wanted to take readings every minute.

The problem is that Arduino only writes first 150-180 entries to the card and stops for some reason.

I cannot find the reson, so I ask your help, my friends :slight_smile:

Here is the code:

//for humiture sensor
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
//for barometer
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
//for SD card
#include <SD.h>

#define DHTPIN            2         // Pin which is connected to the DHT sensor (humiture).

#define BMP_SCK 13                  //barometer
#define BMP_MISO 12                 //barometer
#define BMP_MOSI 11                 //barometer
#define BMP_CS 10                   //barometer

File myFile;                        //SD card

const int photocellPin = A0;        //photosensor
int outputValue = 0;                //photosensor

//for humiture sensor
#define DHTTYPE           DHT11     // DHT 11 
//for barometer
Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
  Serial.begin(9600);
  // Initialize HUMITURE device.
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);
  // Set delay between sensor readings based on sensor details.
  //delayMS = sensor.min_delay / 1000;
  delayMS = 60000; //in miliseconds
  //initialize BAROMETER
  if (!bme.begin()) {
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }

  //SD SETUP
  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
}

void loop() {
  Serial.print("Humiture sensor values\n");
  Serial.print("************************************\n");
  // Delay between measurements.
  //delay(delayMS);
  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println("Error reading temperature!");
  }
  else {
    Serial.print("Temperature: ");
    Serial.print(event.temperature - 1);
    Serial.println(" *C");
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println("Error reading humidity!");
  }
  else {
    Serial.print("Humidity: ");
    Serial.print(event.relative_humidity + 20);
    Serial.println("%");
  }

  // BAROMETER
  Serial.print("Barometer values\n");
  Serial.print("************************************\n");
  Serial.print("Temperature = ");
  Serial.print(bme.readTemperature() - 4.22);
  Serial.println(" *C");

  Serial.print("Pressure = ");
  Serial.print(bme.readPressure()+4339.15);
  Serial.println(" Pa");

  Serial.print("Approx altitude = ");
  Serial.print(bme.readAltitude(1018.00)); // this should be adjusted to your local forcast
  Serial.println(" m");

  Serial.println();
  //delay(delayMS);

  //PHOTOSENSOR
  Serial.print("Photosensor values\n");
  Serial.print("************************************\n");
  outputValue = analogRead(photocellPin);
  Serial.print("Brightness = ");
  Serial.println(outputValue);
  Serial.print("_______________________________________\n");

  //SD card
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.print("H_temp = ");
    myFile.print(event.temperature - 1);
    myFile.print(" ");

    myFile.print("H_humi = ");
    myFile.print(event.relative_humidity +22);
    myFile.print(" ");

    myFile.print("B_temp = ");
    myFile.print(bme.readTemperature() - 3);
    myFile.print(" ");

    myFile.print("B_pres = ");
    myFile.print(bme.readPressure() + 4339.15);
    myFile.print(" ");

    myFile.print("B_elev= ");
    myFile.print(bme.readAltitude(1018.00));
    myFile.print(" ");

    myFile.print("P_valu= ");
    myFile.print(outputValue);
    myFile.println(" ");
    
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  delay(delayMS);
}

How much free RAM is reported after compiling?

I cannot find the reson, so I ask your help, my friends

Did you get this warning when compliling;

'Low memory available, stability problems may occur'

Lucario448:
How much free RAM is reported after compiling?

Here is the output after the complilation and upload

Sketch uses 21862 bytes (8%) of program storage space. Maximum is 253952 bytes.
Global variables use 1482 bytes (18%) of dynamic memory, leaving 6710 bytes for local variables. Maximum is 8192 bytes.

neotba:
Here is the output after the complilation and upload

Sketch uses 21862 bytes (8%) of program storage space. [b]Maximum is 253952 bytes[/b].

Global variables use 1482 bytes (18%) of dynamic memory, leaving 6710 bytes for local variables. Maximum is 8192 bytes.

That suggests your using an ATmega2560, but your first post said;

The setup is Arduino UNO

My bad, sorry for that

Maybe try doubling data written and see if the lockup happens noticeably faster.