Arduino is not opening the file on SD card (from 2nd loop cycle)

Hi everyone,

I am working on a project to read T and RH and send the data on an SD card. The values have been also read on an LCD screen.
After the 2nd cycle of the loop, I have the message on the LCD "file not open". So the Arduino creates the reading.csv file but the file is actually empty on the SD card.

How is it possible that on the 1st cycle of the main loop the file is opening and then on the 2nd cycle is not open anymore?

Here is my code:

// Libraries
  #include <dht.h> // Includes the libraries for the sensor
  #include <LiquidCrystal.h> // Includes the libraries for the LCD
  #include <DS3231.h>; // Includes the libraries for the clock
  #include <SPI.h>; // Includes the libraries for the Serial Peripheral Interface that allows the Master (Arduino) to control a slave device
  #include <SD.h>; // Includes the libraries for the SD card
    
// Interface and Objects definitions
  DS3231 rtc(SDA, SCL); // Initialises the DS3231 using the hardware interface
  #define dataPin 2 // Defines pin number to which the sensor is connected (avoid D4-9 and A0 used by LCD)
  dht DHT; // Creates a DHT object
  LiquidCrystal lcd(8,9,4,5,6,7); // Creates a LCD LiquidCrystal object
  const int chipSelect = 10; // Assigns the pin for the SPI-SD chip selection
  
void setup() 
{
  pinMode(chipSelect, OUTPUT); // Ensures that the SPI-SD selection pin is an output
  rtc.begin(); // Initialises the rtc object
  // rtc.setDOW(TUESDAY); // Sets Day-of-Week to TUESDAY
  rtc.setTime(14, 54, 0); // Sets the time to 12:00:00 (24hr format)
  rtc.setDate(6, 1, 2017); // Sets the date to January 1st, 2017
  lcd.begin(16, 2); // Initialises the interface to the LCD screen, and specifies the dimensions
  lcd.print("Initialising SD");
  delay (2000);
  lcd.clear();
    if (!SD.begin(chipSelect)) 
    {
      lcd.println("SD failed!");
      return;
    }
    lcd.println("SD OK");
    delay(2000);
    lcd.clear();
  lcd.print("Reading sensor"); // Prints to the LCD
  delay (4000); // Waits 4 seconds
}

void loop() 
{
// Open the file on the SD card
  delay(2000);
  File readings = SD.open("readings.csv", FILE_WRITE); 

// Reading the data
  int readData = DHT.read22(dataPin); // Reads the data from the sensor
  float t = DHT.temperature; // Gets the values of the temperature
  float h = DHT.humidity; // Gets the values of the humidity

// Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t))
    {
      lcd.println("DHT FAIL!");
      return;
    }

// If the file opened OK, write to it on SD card
lcd.clear();
if (readings) 
  {
    lcd.print("file OPEN");
    delay(1000);
    readings.print(t);
    readings.print(",");
    readings.print(h);
    // close the file:
    readings.close();
    lcd.clear();
    lcd.println("file CLOSE");
    delay(1000);
  } 
  else 
  {
    // if the file didn't open, print an error:
    lcd.println("file not open");
    delay(4000);
  }

// Printing the results on the LCD screen with time and date
  delay(2000);
  lcd.clear(); // Clears the LCD screen and positions the cursor in the upper-left corner
  lcd.print(rtc.getDateStr()); // Prints date
  lcd.setCursor(8,1); // Moves the cursor on the second line of the LCD
  lcd.println(rtc.getTimeStr()); // Prints time
  delay (4000);  // Waits 4 seconds
  lcd.clear(); // Clears the LCD screen and positions the cursor in the upper-left corner
  char tempF[6]; // Defines a character variable
  char humF[6];
  dtostrf(t, 5, 1, tempF); // Converts a float to a char array so it can then be printed easily
  dtostrf(h, 2, 0, humF);
  lcd.print("T:"); lcd.print(tempF); lcd.print((char)223); lcd.print("C ");
  // NOTE: char(223) identifies the degree symbol
  lcd.print("H: "); lcd.print(humF); lcd.print("%");

delay(2000); // Delays 2 seconds, as the DHT22 minimum sampling rate is 0.5Hz
  
}

