PlayingWithFusion.com Home


0 Items
View shopping cart itemsView Cart

Document: 1216
Posted: March 6, 2025
Modified: March 6, 2025

Home > Tech Article Categories > R3actor and Arduino > SD Card with R3aktor

SD Card with R3aktor

Use the built in R3aktor SD card socket to write a text file to a micro SD card

Introduction

The R3aktor M0 Logger Board has a built-in micro-SD Card reader that can utilize a standard Arduino micro-SD card reader library. This tutorial will show you how to install the micro-SD card reader library and utilize its features.

Install Required Libraries

Open the Library Manager by selecting the library manager icon on the left.

In the search box, type sd and make sure to set the Type filter to Arduino.

Hover your mouse over the SD by Arduino, Sparkfun library and select install. Wait for the process to complete.

Write Software

Copy the following code into the Arduino IDE:

  1. /*
  2. SD Card Example
  3. R3aktor M0 Logger Board
  4. Playing With Fusion
  5.  
  6. Author: Noah Johnson
  7. September 16, 2022
  8. */
  9.  
  10. #include <SD.h>
  11.  
  12. File sdFile;
  13.  
  14. void setup() {
  15.   // Open serial communications and wait for port to open:
  16.   Serial.begin(9600);
  17.   while (!Serial) {
  18.     ; // wait for serial port to connect. Needed for native USB port only
  19.   }
  20.  
  21.   Serial.print("Initializing SD card...");
  22.  
  23.   if (!SD.begin(SDCARD_SS_PIN)) { // SDCARD_SS_PIN is the defined pin for the SD Card reader
  24.     Serial.println("Initialization failed.");
  25.     while (1);
  26.   }
  27.   Serial.println("Initialization done.");
  28.  
  29. //  ----- Writing to the SD Card -----  //
  30.  
  31.   // open the file SD.open("file_name", mode)
  32.   sdFile = SD.open("PWF.txt", FILE_WRITE);
  33.  
  34.   // test if the file correctly opened and write to it
  35.   if (sdFile == true) {
  36.     Serial.println("File opened");
  37.     Serial.print("Writing to PWF.txt...");
  38.     sdFile.println("And you thought fire was dangerous...");
  39.     // close the file:
  40.     sdFile.close(); // Each file must be closed before another can be opened
  41.     Serial.println("File closed");
  42.   } else {
  43.     // if the file didn't open, print an error:
  44.     Serial.println("error opening PWF.txt");
  45.   }
  46.  
  47. //  ----- Reading from the SD Card -----  //
  48.  
  49.   sdFile = SD.open("PWF.txt");
  50.  
  51.   if (sdFile == true) {
  52.     Serial.println("File opened");
  53.     Serial.println("PWF.txt:");
  54.  
  55.     // read data from file
  56.     while (sdFile.available()) {
  57.       Serial.write(sdFile.read());
  58.     }
  59.     // close the file:
  60.     sdFile.close();
  61.     Serial.println("File closed.");
  62.   } else {
  63.     // if the file didn't open, print an error:
  64.     Serial.println("error opening test.txt");
  65.   }
  66. }
  67.  
  68. void loop() {
  69. }

Setup Hardware and Program

Insert a micro-SD card into the micro-SD card slot on the R3aktor board.

Connect your R3aktor M0 Logger to your computer via USB-C cable.  Then, in the Arduino IDE, make sure you have selected Select Board -> R3aktor M0 Logger.

Upload the code by selecting the upload button and wait for the process to complete.

Now that the software is loaded onto the R3aktor, we can verify it is running by watching the Serial Monitor.  Open the Serial Monitor by selecting Tools -> Serial Monitor.

The serial monitor will open at the bottom of the Arduino IDE window. If everything was done correctly, the output will be identical.

Related Products