1

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);
     }
}    
4
  • How is the SD card reader connected? Is it part of a shield? It might also be helpful if you show us the complete program. Are you calling SD.begin() anywhere? Mar 15, 2015 at 10:45
  • @peter Hi,I have called the SD.begin() in setup(). The issue is "write" function is not working. When I am using "print", I am able to write to the file. Thanks!
    – Naresh
    Mar 15, 2015 at 15:52
  • 1
    How do you know it's not working? What's actually happening? Mar 15, 2015 at 16:30
  • I am writing the byte b to the text file. When I am reading the SD card, I can't see the byte in it which I am able to see when I use "print" function instead of "write". I desperately want "write" function to operate so that I can directly write arrays without any "for loops", which I will not be able to achieve if I use "print" function. Thanks!
    – Naresh
    Mar 15, 2015 at 16:48

2 Answers 2

0

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.

2
  • 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
    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. Mar 15, 2015 at 18:40
0

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.

2
  • Hi, the issue is "write" function is not working. When I am using "print", I am able to write to the file. Thanks!
    – Naresh
    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
    Mar 17, 2015 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.