For this section of the lab you need your Arduino, the breadboard, the SD card reader, the SD card, and the usual wires and odds and ends.
Step 1:
We need to start by connecting the SD card reader to the Arduino. All such boards and devices need external power as well as specific commuication connections. This Arduino Project Hub guide will show you how to make the required connections. While the code shown on this guide is essentially the same as what I am sharing with you below I have provided a lot more commentary which I hope is helpful.
Step 2:
Here's the code for testing the SD card reader. As usual, open a new sketch, copy and paste, and then save in your Arduino reference folder for future use.
/* MicroSD Card Reader Test In the context of PH212 this sketch is substantially different in that it uses significant libraries to execute the needed steps in reading and writing from and to a microSD card in a dedicated board. The libraries in question aredocumented here... https://www.arduino.cc/en/Reference/SD documented here... https://www.arduino.cc/en/Reference/SPI SD card attached to SPI bus as follows: ** MOSI - pin 11 ** MISO - pin 12 ** CLK - pin 13 ** CS - pin 10 It is an important skill for an STEM worker to be able to understand and use a tool that they don't fully understand. This is personallly difficult for me because I want to know how it happens but sometimes you just gotta go with the flow and this is one of those times. I got the very lightly commented code below from https://electronicshobbyists.com/arduino-sd-card-shield-tutorial-arduino-data-logger/ and I am thankful to be able to do so. There are many almost identical versions of this code so it is clearly in the public domain. I will add my own thoughts or perhaps insights to the comments with my initals so that you know to be cautious about them. */ // these #include statements make the functions defined in these libraries // accessible to the writer of this sketch. See the documentation links // above for detailed descriptions of all functions. Notice that any function // used from these libraries starts with SD.'function' or SPI.'function'. You // can tell the IDE recognizes them because of the color change. #include <SD.h> #include <SPI.h> // Creates an object that is of a type File and gives it a name much // like defining an int or float (needs more clarity) -- BE File sdcard_file; bool writetoFile = true; // set to false is all you want is to read bool removeFile = true; // if you wish to erase file after reading // NOTE: data file name must be manually edited on lines 81, 94, 111, 120, 122 // the CS pin manages the back and forth flow of information to the card as // part of the Serial Protocol Interface (SPI) method of communication. - BE int CS_pin = 10; // this test sketch has been set up to complete a short set of writes and // reads to the connected SD card. This is not actually looping as you would // need in a data logger. This is once and done which is a useful feature of // the void setup() call. void setup() { // Opens the serial monitor so we can watch the write/read process Serial.begin(9600); //Setting baudrate at 9600 while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } pinMode(CS_pin, OUTPUT);//declaring CS pin as output pin // SD.begin initiallizes the library and the card and returns TRUE if // successful. Argument articulates the control pin. Default pin depends // on board. if (SD.begin(10)) { Serial.println("SD card is initialized and it is ready to use"); } else { Serial.println("SD card is not initialized"); } // This opens a file called mydata.txt on the card in the write mode. Can // also open it in FILE_READ mode. I have tried a number of approaches and // it seems you must type in the file name explicitly due to expectations of // the SD library. sdcard_file = SD.open("mydata.txt", FILE_WRITE); // If file opens successfully sdcard_file evaluates as TRUE and if you want to write to // the file (writetoFile = true). Otherwise you are just using this to read a file if (sdcard_file && writetoFile) { //If the file is found Serial.println("Writing to file is under process"); sdcard_file.println("This data is a new test."); //Writing to file // test whether just print leaves different data. sdcard_file.close(); //Closing the file -- after every operation Serial.println("Done, file closed"); } else { Serial.println("Failed to open the file"); } sdcard_file = SD.open("mydata.txt"); if (sdcard_file) { Serial.println("Reading from the file"); Serial.println(); // The file that is being read is sdcard_file opened on the SD card. // File is read one character at a time until it reaches the end while (sdcard_file.available()) { Serial.write(sdcard_file.read()); } sdcard_file.close(); Serial.println("Done with Read"); } else { Serial.println("Failed to open the file"); } // Check to see if the file exists: This is a potentially useful tool when reading an // SD card. if (SD.exists("mydata.txt")) { Serial.println("data file exists."); } else { Serial.println("data file doesn't exist."); } // delete the file if removeFile = true if (removeFile) { Serial.println("Removing data file..."); SD.remove("mydata.txt"); } // verify that file is gone if (SD.exists("mydata.txt")) { Serial.println("data file still exists."); } else { Serial.println("data file is gone."); } } void loop() { //Nothing in the loop }
Step 3: Actual Testing!
Because this sketch only runs through one time and then stops (notice that there is nothing in the loop() portion of the sketch) it will be wise to open the serial monitor BEFORE uploading the sketch. If you want to rerun the test just press the reset button on the Arduino and it will rerun. After you have verified that you seem to be able to open, write to, and read a file on the microSD card you can return to the main lab.