Arduino Forum

Using Arduino => Storage => Topic started by: benjaminhoey on Apr 25, 2017, 02:11 pm

Title: how to know if println fails to sd card?
Post by: benjaminhoey on Apr 25, 2017, 02:11 pm
Hi,

I am using an LC-TECH 5v SD card module. Using the code and wiring from this tutorial:

sd-card-module-and-temperature-sensor (https://dzrmo.wordpress.com/2013/02/09/sd-card-module-and-temperature-sensor/#comment-1212)\

I didnt have trouble initializing the sd card, but I had trouble writing to the file. I found out that I needed to use an external power supply because there wasn't enough current. Now everything is working fine.

This setup will be used on a moving vehicle, and what I would like to do, is figure out some way of letting me know if the arduino is not writing to the file - so for example light an LED.

I know it is possible to know if the sd cards hasn't initialized, but I dont know how to find out if println() to the file has failed or not.

Is this possible?
Title: Re: how to know if println fails to sd card?
Post by: pert on Apr 25, 2017, 02:54 pm
The first place to look for an answer to this sort of question is the reference page for the library:
https://www.arduino.cc/en/Reference/SD (https://www.arduino.cc/en/Reference/SD)
If you look at the documentation of println() linked from that page:
https://www.arduino.cc/en/Reference/FilePrintln (https://www.arduino.cc/en/Reference/FilePrintln)
Quote
Returns

byte
println() will return the number of bytes written, though reading that number is optional
So if println() returns 0 you know the write failed. For example:
Code: [Select]

if(myFile.println((float)DHT11.humidity, DEC)) == 0) {
  digitalWrite(errorLED, LOW);
}
Title: Re: how to know if println fails to sd card?
Post by: benjaminhoey on Apr 25, 2017, 03:23 pm
perfect thanks!