Advertisement
pleasedontcode

SD Card Write rev_01

Sep 19th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: SD Card Write
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-09-19 09:03:35
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* write hello world in sd card */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <SPI.h>
  25. #include <SdFat.h>  //https://github.com/greiman/SdFat
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF SPI PINS *****/
  32. const uint8_t sdcard_SDCardModule_SPI_PIN_MOSI_D23      = 23;
  33. const uint8_t sdcard_SDCardModule_SPI_PIN_MISO_D19      = 19;
  34. const uint8_t sdcard_SDCardModule_SPI_PIN_SCLK_D18      = 18;
  35. const uint8_t sdcard_SDCardModule_SPI_PIN_CS_D5     = 5;
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. // Create an instance of the SdFat class
  39. SdFat sd; // This object will be used to interact with the SD card
  40. SdFile file; // Create an instance of SdFile to handle file operations
  41.  
  42. void setup(void)
  43. {
  44.     // Initialize serial communication for debugging
  45.     Serial.begin(9600);
  46.     while (!Serial) {
  47.         yield(); // Wait for serial port to connect
  48.     }
  49.  
  50.     pinMode(sdcard_SDCardModule_SPI_PIN_CS_D5, OUTPUT); // Set the CS pin as output
  51.     // Start the SPI library:
  52.     SPI.begin(); // Initialize the SPI library
  53.  
  54.     // Initialize the SD card
  55.     if (!sd.begin(sdcard_SDCardModule_SPI_PIN_CS_D5)) {
  56.         Serial.println("SD card initialization failed!");
  57.         return; // Stop if the SD card initialization failed
  58.     }
  59.     Serial.println("SD card initialized successfully.");
  60.  
  61.     // Open a file for writing
  62.     if (file.open("hello.txt", FILE_WRITE)) { // Create or open the file "hello.txt"
  63.         file.println("Hello, World!"); // Write "Hello, World!" to the file
  64.         file.close(); // Close the file
  65.         Serial.println("Message written to SD card.");
  66.     } else {
  67.         Serial.println("Failed to open file for writing.");
  68.     }
  69. }
  70.  
  71. void loop(void)
  72. {
  73.     // Stop the loop after writing to the file
  74.     while (true); // Infinite loop to prevent repeated writes
  75. }
  76.  
  77. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement