Issue With Playing .wav Files Using Micro SD

For this small project I am only trying to play loop of a song. I have simple code using the SD card library and the PCM library. The code compiles and uploads to the Arduino but not before displaying
In function 'void setup()':
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
tmrpcm.play("1.wav");
^
Then, even though the code compiles and uploads to the board, the music doesn't play.

#include <pcmRF.h>
#include <SD.h>                          
#include <TMRpcm.h>                     


#define SD_ChipSelectPin 4               

TMRpcm tmrpcm;                            

void setup(){
 
  tmrpcm.speakerPin = 9;                  
  
  if (!SD.begin(SD_ChipSelectPin)) {      
    return;                              
  }
  
  tmrpcm.setVolume(6);                  
  tmrpcm.play("1.wav");                   
}

void loop(){}

Is there anything wrong with my code that would cause this or is there something possibly wrong with my microSD set up?

If your SD card does not initialize properly in the SD.begin() call, you simply return from setup() which then goes into loop() forever. It would be much better to

void setup(){
  Serial.begin(115200);
  delay(200);
  Serial.println("Powering up"); 
  tmrpcm.speakerPin = 9;                  
  
  if (!SD.begin(SD_ChipSelectPin)) {      
    Serial.println("Failed to initialize SD card");
    while(1);  // loop forever                              
  }
  Serial.println("Playing file");
  tmrpcm.setVolume(6);                  
  tmrpcm.play("1.wav");                   
}

and then open the Serial Monitor, set the baud rate to 115200 and see what gets printed out

Thank you, changing the baud rate finally caused it to display something useful. Unfortunately it is failing to initialize the SD card. How would one go about fixing that because my SD card reader module does not have labelled pins? Though they seem to be in the normal spots as I am still getting power delivery to the module.

It really depends on what SD module you are using. It should document where the CS pin maps to. A common one is pin 10 but trying random pins seems like the wrong way to solve this problem.
If you look at the cardInfo example sketch that comes with the SD library:

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN

Unfortunately there is nothing printed onto the card itself so I am going to see if I can find a different module tomorrow. I will come back to this project once I have a different module. Thank you so much for your help!

Just try them all:

#include <SD.h>

const byte pins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
const int numPins = sizeof(pins) / sizeof(pins[0]);


void setup() {
  Serial.begin(115200);
  delay(200);
  Serial.println("Powering up");

  for ( int i = 0; i < numPins; ++i ) {
    Serial.print("Trying pin "); Serial.print(pins[i]);
    if (!SD.begin(pins[i])) {
      Serial.println(" Failed");
    }
    else {
      Serial.println(" Success");
    }
  }
}

void loop() {

}

Thank you so much, it finally recognizes the card I was apparently one pin off even though I swear I have looked at it several times. It won't play the file, it reads "playing file" on the original code but doesn't play the file so I will look into that and let you know.

I was using a bad SD card because I switched it and now the audio plays. There is added noise and the audio is playing in slow motion but it works. Thank you so much for the help!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.