1 2 Previous Next 16 Replies Latest reply on May 28, 2014 2:56 PM by Yuzee

    How to write files to SD?


      Hi everyone!!  I use the example of SD ,but it doesn't work that even if I use chip select 4 or 10.

       pinMode(4, OUTPUT);
        if (!SD.begin(4)) {
          Serial.println("initialization failed!");
        Serial.println("initialization done.");



        • 1. Re: How to write files to SD?

          Are you trying to do access the on-board SD card?  You don't need a chip select for that since it's not using a SPI interface.  If it's an SD card Aurduino shield, I don't believe that works (since the SD library is set up to use the on-board card only).




          if (!SD.begin()) {

             Serial.println("Initialization failed!");



          Serial.println("Initialization done.");

          • 2. Re: How to write files to SD?

            Hi Krampus :


            I have tried your sketch . It's not work.

            I know that it's not using a SPI interface. How can i control the SD card on the on-board card.  Or I have to use another SD card socket like the attachment.



            • 3. Re: How to write files to SD?

              Krampus does give the correct procedure for using the on-board SD card, though your code should work, too. You don't have to specify the pin number in the call to SD.begin(). If your initialization is failing using both of these constructs, then something else is amiss.


              Some questions for you:

              • Are you booting off of your SD card? If so, then we know that your SD reader is probably working.
              • If you aren't booting off of your SD card, then is your card formatted? Note that the SD library only supports FAT16 and FAT32 filesystems. Is it seated all the way in?
              • 4. Re: How to write files to SD?

                Hi mechalas:


                I am not sure whether it booting off of my SD card. How to know it?

                My SD card has formatted by SDFormatterv4 . I confirm that it is FAT32.



                • 5. Re: How to write files to SD?

                  Unless you specifically followed the procedure to write the SD Linux image for Galileo to the card, then you aren't booting from it.


                  To be honest, that's where I'd start. It's a good test since if Galileo can boot from it, it will be able to write to it. If you can't get it to boot from the card, then you may have a compatibility issue of some sort.


                  Have you tried another card?

                  How large is your card? Also note that it must be an SD or SDHC card. SDXC is not supported. Though this is probably not the issue if you have formatted with FAT32.

                  • 6. Re: How to write files to SD?

                    Hi mechalas:


                    Memory size is 8GB,it is SDHC,and I have tried another 8GB SDHC card.

                    I hope it could like arduino SD shield easily read and write.

                    Refer to Workaround for SD file creation bug.  which wrote  " system("touch /media/mmcblk0p1/test.txt");" .

                    I think it could via linux to read and write. What can I do and where do I start.



                    • 7. Re: Re: How to write files to SD?

                      Well, normally it can easily read and write, just like an Arduino SD shield. The question is, why isn't yours working?


                      The issue you are linking to on file creation was solved in the latest software release, which came out just a couple of days ago. If you haven't updated to that yet, I would do that, too.


                      Reading and writing to it via Linux is easy, and no different than on any other Linux system. You just mount the SD card as a filesystem. It will probably be assigned as /dev/mmcblk0p1 or something very similar, though Linux should mount it automatically. To find out if Linux even sees it, you can execute this in a sketch:


                      system("/sbin/fdisk -l > /dev/ttyGS0");


                      To see if it's mounted:


                      system("df > /dev/ttyGS0");
                      • 8. Re: How to write files to SD?

                        Hi mechalas:


                        I think is it not work because of warning?






                        • 9. Re: How to write files to SD?

                          I'm sorry, but I don't understand what you are trying to show me here. I can't see your sketch code, and I can't see what warning you are referring to. It looks like the sketch is compiling and downloading just fine.


                          Just cut & paste your sketch code, and the text of whatever warning you are seeing.

                          • 10. Re: How to write files to SD?

                            Hi mechalas:

                                 This sketch is the example of ReadWrite (C code- 84 lines - codepad).

                            could your galileo run this sketch? Or maybe my galileo is amiss .





                            • 11. Re: How to write files to SD?

                              Yes, this sketch works fine on my Galileo:

                              Initializing SD card...initialization done.

                              Writing to test.txt...done.


                              testing 1, 2, 3.

                              It could be that there is an issue with the SD reader on your system. It could also be that it does not like your SD cards or the formatting of it.


                              Have you tried any of the suggestions I've given you yet? Have you tried any of the system commands, above, to see if Linux is even recognizing your SD card? Have you tried to write the Linux image to your SD card and boot from that?

                              • 12. Re: How to write files to SD?

                                Hi mechalas:


                                I tried your suggestions. It shows


                                what does the message mean?


                                I have tried booting linux form SD Card . and Led 13 is shining.


                                • 13. Re: Re: How to write files to SD?

                                  OK. This tells me that your SD reader/writer on your Galileo is probably working. Linux is able to see it, and recognizes the partition table. It identifies an 8 GB card formatted as FAT32.


                                  However, it is not automatically mounting the filesystem on the card. Here's what my Galileo prints:

                                  Disk /dev/mmcblk0: 7931 MB, 7931428864 bytes

                                  1 heads, 16 sectors/track, 968192 cylinders

                                  Units = cylinders of 16 * 512 = 8192 bytes



                                          Device Boot      Start         End      Blocks  Id System

                                  /dev/mmcblk0p1             513      968192     7741440   b Win95 FAT32

                                  Partition 1 does not end on cylinder boundary

                                  Filesystem           1K-blocks      Used Available Use% Mounted on

                                  devtmpfs                117572         0    117572   0% /dev

                                  tmpfs                   119044       108    118936   0% /var/volatile

                                  tmpfs                   119044         0    119044   0% /media/ram

                                  /dev/mmcblk0p1         7737344    310944   7426400   4% /media/mmcblk0p1


                                  See that last line? It's found my SD card and mounted it as a filesystem. Your has not.


                                  At this point, my best guess is that Linux does not like the formatting of your SD card. You said you formatted it with SDFormatterv4. Try just formatting it with Windows or in Linux instead of using a 3rd party application.

                                  • 14. Re: How to write files to SD?

                                    Hi mechalas:


                                    Does it mean galileo can read my SD but linux cannot?

                                    Could I mount the SD on /media/mmcblk0p1?

                                    Do you boot linux from SD?

                                    1 2 Previous Next