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();
}
SD Card Module on ESP 32
-
- Posts: 1701
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: SD Card Module on ESP 32
All of your program runs only from the setup() function. This function is executed by the system only once, when the chip starts.
Re: SD Card Module on ESP 32
But what is the problem? This runs and perform okay but only when you reset your program? Or you need to have the EN button pressed always for this to work? I don't understand the issue
Who is online
Users browsing this forum: No registered users and 89 guests