StratoLab

Lesson 5: Storing the data

Working with SD card using the Arduino language

Pre-requisites:

Objectives:

What you will be using:

What you will be learning:

Video Walk-through

In addition to the reading below, you can watch this video for guidance!

Wiring

Pinout chart

Pin on SD card reader | Pin on Arduino
—— | —— GND | GND
VCC | 5V
MISO | 12
MOSI | 11
SCK | 13
CS | 10



<img src=assets/images/SDCardSIDE.jpg width=”350” >
<img src=assets/images/ArduinoSdcard1.jpg width=”350” >
<img src=assets/images/ArduinoSDcard2.jpg width=”350” >

You are welcome to use the bread board as well to wire everything up. Lesson Five

Formatting the SD Card

The SD card module works with a maximum of 32GB. This means you can either use a 32GB card or partition a larger card with a smaller drive. You can use the disk management application availble on Windows OS to do this function.

NOTICE: Use caution with this tool as it can alter your boot drive as well.

Alt text

Working code to test SD card

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("This is a test file :)");
    myFile.println("testing 1, 2, 3.");

    for (int i = 0; i < 20; i++) {
      myFile.println(i);
    }

    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}
void loop() {
  // nothing happens after setup
}


Review

Want more?

If you have finished with the base lesson, check out the items below.

Things to think about, validate, and/or try:

Update the code to do any/all of the following:

  1. Write your own custom output to the file and change the filename
  2. Use a loop to write the even numbers from 0 to 100 into the file 😵

Troubleshooting

Need help?

Watch the walk-through video for guidance!