# SD data logger SD.open() fails

**URL:** <https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540>\
**Category:** Programming\
**Created:** [May 6, 2021, 5:59pm UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540 "2021-05-06T17:59:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicomm](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nicomm](https://forum.arduino.cc/u/nicomm)\
**Post date:** [May 6, 2021, 5:59pm UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/1 "2021-05-06T17:59:34Z")

</div>

Hi everyone,

Arduino drives me a bit crazy these days. I have built a thing which can measure temperature, humidity of air and of soil and air pressure. I added a I2C Display and it connects via wifi to my router to catch time via NTP. It works fine. I wanted to add a data logger function for my measurements to save them to a SD-card. The simple Arduino example sketch works fine to me. However my own sketch works too, but nothing was saved to my SD-card and it says: "error opening datalog.txt". So this means the "SD.open" function fails. But why?

#include \<LiquidCrystal\_I2C.h\>  
#include \<SPI.h\>  
#include \<SD.h\>  
#include \<DHT.h\>  
#include \<Wire.h\>  
//Pressure Sensor  
#include \<Adafruit\_BMP085.h\>  
#include \<WiFi101.h\>  
#include "arduino\_secrets.h"  
//modified library  
#include \<NTPClient.h\>  
#include \<WiFiUdp.h\>

#define DHTPIN 8  
#define DHTTYPE DHT22  
#define SHUMSENSOR A0

DHT dht(DHTPIN, DHTTYPE);  
LiquidCrystal\_I2C lcd(0x3F, 20, 4);  
Adafruit\_BMP085 bmp;  
WiFiClient Client;

const int chipSelect = 4;  
int status = WL\_IDLE\_STATUS;

WiFiUDP ntpUDP;  
NTPClient timeClient(ntpUDP);

void setup() {  
bmp.begin();  
dht.begin();  
Serial.begin(9600);  
lcd.init();  
lcd.backlight();  
analogReadResolution(12);  
while(status != WL\_CONNECTED) {  
Serial.print("Verbinde zu SSID: ");  
Serial.println(ssid);  
status = WiFi.begin(ssid, pass);  
delay(10000);  
}  
Serial.println("Verbunden!");

timeClient.begin();  
// Set offset time in seconds to adjust for your timezone, for example:  
// GMT +1 = 3600  
// GMT +8 = 28800  
// GMT -1 = -3600  
// GMT 0 = 0  
timeClient.setTimeOffset(7200);

while (!Serial);  
Serial.print("Initializing SD card...");  
if (!SD.begin(chipSelect)) {  
Serial.println("initialization failed. Things to check:");  
Serial.println("1. is a card inserted?");  
Serial.println("2. is your wiring correct?");  
Serial.println("3. did you change the chipSelect pin to match your shield or module?");  
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");  
while (true);  
}  
Serial.println("initialization done.");  
}

void loop() {  
delay(2000);  
//functions for LCD  
readTempHum();  
readPress();  
readSHum();  
writeSD();

while(!timeClient.update()) {  
timeClient.forceUpdate();  
}

Serial.print(timeClient.getFormattedDate());  
Serial.print(", ");  
Serial.print(timeClient.getFormattedTime());  
Serial.print(", ");  
Serial.print(dht.readTemperature(), 1);  
Serial.print(" °C, ");  
Serial.print(dht.readHumidity(), 1);  
Serial.print(" hPa, ");  
Serial.println(analogRead(SHUMSENSOR));  
}

void readTempHum() {  
lcd.setCursor(0, 0);  
lcd.print("Temperatur: ");  
lcd.setCursor(0, 1);  
lcd.print("Luftfeuchte: ");  
lcd.setCursor(13, 0);  
lcd.print(dht.readTemperature(), 1);  
lcd.setCursor(18, 0);  
lcd.write(0xDF);  
lcd.print("C");  
lcd.setCursor(13, 1);  
lcd.print(dht.readHumidity(), 1);  
lcd.setCursor(17, 1);  
lcd.print("%");  
}

void readPress() {  
lcd.setCursor(0, 2);  
lcd.print("Luftdruck: ");  
lcd.setCursor(13, 2);  
lcd.print(bmp.readPressure() / 100);  
lcd.print(" hPa");  
}

void readSHum() {  
lcd.setCursor(0, 3);  
lcd.print("Bodenfeuchte: ");  
lcd.setCursor(14, 3);  
int bodenFeuchte = analogRead(SHUMSENSOR);  
lcd.print(bodenFeuchte);  
}

void writeSD() {  
File dataFile = SD.open("datalog.txt", FILE\_WRITE);  
// if the file is available, write to it:  
if (dataFile) {  
dataFile.print(timeClient.getFormattedDate());  
dataFile.print(", ");  
dataFile.print(timeClient.getFormattedTime());  
dataFile.print(", ");  
dataFile.print(dht.readTemperature(), 1);  
dataFile.print(" °C, ");  
dataFile.print(dht.readHumidity(), 1);  
dataFile.print(" hPa, ");  
dataFile.println(analogRead(SHUMSENSOR));  
dataFile.close();  
}  
// if the file isn't open, pop up an error:  
else {  
Serial.println("error opening datalog.txt");  
}  
}

Here a nicer view: [#include \<LiquidCrystal\_I2C.h\>#include \<SPI.h\>#include \<SD.h\>#include \<DHT - Pastebin.com](https://pastebin.com/Axr7mFjh)

Please help me, I tried many things, but nothing worked. 🥺

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 6, 2021, 6:01pm UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/2 "2021-05-06T18:01:26Z")

</div>

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

---

<div class="post-metadata">

**Author:** ![drmpf](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drmpf/32/1126438_2.png) [@drmpf](https://forum.arduino.cc/u/drmpf)\
**Post date:** [May 7, 2021, 2:30am UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/3 "2021-05-07T02:30:36Z")

</div>

Try changing

```cpp
"datalog.txt"

```

to

```cpp
"/datalog.txt"

```

---

<div class="post-metadata">

**Author:** ![nicomm](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nicomm](https://forum.arduino.cc/u/nicomm)\
**Post date:** [May 7, 2021, 9:23am UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/4 "2021-05-07T09:23:55Z")

</div>

```cpp
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <DHT.h>
#include <Wire.h>
//Pressure Sensor
#include <Adafruit_BMP085.h>
#include <WiFi101.h>
#include "arduino_secrets.h"
//modified library
#include <NTPClient.h>
#include <WiFiUdp.h>

#define DHTPIN 8
#define DHTTYPE DHT22
#define SHUMSENSOR A0

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x3F, 20, 4);
Adafruit_BMP085 bmp;
WiFiClient Client;

const int chipSelect = 4;
int status = WL_IDLE_STATUS;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

void setup() {
  bmp.begin();
  dht.begin();
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  analogReadResolution(12);
  while(status != WL_CONNECTED) {
    Serial.print("Verbinde zu SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  Serial.println("Verbunden!");

  timeClient.begin();
  // Set offset time in seconds to adjust for your timezone, for example:
  // GMT +1 = 3600
  // GMT +8 = 28800
  // GMT -1 = -3600
  // GMT 0 = 0
  timeClient.setTimeOffset(7200);

  while (!Serial);
    Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("1. is a card inserted?");
    Serial.println("2. is your wiring correct?");
    Serial.println("3. did you change the chipSelect pin to match your shield or module?");
    Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
    while (true);
  }
  Serial.println("initialization done.");
}

void loop() {
  delay(2000);
  //functions for LCD
  readTempHum();
  readPress();
  readSHum();
  writeSD();
  
  while(!timeClient.update()) {
    timeClient.forceUpdate();
  }
  
  Serial.print(timeClient.getFormattedDate());
  Serial.print(", ");
  Serial.print(timeClient.getFormattedTime());
  Serial.print(", ");
  Serial.print(dht.readTemperature(), 1);
  Serial.print(" °C, ");
  Serial.print(dht.readHumidity(), 1);
  Serial.print(" hPa, ");
  Serial.println(analogRead(SHUMSENSOR));
}

void readTempHum() {
  lcd.setCursor(0, 0);
  lcd.print("Temperatur: ");
  lcd.setCursor(0, 1);
  lcd.print("Luftfeuchte: ");
  lcd.setCursor(13, 0);
  lcd.print(dht.readTemperature(), 1);
  lcd.setCursor(18, 0);
  lcd.write(0xDF);
  lcd.print("C");
  lcd.setCursor(13, 1);
  lcd.print(dht.readHumidity(), 1);
  lcd.setCursor(17, 1);
  lcd.print("%"); 
}

void readPress() {
  lcd.setCursor(0, 2);
  lcd.print("Luftdruck: ");
  lcd.setCursor(13, 2);
  lcd.print(bmp.readPressure() / 100);
  lcd.print(" hPa");
}

void readSHum() {
  lcd.setCursor(0, 3);
  lcd.print("Bodenfeuchte: ");
  lcd.setCursor(14, 3);
  int bodenFeuchte = analogRead(SHUMSENSOR);
  lcd.print(bodenFeuchte); 
}

void writeSD() {
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print(timeClient.getFormattedDate());
    dataFile.print(", ");
    dataFile.print(timeClient.getFormattedTime());
    dataFile.print(", ");
    dataFile.print(dht.readTemperature(), 1);
    dataFile.print(" °C, ");
    dataFile.print(dht.readHumidity(), 1);
    dataFile.print(" hPa, ");
    dataFile.println(analogRead(SHUMSENSOR));
    dataFile.close();
  }
    // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

```

---

<div class="post-metadata">

**Author:** ![nicomm](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nicomm](https://forum.arduino.cc/u/nicomm)\
**Post date:** [May 7, 2021, 9:26am UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/5 "2021-05-07T09:26:26Z")

</div>

No effect, but thanks for your answer!

---

<div class="post-metadata">

**Author:** ![srnet](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/srnet/32/238868_2.png) [@srnet](https://forum.arduino.cc/u/srnet)\
**Post date:** [May 7, 2021, 9:27am UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/6 "2021-05-07T09:27:29Z")

</div>

Also, when troubleshooting a problem with one device, the SD card in this case, strip back the program so that all it does is test the SD card.

So in this case remove all the DHT, Display, WiFi etc.

Then test the SD again.

---

<div class="post-metadata">

**Author:** ![Nick\_Pyner](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nick\_Pyner](https://forum.arduino.cc/u/Nick_Pyner)\
**Post date:** [May 7, 2021, 2:56pm UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/7 "2021-05-07T14:56:56Z")

</div>

Opening and closing the SD each time you go round the loop is bad practice. It may be OK to start with, but usually gives you grief in the long run. I don't know if this is your problem, but you may have done something that makes it effect you sooner rather than later.

---

<div class="post-metadata">

**Author:** ![nicomm](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nicomm](https://forum.arduino.cc/u/nicomm)\
**Post date:** [May 7, 2021, 6:59pm UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/8 "2021-05-07T18:59:41Z")

</div>

I figured out that the DHT22-Sensor is responsible with the trouble.  
As soon I stripped everything out off the code which has to do with the DHT, it works.  
But why? I want to see temperature and humidity on my LCD and write it to my SD-card.

---

<div class="post-metadata">

**Author:** ![nicomm](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nicomm](https://forum.arduino.cc/u/nicomm)\
**Post date:** [May 7, 2021, 7:01pm UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/9 "2021-05-07T19:01:54Z")

</div>

Okay, I can open the file in the void setup(), but when do I have to call the close() function?  
I want that the SD-card became written since the measurements are done.

---

<div class="post-metadata">

**Author:** ![nicomm](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nicomm](https://forum.arduino.cc/u/nicomm)\
**Post date:** [May 8, 2021, 10:11am UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/10 "2021-05-08T10:11:53Z")

</div>

Wow the problem solved! I changed the DHT-pin from pin 6 to pin 8. I stored the solution of the measurement globally in a float and that's it. It works fine.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 5, 2021, 10:11am UTC](https://forum.arduino.cc/t/sd-data-logger-sd-open-fails/858540/11 "2021-09-05T10:11:59Z")

</div>

Closed on September 5, 2021, 10:11am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
