Create a new file on the SD card every time the Arduino reboots

So basically I want to have a new file on my SD card every time the Arduino is rebooted, without replacing the same file every time. In other words, if I reboot the Arduino 3 times I want to end up with 3 different files and not one file only. The current code which you can see below gives me only one file no matter how many times I try to reboot.
(I tried adding the to the void loop section, however, it does not do what I want.)
As you can see also I added the random-number command so that it creates a different file name and does not overwrite the same file that it created the first time, but unfortunately did not work. So, can anyone find a solution, please?

sketch_jan20a.ino (647 Bytes)

The random() function returns the same series of "random" numbers each time the Arduino is booted unless you use the randomSeed() function. But even then a duplicate number may be generated at some time

One way round this is to store the number of the current file in EEPROM then, on reset, load it, increment it, save it again and use the number to create a unique filename

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

Try doing a dir of what is on the SD card first and then write the next file. OR have one file (create if missing) that holds the number of the last file written on startup

@UKHeliBob many thanks for the reply. I did what you said and you can take a look at the following code, unfortunately, the problem now is that the numbers that are being generated are (unsigned long) and I have to convert them to a (String) for naming the file. So do you have any idea how to do so?

// Include the libraries:
int pinCS = 10;

unsigned long randomNumber;
String filename = randomNumber;
String results = "results.txt";
String Name = (filename + results);

File myFile; // creation of new file on SD card

void setup() 
   Serial.begin(9600); // To show the results on the serial monitor
 pinMode (pinCS,OUTPUT);
randomNumber = random (0,2000);

void loop()
    myFile =, FILE_WRITE);
// if the file opened okay, write to it:
  if (myFile) {
        myFile.print("test 1 2 3");
        myFile.close(); // close the file:

I tried adding the following but did not work also:

String filename = String (randomNumber);

Firstly, the use of String objects is known to cause problems in the small memory of a microcontroller. It is generally considered preferable to use zero terminated arrays of chars (aka C strings) instead

As to your question, try this

char fileName[20];

void setup()
  while (!Serial);
  for (int n = 0; n < 16; n++)
    sprintf(fileName, "file%02d.txt", n);

void loop()

many thanks, using the code which you posted is only going to view these 16 names all one shot, but as I mentioned in my first message I need at every time the Arduino is booted a new file is created without overwriting the first.

The code was an example of how to create a filename from a number. Of course, the number does not have to come from a for loop and is not limited to 16 as long as the filename including the number does not exceed 8 characters in length in order to conform to the 8.3 filename requirement of the SD library

thank you In that case, do you have any idea (in terms of a code) on how can I use that in creating a new file on the SD card? all my trials are ending up either by creating them all one-shot or overwriting the same file over and over.

Start by writing a small sketch that just writes the number (not the name) of the first file to an EEPROM location. If you use an unsigned int for the number and use the EEPROM.put() function to write it then you will be able to create up to 65,535 different filenames. I imagine that will be enough

Your real sketch should read the number using EEPROM.get() in setup() and put it in an unsigned int variable. Using the number, create the filename as I have shown and open it. If it is created and opened successfully then increment the file number and save it back to EEPROM.

Use the filename throughout your program as normal

Have a look at the code for Sparkfun's OpenLog on their github site. It logs data to an SD card and creates a new filename each time it is powered up.