Arduino:
Creating a temporary file

How to:

Arduino typically interacts with microcontrollers that don’t have a traditional filesystem—so “files” aren’t managed the same way they are on a PC. Instead, we use EEPROM (a small amount of memory that persists across resets) or an SD card with a shield. Here’s a basic example of writing and reading temporary data to EEPROM:

#include <EEPROM.h>

// Write a temporary value to EEPROM
void writeTempEeprom(int address, byte value) {
  EEPROM.write(address, value);
}

// Read a temporary value from EEPROM
byte readTempEeprom(int address) {
  return EEPROM.read(address);
}

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Write and read from EEPROM
  writeTempEeprom(0, 123); // Example value and address
  byte tempValue = readTempEeprom(0);

  // Output the temporary value
  Serial.print("Temporary Value: ");
  Serial.println(tempValue);
}

void loop() {
  // Nothing here for this example
}

And if you’re working with an SD card:

#include <SPI.h>
#include <SD.h>

File tempFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait for serial port to connect. Needed for native USB port only
  }

  if (!SD.begin(4)) {
    Serial.println("Initialization failed!");
    return;
  }

  tempFile = SD.open("temp.txt", FILE_WRITE);

  // Write something to the temporary file
  if (tempFile) {
    tempFile.println("Temporary data string");
    tempFile.close();
  } else {
    Serial.println("Error opening temp.txt");
  }
  
  // Read from the temporary file
  tempFile = SD.open("temp.txt");
  if (tempFile) {
    while (tempFile.available()) {
      Serial.write(tempFile.read());
    }
    tempFile.close();
  } else {
    Serial.println("Error opening temp.txt");
  }

  // Optionally, remove the temporary file after use
  SD.remove("temp.txt");
}

void loop() {
  // Nothing here for this example
}

Sample output (for both examples) on the Serial Monitor after running the setup should be:

Temporary Value: 123

Or, for the SD card example:

Temporary data string

Deep Dive

Historically, temporary files in programming cater to needs like caching, logs, or inter-process communications. On systems like PCs, with full operating systems, temp files are widespread. In Arduino, it’s different. Microcontrollers have limited non-volatile storage (EEPROM), or we add external storage like SD cards.

Alternatives to EEPROM for short-term data include using RAM (quickly lost between power cycles and reboots) or external memory like Flash or a hard-wired IC.

Implementation-wise, when writing to EEPROM on an Arduino, remember that it has a limited write cycle (often around 100,000 cycles). Overusing it can wear it out—so use it sparingly for truly temporary scenarios.

Using an SD card for temporary storage is akin to regular file handling on a PC. It offers more space, but requires proper management like ensuring decent quality cards, handling file opening/closing correctly, and understanding that it’s relatively slow compared to EEPROM or RAM.

See Also