[SOLVED] Trouble using TMRpcm library's play method

I’m using a Mega 2560 as my board, and I’m trying to play WAV files using the TMRpcm library (found here). Unfortunately, I cannot get the play method to work.

As specified by the library’s documentation, I have produced a 2.wav file in 8-bit mono 16 KHz through an iTunes conversion and put in on my SD card.

I’m using the “basic.ino” sketch provided in the library’s example folder and have changed the pins accordingly to fit my layout. As recommended by someone who had volume issues with this library, I have speakerPin connected via a 100 Ohm resistor to the gate of a transistor which has the arduino’s 5V to source and drain to the positive terminal of my speaker (which is a simple 16 Ohm, 0.5 W speaker I ripped out of a greeting card).

To ensure that I wasn’t having hardware issues, I ran toneMelody from Arduino’s example projects and it works just fine. I think that means I can safely assume the speaker is good, the wiring is good, and the transistor doesn’t mess anything up.

Anyways, below is the modified “basic.ino” sketch I was talking about. The output I get is:
SD card is good
0

Does anyone have any ideas why it might not be working? Thanks!

#include <SD.h>                      // need to include the SD library
#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
//#define SD_ChipSelectPin 4  //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>

TMRpcm tmrpcm;   // create an object for use in this sketch


void setup(){

  tmrpcm.speakerPin = 11; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc

  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
  Serial.println("SD card is good");
  tmrpcm.setVolume(4);
  tmrpcm.play("2.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
  Serial.println(tmrpcm.isPlaying());
}



void loop(){  

  if(Serial.available()){    
    if(Serial.read() == 'p'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("2.wav");
    }
  }

}

After many, many hours of reading and trying various things, I finally figured it out! I solved it by going to the library's header file, pcmConfig.h, and commenting out all the parts in the "GENERAL USER DEFINES" section. This includes:

#define buffSize 128 //must be an even number
#define SD_FULLSPEED
#define HANDLE_TAGS
#define DISABLE_SPEAKER2
#define USE_TIMER2
#define debug

Thanks for the help!

I’m working on an UNO, and once I uncommented those lines, it (almost) worked. :grin:

For anyone else who is having trouble - in order for my audio files to play, I had to put “.WAV” after the file name in the code (not in the actual file name on the SD card). For example, the basic example code uses audio.play(“music”) - change that to audio.play(“music.WAV”), but make sure that the actual file name on your SD card is “music”.

I also had to make sure the library was updated before it worked.

You may want to fiddle around with the sound quality settings specific to your pieces of audio, and find out which sounds best.

Finally, if the audio isn’t sounding quite right or is distorted, you could try calling the audio.setVolume() function… 1 is lowest volume and 7 is highest; I found that 6 and 7 were too high for the audio samples I worked with.