🚚 Free delivery on orders over: €35 Croatia • €60 EU • $100 US

HUM: SD CARD MODULE

microSD card reader breakout-Communication

You are a beginner with Dasduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by soldered where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Dasduino and the basic code. Everything else is left to your imagination.

INTRODUCTION

In this tutorial we will learn how to store data from Dasduino to an SD card using the SD card module. SD card module allows connecting an SD card of up to 32GB to Dasduino and we can store different data that is important and later process it on the computer or use it again on Dasduino. Using this module we can also read data from an SD card and display them on the LCD display or use them for another function.

SD CARD PREPARATION

To work, the module uses an SD card to which it stores data, but in order to work with the card it is necessary to format the card in the FAT16 or FAT32 format. For micro SD card it is necessary to use an adapter and if the card does not work with it, we must set the switch marked with Lock (in the picture) to the upper position in order to work with the card (if it is under you will not be able to format it). When you format the card in the FAT16 or FAT32 format you are ready to work. It is not necessary to have a file saved on the card if we want to write on it because Croduino will create a file itself, with the name we set in the code.

HOW TO CONNECT IT?

The module uses SPI communication so we need to connect the pins of the module to Dasduino pins that are used for SPI communication. Pin CS can be connected to any other Dasduino pin, only then we must change which pin we have connected it to in the code.
SD-Modul >>>>>>>>>>Dasduino
MOSI >>>>>>>>>>>>>>>> D11
MISO >>>>>>>>>>>>>>>> D12
SCK >>>>>>>>>>>>>>>>> D13
CS >>>>>>>>>>>>>>>>>> D10
+5V >>>>>>>>>>>>>>>> +5V
GND >>>>>>>>>>>>>>>> GND
When we connect Dasduino and the SD module we are ready to program Dasduino in order for it to store data to an SD card or read from the card.

ARDUINO CODE

In the following example we read values from the analog pin and store them on the card. To the analog pin we can connect the potentiometer (one end of the potentiometer to +5V, the other to GND, and the slider to the analog pin) or if nothing is connected, we will get random values between 0 and 1024.
We save the data to the Podaci.csv file, and later we can enter data from it to Excel and process it, and that is why data is divided by commas and each data has its own serial number which is set by the variable ocitanje. If we do not need data processing we can change it so that the data is saved to a .txt file (only replace csv with txt). The name of the file must not exceed 8 signs, because the program will not work. To work with the SD module it is not necessary to download a library because it is in the Arduino IDE along with the library for SPI communication.

#include "SPI.h" //including the SPI library that allows SPI communication
#include "SD.h"  //including the SD library that serves to work with an SD card
const int CS = 10;  //defining CS pin
int ocitanje=1;   //defining variable ocitanje and setting its value to 1
void setup() {
  Serial.begin(9600);  //starting serial communication
  pinMode(CS,OUTPUT);  //setting CS as an output pin
  //initializing the SD card and checking if the initialization was successful
  if (!SD.begin(CS)) {
    //if the initialization is not successful, a message about the error is displayed
    Serial.println("The card is not connected properly.");
    return;
  }
  //if the card is connected properly and the initialization is successful, a message that the card is ready for work is displayed
  Serial.println("The card is connected properly and ready for work.");
  //checking if there is a file Podaci.csv, if there is, we skip the part of the code in the braces
  if(!SD.exists("Podaci.csv"))
  {
  String prvi= "Ocitanje,AnalogniPin"; //To String prvi we save words Ocitanje and Analogni pin that are comma-separated
  File ocitanja = SD.open("Podaci.csv", FILE_WRITE); // opening the file Podaci.csv for writing and reading from the file which is done through
                                                     //an object reading, using which we access the file
  if (ocitanja) { //checking if the file is open and if it is, a code in the braces is executed, if not, then it goes to the else
                  //function
   ocitanja.println(prvi); //saving String prvi to the file using object reading with the function println
   ocitanja.close(); //closing the object reading, i.e. through the object we close the file Podaci.csv
   Serial.println(prvi); //printing out String prvi on the Serial monitor 
  }
  //if opening the file was not successful, an error message is displayed
  else{
   Serial.println("Not able to open Podaci.csv");
    }}}
void loop() {
   int senzor = analogRead(A0); //into the variable sensor we save the readings from analog pin A0
   String podaci = String(ocitanje)+','+String(senzor); //into the String podaci we save values ocitanje and senzor, but convert them to String and save them as one sentence
   //variable ocitanje is our assistant variable which indicates sequence of data and it goes from 1 and increases every next saving  
   File ocitanja = SD.open("Podaci.csv", FILE_WRITE); //opening file podaci.csv
   //the following code is the same as when we save String prvi, only now we save String data and we do it every second.
  if (ocitanja) {
    ocitanja.println(podaci);
    ocitanja.close();
    Serial.println(podaci);
  }
  else {
    Serial.println("Not able to open Podaci.csv");
  }
  ocitanje++; //increasing variable ocitanje by 1
  delay(1000); //1 s pause
}

Another example shows how to read data from an SD card, but before it we must store some data on the card (either using Dasduino, or the computer).

#include "SPI.h" //including the SPI library that allows SPI communication
#include "SD.h"  //including the SD library that serves to work with an SD card
const int CS = 10;  //defining CS pin
void setup() {
  Serial.begin(9600);  //starting serial communication
  pinMode(CS,OUTPUT);  //setting CS as an output pin
  //initializing the SD card and checking if the initialization was successful
  if (!SD.begin(CS)) {
    //if the initialization is not successful, a message about the error is displayed
    Serial.println("The card is not connected properly.");
    return;
  }
  //if the card is connected properly and the initialization is successful, a message that the card is ready for work is displayed
  Serial.println("The card is properly connected and ready for work.");
  File podaci = SD.open("Podaci.txt", FILE_READ); // opening the file Podaci.txt for reading from the file 
  if (podaci) { //checking if the file is open and if it is, the code in the braces is executed, if not, then it goes to the else function
      while (podaci.available()) {//in the loop we check if the data is accessible for reading and as long as they are, we print them out on the Serial monitor
      Serial.write(podaci.read()); //reading data from the SD card with the function read() and printing them on the Serial monitor
    }
  podaci.close(); //closing the object data, i.e. through the object we close the file Podaci.txt
  }
  //if opening the file was not successful, an error message is displayed
  else{
   Serial.println("Not able to open Podaci.txt");
    }
}
void loop() {
//in the void loop we do not read data because the same data would be repeated constantly, but if you need to read something from the SD card in your code, you can do it
}