sd_test.ino 675 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <SPI.h>
  2. #include <SD.h>
  3. #define CS_PIN 5
  4. void setup() {
  5. Serial.begin(115200);
  6. // Initialize SD card
  7. if (!SD.begin(CS_PIN)) {
  8. Serial.println("Card Mount Failed");
  9. return;
  10. }
  11. Serial.println("Card Initialized");
  12. // Create and open file
  13. File file = SD.open("/HelloWorld.txt", FILE_WRITE);
  14. // Check if the file opened successfully
  15. if (!file) {
  16. Serial.println("Failed to open file for writing");
  17. return;
  18. }
  19. // Write to file
  20. file.println("Hello World!");
  21. // Close the file
  22. file.close();
  23. Serial.println("File written successfully");
  24. }
  25. void loop() {
  26. // Nothing to do here
  27. }