#include <SD.h>
const int chipSelect = 10; // Set the chip select pin for your SD card module
void setup() {
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed.");
return;
}
Serial.println("SD card initialized successfully.");
}
void loop() {
File dataFile = SD.open("wokwi.txt", FILE_WRITE); // Open the file "data.txt" on the SD card for writing
if (dataFile) {
dataFile.println("Hello, world!"); // Write data to the file
dataFile.close(); // Close the file
Serial.println("Data written to SD card.");
}
else {
Serial.println("Error opening file.");
}
delay(1000); // Wait for 1 second before writing again
File dataFile2 = SD.open("wokwi.txt", FILE_READ); // Open the file "data.txt" on the SD card for reading
if (dataFile2) {
while (dataFile2.available()) { // Read the data from the file
Serial.write(dataFile2.read());
}
dataFile2.close(); // Close the file
}
else {
Serial.println("Error opening file.");
}
}