Of course, this post will also help me to discuss and improve my coding skill, considering that I am a completely new to the Arduino.
This is my very first project and I would like to thanks all the people that wrote articles and posts from where I took "bits" of my complete code

Thanks a lot for everyone's support.

Mario

The values have been also read on an LCD screen.

How do you READ values FROM an LCD screen?

  dtostrf(t, 5, 1, tempF); // Converts a float to a char array so it can then be printed easily
  dtostrf(h, 2, 0, humF);
  lcd.print("T:"); lcd.print(tempF); lcd.print((char)223); lcd.print("C ");
  // NOTE: char(223) identifies the degree symbol
  lcd.print("H: "); lcd.print(humF); lcd.print("%");

Why?

From LiquidCrystal.h:

class LiquidCrystal : public Print {

The Print class KNOWS how to print a float.

If you want humidity as an int, why are you storing the value in a float?

What kind of SD reader/writer do you have? How is it connected to the Arduino?

If you disconnect the LCD, do you still have problems writing to the SD card?

what's your LCD and how is it connected?

Thanks for the very quick replies!

When I am looking at the LCD screen this is what will happen:

  1. "Initialising SD" with the LCD screen becoming "less bright"
  2. "SD OK" with the brightness - from NOW ON - back.
  3. "Reading sensor"
  4. "file OPEN"
  5. "file CLOSE"
  6. Showing date and time
  7. showing values of T and RH
  8. "file not open"
  9. carry on with just showing date time and after T and RH as per points 6) and 7)

So I am bit surprised that the file OPENS but then the next cycle is not open anymore...

PaulS:
How do you READ values FROM an LCD screen?

 dtostrf(t, 5, 1, tempF); // Converts a float to a char array so it can then be printed easily

dtostrf(h, 2, 0, humF);
 lcd.print("T:"); lcd.print(tempF); lcd.print((char)223); lcd.print("C ");
 // NOTE: char(223) identifies the degree symbol
 lcd.print("H: "); lcd.print(humF); lcd.print("%");



Why? 

From LiquidCrystal.h:


