SD Card Module on ESP 32
Posted: Mon May 20, 2024 8:16 am
I tried using an SD card module with ESP32 but it can only appear on the serial monitor after pressing the EN button. I have attached the program below
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#define SD_CS_PIN 5 // Pin CS untuk SD card
// Definisikan pin SPI
#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
SPIClass spi = SPIClass(VSPI);
void setup() {
Serial.begin(115200);
// Inisialisasi SPI
spi.begin(SPI_SCK, SPI_MISO, SPI_MOSI, SD_CS_PIN);
if (!SD.begin(SD_CS_PIN, spi)) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
// Menulis data ke file
writeFile(SD, "/hello.txt", "Hello World!\n");
// Membaca data dari file
readFile(SD, "/hello.txt");
}
void loop() {
// Tidak ada yang dilakukan di loop
}
void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
void readFile(fs::FS &fs, const char * path) {
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while (file.available()) {
Serial.write(file.read());
}
file.close();
}
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#define SD_CS_PIN 5 // Pin CS untuk SD card
// Definisikan pin SPI
#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
SPIClass spi = SPIClass(VSPI);
void setup() {
Serial.begin(115200);
// Inisialisasi SPI
spi.begin(SPI_SCK, SPI_MISO, SPI_MOSI, SD_CS_PIN);
if (!SD.begin(SD_CS_PIN, spi)) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
// Menulis data ke file
writeFile(SD, "/hello.txt", "Hello World!\n");
// Membaca data dari file
readFile(SD, "/hello.txt");
}
void loop() {
// Tidak ada yang dilakukan di loop
}
void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
void readFile(fs::FS &fs, const char * path) {
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while (file.available()) {
Serial.write(file.read());
}
file.close();
}