Example Code for Arduino-U-disk Mode
Last revision 2026/01/13
This article guides users on utilizing Arduino-U-disk mode with the FireBeetle M0 development board, providing hardware and software setup instructions, usage methods, and sample code for data storage and retrieval.
Hardware Preparation
- Firebeetle 2 M0 Development Board(SKU: DFR0652) ×1
- USB Cable ×1
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Install FireBeetle 2 M0 board URL: https://downloadcd.dfrobot.com.cn/boards/package_DFRobot_index.json
Other Preparation Work
The FireBeetle M0 features a built-in 12MB USB disk. When you need to store small amounts of data, there's no need to purchase an SD card module. You can access and edit data saved on the development board as if it were a USB drive, and you can also use the board's built-in library to read and write data while in USB disk mode.
Usage Method:
- Double-click the RST button to enter USB disk mode. When in USB disk mode, the RGB LED in the center of the board will remain solid green.

- Reading and Writing Data Using the Built-in UD Library.
Sample Code
Init U disk, read & write test.txt then print via serial.
/*!
* @file ReadWrite.ino
* @brief UD Disk read/write
* @n This example shows how to read and write data to and from an UD disk file
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @version V1.0
* @date 2019-03-5
* @get from https://www.dfrobot.com
*/
#include <SD.h>
#define ONBOARD_UD_M0_CS 32 // The CS pin for the onboard U disk on Firebeetle M0 is 32
#define NONBOARD_SD_MOUDLE_CS 2 // The CS pin of the SPI module is connected to digital pin 2 on the M0
File myFile;
void setup() {
// Open serial communications and wait for the port to open:
Serial.begin(115200);
while (!SerialUSB) {
; // Wait for the serial port to connect. Needed for native USB port only
}
Serial.print("Initializing UD disk...");
/* If using the onboard U disk of the M0 board, initialize as follows */
if (!SD.begin(/*csPin = */ONBOARD_UD_M0_CS, /*type = */TYPE_ONBOARD_UD_SAMD)) {
SerialUSB.println("initialization failed!");
while(1);
}
/* If an external SPI SD card module is connected, initialize as follows */
//if (!SD.begin(/*csPin = */ONBOARD_UD_M0_CS, /*type = */TYPE_NONBOARD_SD_MOUDLE)) {
// SerialUSB.println("initialization failed!");
// while(1);
//}
SerialUSB.println("initialization done.");
// Open the file. Note that only one file can be open at a time,
// so you must close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// If the file opened successfully, write to it:
if (myFile) {
SerialUSB.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// Close the file:
myFile.close();
SerialUSB.println("done.");
} else {
// If the file didn't open, print an error:
SerialUSB.println("error opening test.txt");
}
// Re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.print("test.txt:");
// Read from the file until there's nothing left:
while (myFile.available()) {
SerialUSB.write(myFile.read());
}
SerialUSB.println();
// Close the file:
myFile.close();
} else {
// If the file didn't open, print an error:
SerialUSB.println("error opening test.txt");
}
}
void loop() {
// Nothing happens after setup
}
Result
Serial print init info, write & read specified content of test.txt.
Was this article helpful?
