Catalex SD Card Reader - Serial Writing to Disk File


// Sample Program, sends data to SD card using Arduino Uno and Catalex card reader ===== // If you have trouble, check for cold solder joints, catalex boards are apparently not tested // Second, note position of the line "File myfile" in the program, allows global access to resources // Program reads time in ms since program began, outputs at one second intervals======= #include <SPI.h> #include <SD.h> File myfile; int a=0; unsigned long time; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { } ; // wait for serial port to connect. Needed for Leonardo only Serial.print("Initializing SD card..."); // see if the card is present and can be initialized: if (!SD.begin(9)) { Serial.println("Card failed, or not present"); // don't do anything more: while (1) ; } Serial.println("card initialized."); // Open up the file we're going to log to! myfile = SD.open("datalog.txt", FILE_WRITE); if (! myfile) { Serial.println("error opening datalog.txt"); // Wait forever since we cant write data while (1) ; } } void loop() { //================= Writing phase =============== if (a<10){ time = millis(); Serial.print("Time: "); Serial.println(time); myfile.print("Time: "); myfile.println(time); delay(1000);} // wait a second so as not to send massive amounts of data //======== closing phase ================ if (a==12){ myfile.close(); } //========= done loop forever ============== if (a>14) {a=14; Serial.println("Done"); delay(3000);} a++; }

If you are looking for information on this topic, there are three main sources. Serial read and write is fairly simple as shown in the example above and there are numerous examples documented throughout the web. For positional writing of objects on disk (a dangerous sport in any land) you will need a good book on C language that covers the seek() and write() functions. All the commands in SD.h are listed on the Arduino site, their names are self explanatory, and numerous examples can be found on the web. My particular journey takes two paths. One, to create an environmental data logger and possible controller, and second to document a cheap altimeter for deploying parachutes on low cost amateur rockets. The picture below shows first, the data echoed back to the host computer by the serial print command "Serial.print();". The second and third columns show the data sent to the SD chip by the drive print lines "myfile.print();" The program ran three times, each time opening and concatenating the information! This data was read from the microchip by my computer using a sony SD drive adapter from Walmart after it was written to by the Arduino. Finally, there is a screen picture from my computer showing the file DATALOG.TXT that was coaxed at great human cost into holding this frivolous cache of butterfly wings for presentation here. The memory used was about 40% of the arduino capacity just to run this brief program. Web sources say much of this capacity can be regained by editting the SD.h header file. Ugh! Not me...




Below is the wiring diagram. If this format looks familiar, I reworked it from a diagram posted here.


Edward Kimble, PhD click here to e-mail me at: kimble@gunstar1.com
Edited April 29, 2016