fbpx

Interface Sdcard With Arduino Uno

Secure Digital (SD) is a non-volatile memory card.The microcontroller have to back up the data which they have read during their operation the data which they need to access during their running time.

The SD memory card comes in large storage capacities and they are compatible with the SPI bus of the microcontroller.The files in an SD memory card are stored using FAT32 or FAT16 and one should use the code or FAT file-system library to access the files from an SD card.The SD card operates on 3.3V logic and hence to interface it with a microcontroller which runs on 5V logic one should use a Logic Level Converter.

Interfacing SDcard

Digital pins 10, 11, 12 and 13 can be configured as SS, MOSI, MISO and SCK. The MISO of the memory card should be connected to the pin number 11, the MOSI should be connected to the pin number 12 and the SCK should be connected to the pin number 13 of the Arduino pro-min.

Commands

  • SD.begin()-To initialize memory card
  • SD.open()-To open a file from the SD card
  • File. println()-To write a data to the file
  • File.read()-To read a byte from the file
  • file.close()-To close a file

Hardware Required

  • SD card.
  • Arduino uno.

Application

  • Data Logger.

Circuit

Code

 #include <Time.h>
 #include <Wire.h>
 #include <DS1307RTC.h>
 #include <SPI.h>
 #include <SD.h>
 const int chipSelect = 4;
 int sensorPin = A0;
 int sensorValue = 0;
 void setup()
 {
 Serial.begin(9600);
 while (!Serial)
 {
 ; // wait for serial port to connect. Needed for Leonardo only
 }
 setSyncProvider(RTC.get);
 if(timeStatus()!= timeSet)
 Serial.println("Unable to sync with the RTC");
 else
 Serial.println("RTC has set the system time");
 Serial.print("Initializing SD card...");
 if (!SD.begin(chipSelect))
 {
 Serial.println("Card failed, or not present");
 return;
 }
 Serial.println("card initialized.");
 }
 
 void loop()
 {
 String dataString = "";
 int sensor = analogRead(sensorPin);
 dataString += String(sensor);
 if (sensorPin < 2)
 {
 dataString += ",";
 }
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if (dataFile)
 {
 dataFile.println(dataString);
 dataFile.print(hour());
 printDigits(minute());
 printDigits(second());
 dataFile.print(" ");
 dataFile.print(day());
 dataFile.print("/ ");
 dataFile.print(month());
 dataFile.print("/ ");
 dataFile.print(year());
 dataFile.println();
 dataFile.close();
 Serial.println(dataString);
 }
 else
 {
 Serial.println("error opening datalog.txt");
 }
 }
 void printDigits(int digits)
 {
 Serial.print(":");
 if(digits < 10)
 Serial.print('0');
 Serial.print(digits);
 }
 } 

Leave a Reply

Your email address will not be published. Required fields are marked *