Thursday, 26 July 2018

Arduino SD Card Module Interface – Hook-up Guide and Data Logging

In this project, I will show you what is an SD Card Module, how to interface a MicroSD Card Adapter with Arduino and how the Arduino SD Card Module Interface can be used for Data Logging of sensor data.

Introduction

We have interfaced several sensors like Humidity, Temperature, RTC Clock, etc. with Arduino in several earlier projects. All we did in those projects is hook-up a sensor with Arduino and view the sensor’s data on either an LCD or the Arduino IDE’s Serial Monitor.

As soon as you power-off the Arduino, all the previous data read from the sensor is lost and there is now way you can retrieve that data.

Arduino SD Card Module Interface Image 1

Data Logging is a process of recording data based on time or an event. Data Logging is already being implemented in several applications like Weather (Temperature), Agriculture (Soil Moisture), Automobiles (Crash Data), Aircrafts (Black Box), etc.

In our case, if we want to record the data from a sensor using Arduino, we have to interface an SD Card with Arduino. In order to do that, you have to use a MicroSD Card Adapter or an SD Card Module and understand about the Arduino SD Card Module Interface.

A Brief Note on SD Card Module / Adapter

A Micro SD Card is a flash based, removable memory device. It is non-volatile memory and is often used in mobile phones and other consumer electronic devices.

Arduino SD Card Module Interface Micro SD Card

Before going to look at the SD Card Adapter, you need to understand two things about a typical Micro SD Card.

First thing is the operating voltage. Almost all Micro SD Cards work in a voltage range of 2.7V to 3.6V (typically, 3.3V). Second, is the communication interface. A Micro SD card supports SPI Communication.

Related Post: Basics of SPI Communication

An SD Card Module or a Micro SD Card Adapter is a simple board which facilitates connection between a Micro SD card and a Microcontroller like Arduino. The following is the image of a typical SD Card Module.

Arduino SD Card Module MicroSD Card Adapter

Since Arduino operates at 5V and the Micro SD Card operates at 3.3V, a typical Micro SD Card Adapter or an SD Card Module basically consists of two important components. They are the 3.3V Voltage Regulator IC and a 5V to 3.3V Level Converter IC for the communication pins.

Pins of SD Card Module

Talking about pins, as I have mentioned that a Micro SD Card supports only SPI Communication, the SD Card Module has pins for SPI Communication. So, the pins on an SD Card Module are as follows.

  • VCC – 5V
  • GND – GND
  • MOSI – Master OUT Slave IN (Input)
  • MISO – Master IN Slave OUT (Output)
  • SCK – SPI Clock (Input)
  • CS – Chip Select (Input)

The following image shows the pins and components of an SD Card Module.

Arduino SD Card Module Pins

Arduino SD Card Module Interface

Now that we have seen a little bit about the SD Card Module, let us proceed with interfacing one with Arduino. First thing to remember is that the communication between Arduino and the SD Card Module is through SPI Interface.

Hence, you have to identify the SPI Pins on your Arduino Board. In case of Arduino UNO, the SPI Pins are as follows:

  • MOSI – 11
  • MISO – 12
  • SCK – 13
  • CS or SS – 10

If you are using Arduino Mega, then check for the SPI Pins before making the connection.

Coming to the Arduino SD Card Module Interface, I have designed two circuits for this project. In the first circuit, I have simply made the connection between the Arduino and the SD Card Module and extract the information of the card. This circuit can be considered as an Arduino SD Card Module Hook-up Guide.

In the second circuit, the magic of actual data logging happens. It is an extension to the first circuit with sensors connected to the Analog Pins of Arduino and the data from these sensors is captured on an event.

Components Required

Components mentioned here are combined for both the circuits.

  • Arduino UNO
  • Micro SD Card
  • Micro SD Card Adapter or SD Card Module
  • Push Button
  • 3 x 10KΩ Potentiometers
  • Connecting Wires

Arduino SD Card Module Hook-up Guide

The following image shows the circuit diagram of the Arduino SD Card Module Interface.

Arduino SD Card Module Interface Circuit Diagram

Circuit Design

Connect the MOSI, MISO, SCK and CS (SS) pins of the SD Card Module to Digital I/O Pins 11, 12, 13 and 10 of Arduino. Connect the VCC and GND pins of the SD Card Module to +5V and GND of Arduino.

Code

You can find the code in the Arduino IDE: File – Examples – SD – CardInfo. You can also use the following code.

