#include <SPI.h>
#include <SD.h>
// Define the chip select pin for the SD card
const int CS_PIN = 10;
void setup() {
// Start the serial communication
Serial.begin(115200);
// Initialize the SD card
Serial.print("Initializing SD card...");
if (!SD.begin(CS_PIN)) {
Serial.println("Initialization failed!");
return;
}
Serial.println("Initialization done.");
// Create and open a file on the SD card
File file = SD.open("/test.txt", FILE_WRITE);
// If the file opened successfully, write to it
if (file) {
Serial.println("Writing to file...");
file.println("Hello, ESP32-C6!");
file.close();
Serial.println("File written.");
} else {
Serial.println("Failed to open file for writing.");
}
// Reopen the file in read mode
file = SD.open("/test.txt");
if (file) {
Serial.println("Reading from file:");
// Read the file until there's nothing else in it
while (file.available()) {
Serial.write(file.read());
}
file.close();
} else {
Serial.println("Failed to open file for reading.");
}
}
void loop() {
// Nothing to do here
}