TUTORIAL MICRO SD ATAU SD CARD MENGGUNAKAN ARDUINO
Pada artikel kali ini,kita akan membahas Tutorial Micro SD atau SD CARD menggunakan Arduino, untuk bisa komunikasi arduino dengan micro SD menggunakan module microSD.Module microSD merupakan solusi sederhana untuk tulis dan baca microSD menggunakan Arduino. Pin Out dari module microSD bisa langsung di sambungkan ke Arduino.Salah satu contoh project yg sering memakai module ini adalah Data Logger.
Micro SD atau SD Card komunikasi dengan arduino menggunakan SPI, untuk librarynya pakai "SD.h", secara default library micro SD sudah di sediakan oleh Arduino secara default saat kita Instalasi Software IDE Arduino.Library ini bisa di pakai untuk Baca dan Tulis SD Card dan Micro SD.
Untuk kali ini kita akan melakukan percobaan pada Arduino Uno dengan Module Micro SD.Untuk lebih jelasnya bisa langsung mengikuti langkah-langkah berikut ini :
Tampilan Atas |
Tampilan Bawah |
MODULE / PART YANG DI BUTUHKAN
- Arduino Uno
- microSD Module
- microSD Card
- Kabel Jumper secukupnya
WIRING DIAGRAM
ALOKASI PIN ARDUINO
MicroSD | Arduino |
CS | 10 |
SCK | 13 |
MOSI | 11 |
MISO | 12 |
Vcc | +5V |
GND | GND |
CONTOH PROGRAM 1
Contoh program 1 untuk check dan mengetahui info dari microSD yg terpasang pada microSD module
Contoh program 1 untuk check dan mengetahui info dari microSD yg terpasang pada microSD module
#include < SPI.h >
#include < SD.h >
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 10;
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("\nInitializing microSD 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?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.print("\nCard 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");
return;
}
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster();
volumesize *= volume.clusterCount();
volumesize *= 512;
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
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) {
}
#include < SD.h >
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 10;
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("\nInitializing microSD 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?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.print("\nCard 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");
return;
}
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster();
volumesize *= volume.clusterCount();
volumesize *= 512;
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
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) {
}
SERIAL MONITOR
CONTOH PROGRAM 2
Contoh program 2 untuk buat datalogger sederhana,data tersimpan di microSD
#include < SPI.h >
#include < SD.h >
const int chipSelect = 10;
void setup() {
Serial.begin(9600);
delay(1000);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop() {
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
#include < SD.h >
const int chipSelect = 10;
void setup() {
Serial.begin(9600);
delay(1000);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop() {
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
SERIAL MONITOR
Selamat Mencoba dan Berkarya :)
sumber : www.arduino.cc
Bang gimana cara membaca data terakhir yg update terus file tersebut.
BalasHapusuntuk system tersebut, kita sarankan menggunakan database.jadi data di simpan di PC/Server atau menggunakan Raspberry Pi.semoga membantu :)
BalasHapusKak kalau mau buat program cara mengetahui space memori penuh gimanaaa ya mohon bantuannya untuk TA saya
BalasHapusMemory ada ram, Rom dan memory external.yang mau di lihat memory mana?
Hapus