/*Connections
SD card Module - Arduino UNO
MOSI - Pin 11
MISO - Pin 12
CLK - Pin 13
CS - Pin 10
*/
#include <SPI.h>
#include <SD.h>
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 10;
void setup()
{
Serial.begin(9600);
while (!Serial)
{
;
}
Serial.print("\nInitializing SD card...");
if (!card.init(SPI_HALF_SPEED, chipSelect))
{
Serial.println("Initialization failed. Things to check:");
Serial.println("* Is a card inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* Did you change the chipSelect pin to match your shield or module?");
while (1);
}
else
{
Serial.println("A card is present and wiring is correct.");
}
Serial.println();
Serial.print("Card type: ");
switch (card.type())
{
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
if (!volume.init(card))
{
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
while (1);
}
Serial.print("Clusters: ");
Serial.println(volume.clusterCount());
Serial.print("Blocks x Cluster: ");
Serial.println(volume.blocksPerCluster());
Serial.print("Total Blocks: ");
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
Serial.println();
uint32_t volumesize;
Serial.print("Volume type is: FAT");
Serial.println(volume.fatType(), DEC);
volumesize = volume.blocksPerCluster();
volumesize *= volume.clusterCount();
volumesize /= 2;
Serial.print("Volume size (Kb): ");
Serial.println(volumesize);
Serial.print("Volume size (Mb): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Gb): ");
Serial.println((float)volumesize / 1024.0);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void loop(void)
{
}

Arduino SD Card Module Interface Insert SD Card

Working

Insert a Micro SD Card in the slot provided on the SD Card Module and make the necessary connections. Upload the code to Arduino and open the Serial Monitor. If everything goes well, you can see the information about your Micro SD Card on the serial monitor.

Arduino SD Card Module Interface Card Info

Everything is taken care by the SPI and SD libraries of Arduino. You don’t have to download these libraries as they come with Arduino IDE.

Data Logging with Arduino SD Card Module Interface

The next circuit is about data logging on to a Micro SD Card using the Arduino and SD Card Module. The following image shows three Potentiometers connected to three Analog pins of Arduino.    

Arduino SD Card Module Data Logging Circuit Diagram

Circuit Design

The Interface of Arduino and Micro SD Card Adapter is same as the earlier circuit. Additionally, three potentiometers are used as analog sensors and are connected to A0, A1 and A2 of Arduino UNO.

Also, a button is connected to Pin 7 of Arduino to mark an event.

Code

/*Connections
SD card Module - Arduino UNO
MOSI - Pin 11
MISO - Pin 12
CLK - Pin 13
CS - Pin 10
Button to Pin 7 and GND with internal Pull-up.
Three 10KΩ Potentiometers to Analog Pins A0, A1 and A2.
*/
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
const int butPin = 7;
bool butState;
void setup()
{
pinMode(butPin,INPUT_PULLUP);
Serial.begin(9600);
while (!Serial)
{
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("Card initialized.");
}
void loop()
{
butState = digitalRead(butPin);
if (butState==1)
{
//Default state. Do Nothing.
}
else
{
Serial.println("Data LOG");
String dataString = "";
for (int analogPin = 0; analogPin < 3; analogPin++)
{
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2)
{
dataString += ",";
}
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else
{
Serial.println("error opening datalog.txt");
}
}
delay(300);
}

Working

As I have mentioned earlier, data logging happens either at a predefined interval of time or in case an event is triggered. For the first case i.e., to log the data based on time, you have to interface an RTC Module to Arduino and the data from the sensor can be updated to the log at a certain time interval.

Since this project is a simple interface between Arduino and Micro SD Card, I have not used an RTC Module but a simple Push Button.

When ever the button is pressed, Arduino captures the sensor data from the Analog Pins and writes them to a text file on the Micro SD Card.

Arduino SD Card Module Interface Data Logging

The data is also displayed on the serial monitor. To view the data, simply connect the Micro SD Card to a computer and open the text file.

IMPORTANT NOTES:

  • If your Micro SD is not being read by Arduino, make sure that it is FAT formatted. The SD library supports both FAT16 and FAT32 formats.
  • The SPI Pins MOSI, MISO and SCK are fixed but CS or SS pin can be modified. Make sure that you have selected the correct pin.   

The post Arduino SD Card Module Interface – Hook-up Guide and Data Logging appeared first on Electronics Hub.

No comments:

Post a Comment

Smart textiles and surfaces: How lightweight elastomer films are bringing tech to life

Clothes that can mimic the feeling of being touched, touch displays that provide haptic feedback to users, or even ultralight loudspeakers. ...