I have built a set up with an Arduino UNO hooked up to a switch, so that whenever the switch goes from HIGH to LOW it saves audio to an SD card. I am using the TMRpcm Library. The problem is the current code will only be able to record 1 audio file; I want to the Arduino to run for a long period of time collecting multiple audio files as the switch is turned on and off. I need a way that will allow it so everytime the switch is pushed back down the next file saved will have a different name than the one previously saved (preferably by an increased numeric value eg "1.wav" & "2.wav"). I have tried audio.startRecording(audiofile+".wav", 16000, A0); and it makes it so no files save to the SD card accept an empty file simple titled 'WAV'. I have tried using a switch case but it will not allow me to reach a case over 10, without running out of RAM. Currently, my code can save 1 audio file but I want it to reach a point where it can save as many as the SD card can hold, without having to restart the software.

My current code is here. Any help would be greatly appreciated

//////////////////////////////////////// SD CARD
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10
TMRpcm audio;
//////////////////////////////////////// SWITCH CASE
int audiofile = 0;     // # of recording
unsigned long i = 0;
bool recmode = 0;      // recording state
//////////////////////////////////////// SWITCH
int inPin = 2;         // input Switch Pin
int state = HIGH;      // the current state switch
int reading;           // the current reading from the switch

void setup() {
  pinMode(A0, INPUT);  // Microphone
  pinMode(inPin, INPUT_PULLUP); // Switch
  //////////////////////////////////////// SD CARD
  audio.CSPin = SD_ChipSelectPin;

void loop() {

  reading = digitalRead(inPin);
  while (i < 300000) {
  i = 0;
  if (reading == LOW) {
    if (recmode == 0) {
      recmode = 1;


      audiofile++; // To move case
      audio.startRecording("1.wav", 16000, A0);
  else if (reading == HIGH) {
    recmode = 0;

  • You cannot simply add an integer to a string literal with audiofile + ".WAV", because of the different types. But you can use a char buffer and sprintf("%i.WAV",audiofile);
    – chrisl
    Dec 27, 2018 at 10:50
  • Also asked at: forum.arduino.cc/index.php?topic=587263 If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information.
    – per1234
    Dec 27, 2018 at 11:14
  • @chrisl Thank you very much, this almost solved all my issues!
    – TSauer52
    Dec 27, 2018 at 12:18
  • @per1234 my apologies. I am new to forums as of today and am learning the ground rules on how to properly use them. Thank you for the heads up!
    – TSauer52
    Dec 27, 2018 at 12:19

1 Answer 1


I would (and do) use snprintf to format a string as a filename:

// Global scope, or local static
uint32_t myFileNumber = 1;
char filename[12];

// in your function - results in 00000001.WAV
snprintf(filename, 12, "%08d.WAV", myFileNumber);

%08d will allow between 00000000.WAV and 99999999.WAV and always with 8 digits. Any more and it will get truncated and corrupted (100000000.WA) but won't cause a buffer overrun. You will have to take precautions that you never have more than 100 million files ;)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.