There is no text in the file after the writing

I have a code:

#include <SD.h>
#include "DHT.h"

#define DHTPIN 2 
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);
File myFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
   pinMode(10, OUTPUT);
dht.begin();

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

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

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }
    myFile = SD.open("example.txt", FILE_WRITE);
    for (int i = 0; i < 1000; i++) {
      int h = round(dht.readHumidity());
      int t = round(dht.readTemperature());
      myFile.print(t);
      myFile.print('-');
      myFile.println(h);
      Serial.println(i);
      delay(1000);  
    }
    myFile.close();
    Serial.print("done");
}



void loop() {
  
}

I want to write temperature and humidity from DHT22 to SD. In the serial there is all text (i from 0 to 999), but when programm is end when I open the file it is absolutely empty. What is the problem?

What Arduino do you have? Why are you specifying pin 4 in the sd.begin call? try SD.begin().

wildbill: What Arduino do you have? Why are you specifying pin 4 in the sd.begin call? try SD.begin().

I have UNO. About sd.begin... I actually don't know. I found that code in the web. So I'll try SD.begin().

So I’ll try SD.begin().

Without specifying the chip select pin, the SD library will default to using pin 10. If pin 4 were not the cs pin for your SD module, you would have gotten the “initialization failed” error.

I can not confirm your issue of an empty file when I run a simplified your program with an SD module which indeed uses pin 4 for cs. I can record 1000 values in set up with your code. I don’t have a DHT so I took dummy analog readings on A0 and A1.

Can you read and write to your card/module when using the SD.h library examples?

cattledog: Without specifying the chip select pin, the SD library will default to using pin 10. If pin 4 were not the cs pin for your SD module, you would have gotten the "initialization failed" error.

Yes, that is right.

cattledog: I can record 1000 values in set up with your code. I don't have a DHT so I took dummy analog readings on A0 and A1.

I tried to record only the var i (from 0 to 1000) into the SD. It is work. So maybe it is problem with DHT? What can be wrong with it?

cattledog: Can you read and write to your card/module when using the SD.h library examples?

Yes.

And more. If I record, for example, 100 values from the DHT then it is ok. It is very weirdness situation.

You have not said which DHT library you are using, but the Adafruit library (which it looks like you are using) disables interrupts for a dht read. That can be in conflict with the spi transfer to the SD card.

Try using the "SimpleDHT" by Winlin library available through the library manager. It does not disable interrupts.

There is also another library for the DHT by Rob Tillart which does not disable interrupts but the installation is not available with the library manager.https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib

I downloaded SimpleDHT. Examples work. Now I have a code:

/*
CS to pin 4
MOSI to pin 11
SCK to pin 13
MISO to pin 12 
*/
#include <SimpleDHT.h>
#include <SD.h>

File myFile;

int pinDHT22 = 2;
SimpleDHT22 dht22;

void setup() {
  Serial.begin(9600);
  pinMode(10, OUTPUT);

  float temperature = 0;
  float humidity = 0;
    
  Serial.print("Initializing SD card...");

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

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }
  
  myFile = SD.open("example.txt", FILE_WRITE);
  for (int i = 0; i < 7000; i++) {
    int err = SimpleDHTErrSuccess;
    if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
      Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
      return;
    }
    myFile.print(temperature);
    myFile.print('-');
    myFile.println(humidity);
    Serial.println(i);
    delay(2000);  
  }
  myFile.close();
  
  Serial.print("done");
}

void loop() {
}

But all the time in the serial there is a line “initialization failed!”. Could you explain to me why, please?

But all the time in the serial there is a line "initialization failed!". Could you explain to me why, please?

I do not have any ideas as to why you have this failure. Can you run the basic SD.h examples with the dht22 hooked up?

cattledog: Can you run the basic SD.h examples with the dht22 hooked up?

At first, no. But then I found out what was wrong. A SD module was connected to 3.3V pin. I reconnected it to 5V. And now code with DHT works well. It turns out that the SD did not have enough power.

I found out what was wrong. A SD module was connected to 3.3V pin. I reconnected it to 5V. And now code with DHT works well. It turns out that the SD did not have enough power.

Good job figuring out the problem. SD modules are quite power hungry and some can pull up to 200 ma during a write. People sometimes try and power them from IO pins which does not work.

Thanks for the help!