I have recently started working with arduino UNO and the "write" function in sd Card library is not working. Below is my code. I have tried with "println" which is working fine. Any help regarding this is welcome.
void loop()
{
byte b=10;
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{
dataFile.write(b);
}
}
2 Answers 2
The problem is that print()
and write()
behave differently when you give them numerical data, such as a byte.
When you pass your byte to print()
, it converts it to text which is convenient for humans to read. For example:
byte b = 10;
dataFile.print(b);
This will result in the following ASCII characters being written to the file:
10
That's actually two bytes: one byte with the value 49, which represents ASCII character 1
, followed by a byte with the value 48 representing character 0
.
However, write()
does not convert numbers into text. It writes them in their raw binary form. For example:
byte b = 10;
dataFile.write(b);
When you do this, it doesn't write a "1" and a "0" to the file. It literally writes a binary byte with the value 10. When you try to open this as a text file, your computer interprets that byte as a single ASCII character. It so happens that ASCII value 10 is the newline character, meaning your file basically contains a blank line.
For comparison, try this instead:
byte b = 65;
dataFile.write(b);
ASCII value 65 corresponds to the letter A
(uppercase), so that's what you should see in the file.
The write()
functions are useful for writing raw data to file in the most efficient form for another machine to read (as opposed to being human-readable). This is why they allow you to write arrays -- it acts like one big block of raw data with a fixed format.
In your case, print()
is more appropriate. That means for outputting arrays of numbers, you will need to use loops. That functionality isn't built-in to the print()
functions because different projects might need different formatting. For example, some people might want one number per line, whereas others might want numbers separated by commas, and so on.
-
Thanks Peter, but I want to store large chunks of data as fast as possible so that I can sample incoming data at high rates and using of loops slows the speed. Is there a way to achieve this?Naresh– Naresh2015年03月15日 17:49:51 +00:00Commented Mar 15, 2015 at 17:49
-
@Naresh If speed is critical then it's preferable to use
write()
to output binary. However, you can't read raw binary files using a text editor. You would need some other program on your computer to load it or convert it into a useful format. I don't know of a general purpose program which would do that for you automatically. You'd probably have to write something yourself.Peter Bloomfield– Peter Bloomfield2015年03月15日 18:40:02 +00:00Commented Mar 15, 2015 at 18:40
It is typical of file devices that an output file must be closed when the application is through writing to it - datafile.close()
, using your variable names. Their drivers or libraries typically buffer output data in memory and may not write it to the device until the buffer fills or the application flushes the buffers or closes the file.
See the read/write SD card example at the Arduino site. Especially note the uses of the calls to:
begin
open
print or write, ...
close
Failing to close an output fail might leave unwritten data in memory buffers rather than flushing the last few bytes (all of the data, in the case of your one-byte-write example).
It is also possible that there is another fault - incorrect mapping of I/O pins to the card reader/writer, incorrect wiring, faulty device or card, but I think the programming sequence is the most likely culprit.
-
Hi, the issue is "write" function is not working. When I am using "print", I am able to write to the file. Thanks!Naresh– Naresh2015年03月15日 15:53:55 +00:00Commented Mar 15, 2015 at 15:53
-
I would still make sure I used the whole {begin; open; write; close;} sequence, then examine the resulting output file with a hex-editor on your computer to confirm that does or does not contain what you expect it to.JRobert– JRobert2015年03月17日 17:40:16 +00:00Commented Mar 17, 2015 at 17:40
SD.begin()
anywhere?