class LiquidCrystal : public Print {



The Print class KNOWS how to print a float.

If you want humidity as an int, why are you storing the value in a float?

What kind of SD reader/writer do you have? How is it connected to the Arduino?

If you disconnect the LCD, do you still have problems writing to the SD card?

Sorry, I am not reading from the LCD screen, I am just showing the results on it in real time.

regarding using flat/int with LiquidCrystal I had the doubt too but I just take the hint from the following code/link:

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


#define DHTPIN 22 // what pin we're connected to

#define DHTTYPE DHT11 

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(8,9,4,5,6,7); 

void setup(void) {
  lcd.begin(16, 2);
  lcd.print("Reading sensor");
  dht.begin();
}

void loop() {
  
  float temperature, humidity;

  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  delay(2000); 

 lcd.clear();

 char tempF[6]; 
 char humF[6];
 dtostrf(temperature, 5, 1, tempF);
 dtostrf(humidity, 2, 0, humF);

 lcd.print("T:"); 
 lcd.print(tempF);
 lcd.print((char)223);
 lcd.print("C ");
 lcd.print("H: ");
 lcd.print(humF);
 lcd.print("%");
}

And I "trusted" it.

If you are telling me that I could get rid of everything there and just use t and h I will do. I would not like that DHT22 is providing a floating number and so if I put integer I will create "issues".

As SD writer/reader I have the following:
High Performance DIY SD Card Slot Reading Writing Module for Arduino

http://www.gearbest.com/development-boards/pp_125934.html?currency=GBP&lkid=10133909&gclid=Cj0KEQiA4JnCBRDQ5be3nKCPhpwBEiQAjwN1bqHs1QbsczW8mn0ejFzw_IsAj9Z0gWu6D9xQMKVAtmIaAln18P8HAQ

it is connected in this way:
• MOSI - pin 11 on Arduino Uno
• MISO - pin 12 on Arduino Uno
• CLK - pin 13 on Arduino Uno
• CS - pin 10 on Arduino Uno
GND to Arduino GND and VCC to Arduino +5v.

The LCD is a shield, I disconnected it and connect all the wires without it and the SD card had a reading.csv empty file as well.

J-M-L:
what's your LCD and how is it connected?

It is a shield bought here:
16x2 LCD Shield for Arduino

http://www.maplin.co.uk/p/16x2-lcd-shield-for-arduino-n07dh?cmpid=ppc&gclid=CIrr7Yq03dACFUSx7QodMCoDSw

I will know give you the whole system:

Arduino UNO R3 Development Board
http://www.maplin.co.uk/p/arduino-uno-r3-development-board-n30ku

16x2 LCD Shield for Arduino
http://www.maplin.co.uk/p/16x2-lcd-shield-for-arduino-n07dh?cmpid=ppc&gclid=CIrr7Yq03dACFUSx7QodMCoDSw

Breadboard and various Wire Kit

DS3231 RTC AT24C32 I2C Precision Real Time Clock & Memory Module Arduino
http://www.ebay.co.uk/itm/DS3231-RTC-AT24C32-I2C-Precision-Real-Time-Clock-Memory-Module-Arduino-UK-FAST-/400902900013

UUGear DHT22 (AM2302) Temperature & Humidity Sensor Module
http://www.uugear.com/product/uugear-dht22-temperature-humidity-sensor-module/

High Performance DIY SD Card Slot Reading Writing Module for Arduino
http://www.gearbest.com/development-boards/pp_125934.html?currency=GBP&lkid=10133909&gclid=Cj0KEQiA4JnCBRDQ5be3nKCPhpwBEiQAjwN1bqHs1QbsczW8mn0ejFzw_IsAj9Z0gWu6D9xQMKVAtmIaAln18P8HAQ

I connected the RTC to A4 and A5, the DHT22 to pin 2.

The card is:
https://www.amazon.co.uk/SanDisk-Ultra-Memory-Frustration-Packaging-x/dp/B014IX0202/ref=sr_1_4?s=electronics-accessories&ie=UTF8&qid=1483546739&sr=1-4&keywords=sd%2Bcard&th=1

really thanks for your help guys.

M.

Reading in a thread of the Arduino forum that might be a power supply problem, specifically because I need lot more power for writing/reading on SD card in comparison to just gather data from a sensor or creating a file, and concerned about the losing of the brightness of the screen that now I am noticing:

  • At the beginning, while LCD displays “Initialising SD”
  • In between “file open” and “file close” during the first cycle

I tried:

  • To power the Arduino with a Kuman Power Supply Wall Adapter 9V AC to DC 1A (instead of the USB port)
  • To investigate if I needed 3.3 V or 5V and on my module is clearly written 5V and to connect the 5V to
    5V in Arduino.

So, conclusion, I am still lost.

Any help or something to investigate will be really appreciated.

Mario

I connected the SD module to the 3.3V instead of the 5V and now I do not have the "file not open" issue.

I am worried to burn something long term with this configuration...or is it ALLRIGHT?

or is it ALLRIGHT?

It's fine. The SD card may push the 3.3V regulator to its limit when actually writing, but that shouldn't happen that often.

PaulS:
It's fine. The SD card may push the 3.3V regulator to its limit when actually writing, but that shouldn't happen that often.

So why what was not working with 5V??? I am asking for my own knowledge Paul...

Thanks a lot.

So why what was not working with 5V??? I am asking for my own knowledge Paul...

Fair enough. But, what I have to ask is what that collection of words, in that order, means. Nothing to me, yet.

Sorry for my bad English :frowning:

I meant I would like to understand the reason why it was not working ("file not open" msg on LCD screen and not writing on the file) with 5V.

I am really confused. Very happy it is sorted and that you told me that is OK and I will not burn straight away anything, but I would like to get the concept for my own knowledge.

Thanks Paul

I meant I would like to understand the reason why it was not working

I'd be more surprised if a 3.3V device worked properly at 5V.

My first assumption was that you were properly powering the device - proper voltage and current, and that you had the appropriate level shifters in place on all the input lines. Sounds like this was a bad assumption.

Thanks a lot Paul!

Thanks to everyone that joined the discussion.

Mario