12 Replies Latest reply on Jun 16, 2015 12:54 PM by c.....f

    How to use SD card

    toadaze

      I have been able to mount a SD card in Linux and read/write files to it.  But I'm at a loss as to using the SD card in an Arduino sketch. Normally Arduino uses the SPI pins MISO, MOSI, CLK and CS, but I can see that's not how it is connected on an Edison Arduino board. Has anyone used the SD card with Arduino or Python for that matter?

        • 1. Re: How to use SD card
          faceplant

          What do you want to do with the SD card?  AFAIK, "Arduino" programs run on the Edison as a normal process on Linux, so you should be able to (I assume) read and write files just like you normally do in C/C++.

          • 2. Re: How to use SD card
            toadaze

            At this point, I have only tried the SD example sketches built into the Arduino IDE (version Arduino 1.5.3-Intel.1.0.3). All seem to do the same thing - upload OK but then just hang with no errors or messages.  I think the sketch is waiting on the SD card to connect.  And I think it doesn't connect because these sketches are using SPI communications as a normal SD card shield requires instead of whatever the Edison-Arduino uses.

             

            So, I'm still trying to understand how to access the breakout board SD slot with an Arduino sketch.

             

            If you can give me an example of how C/C++ would access this breakout board SD slot, maybe I could adapt it to an Arduino sketch.  Or are you thinking the SD can only be accessed in C/C++ and not a normal Arduino sketch?

            • 3. Re: How to use SD card
              faceplant

              I've really never used the Arduino IDE, so I can't comment on that, but I don't think you're going to be able to use standard Arduino sketches that use the SD card with the Edison.  You are correct that it is going to use the SPI/SD interface directly, and there's a real SD card driver (and filesystem driver) on the Edison.


              When you plug an SD card into the Edison it mounts it just like any other drive and you can access files just as you can normally in Linux (or Windows for that matter).

               

              In C you would use fopen/fread/fwrite/fclose to read/write files.  In C++ you can also use those or iostreams.  I don't know for sure that those will work with the Arduino interface, but I think they should.

              • 4. Re: How to use SD card
                toadaze

                I'll report back on this later next week.  In the distant past I have written programs in C and run them on an Arduino, including reading and writing to disk files. I never tried a SD card back then, but I'm sure I can handle it the same way.

                 

                Uploading and running C programs on an Arduino is a different ball game than going the Arduino IDE route.  Using the Arduino IDE to access a SD card on a shield requires the use of one of their special libraries. It is nothing like working with file I/O in C.

                 

                Doing this directly in C has certain advantages, but the big disadvantage is not being able to use the hundreds of Arduino libraries that are available. With a lot of work, the libraries could probably be modified to work directly in C, but that kind of defeats their purpose.

                • 5. Re: How to use SD card
                  toadaze

                  I think I might be at a dead end using the onboard SD card slot on the Arduino breakout board from within the Arduino IDE.  Seems ridiculous that a product called Edison Arduino breakout board would not support its onboard SD card slot from its Arduino IDE.

                   

                  There is a SD card library in the Edison Arduino IDE, but it is written for standard SD card shields for the Arduino and uses SPI. I assume it works for such shields, but I want to use the onboard SD slot.  It appears to me the Edison accesses the SD card slot with direct address lines rather than any kind of SPI or i2c interface. And of course Linux treats it as a file.

                   

                  When I used to use C programs with file read/write capabilities on standard Arduino boards, I was doing it through a chip programmer that uploaded the compiled C program directly to the Arduino's Atmega328 chip.  Obviously, the Edison Arduino board doesn't have an Atmega328 chip to run Arduino or C programs, so that's not an option.

                   

                  In addition, my C programs relied on #include <stdio.h> and #include <process.h> for file I/O.  Those are not built into an Arduino IDE.  I suppose an expert could write an Arduino library using them, but I have no idea how to use a .h file to build a library.  There are many more files to an Arduino library than just a .h file.

                   

                  I haven't tried it yet, but I'm sure I can access the SD card slot from Python, since I can read/write to it from Linux - unless the Edison Python is also missing dependent SD card software.  Guess I should try it and report back.

                  • 6. Re: How to use SD card
                    toadaze

                    As suspected, no problem accessing the onboard SD card slot with Python.  Here's quick & dirty Python program that reads a text file from the card and writes it out to a new file on the card.

                     

                    fileIn=open('/media/sdcard/input.csv','r')

                    fileOut=open('/media/sdcard/output.txt','w')

                     

                    # infinite loop until EOF is reached

                    while 1:

                        line=fileIn.readline()

                        if not line: break

                       

                        fileOut.writelines(line)

                       

                    fileIn.close()

                    fileOut.close()

                    • 7. Re: How to use SD card
                      toadaze

                      Here's another approach to writing & reading to the SD card.  There is an Intel IoT Developer Kit over in Intel's Developer Zone for both Edison and Galileo boards. I downloaded and installed the Developer Kit on my Windows machine. It supports C, C++, JavaScript and Python, but I was mostly interested in writing a just a C program to write & read to the SD card.

                       

                      The Kit uses a modified version of Eclipse as its programming environment.

                       

                      Here's a simple C program I used to write some data to the card, read it back and display it.

                       

                      #include <stdio.h>

                      #include <stdlib.h>

                       

                      struct Student {

                         int id;

                         char name[12];

                         int percent;

                      } s1 = { 12345, "Allen", 99 };

                       

                      int main(void) {

                          puts("Student test results");

                       

                          FILE *fp;

                          struct Student s2;

                       

                          //Write to file

                          fp = fopen("/media/sdcard/test.txt", "w");

                          fwrite(&s1, sizeof(s1), 1, fp);

                          fclose(fp);

                       

                          //Read back from file

                          fp = fopen("/media/sdcard/test.txt", "r");

                          fread(&s2, sizeof(s2), 1, fp);

                          fclose(fp);

                       

                          //Display results

                          printf("\nID : %d", s2.id);

                          printf("\nName : %s", s2.name);

                          printf("\nPercent : %d", s2.percent);

                       

                          return (0);

                       

                      }

                       

                      The above program works fine.  However, when I tried to do some other things with file I/O like using fgetc and fputc, I ran into missing #include type problems.  For example, <process.h> and <conio.h> are not included in the C:/iotdk-ide-win/devkit-x86/sysroots/i586-poky-linux/usr/include/ folder. But that's an issue for another day.

                      • 8. Re: How to use SD card
                        faceplant

                        You should be able to find fgetc and fputc in stdio.h.  process.h and conio.h are not standard Posix include files AFAIK.

                        • 9. Re: How to use SD card
                          JPMontero_Intel

                          Hi toadaze,

                           

                          I was able to read and write into the SD card using the code from this site http://arduino.cc/en/Tutorial/ReadWrite. At first the serial monitor did not displayed anything but after I pressed the board reset it displayed the output. Another thing you can do is to add a delay in the set up to give you enough time to open the serial monitor. The file was created in the /media/sdcard directory. You can also check what the sketch wrote by using cat <name of the file>. I hope you find this helpful

                           

                          Regards,
                          JPMontero_Intel

                          • 10. Re: How to use SD card
                            Intel_Alvarado

                            Were the previous suggestions helpful? If you still need assistance please reply with your questions.

                            Regards

                            Sergio

                            • 11. Re: How to use SD card
                              toadaze

                              Thanks JPMontero!  The Arduino example sketch you referenced above does indeed work just like I wanted.  I am surprised the Edison-Arduino onboard SD Card works without any modification to the example sketch using the same pins as an add-on Arduino SD Card shield.

                              • 12. Re: How to use SD card
                                c.....f

                                Where are you running this code to get it to work?

                                 

                                I am trying to run a program from a computer, and using ssp connect to an edison. I am able to open a file if it is directly on the edison.

                                I need to put the file on an sd card on the edison, and now I cannot read the file from the sd card. I am not entirely sure what the file path is, but all the examples that i have tried, including /media/sdcard/text.txt has not worked.

                                 

                                any suggestions? thanks