Hi, I would like to add sound to my project, so I am testing out playing sound files (.wav) from an SD card using the TMRpcm library to play them. I have set up the hardware, but this can't be the problem I don't think as I receive no error serial messages and when I play the sound file, it prints "playing" so it's unlikely the hardware (though not impossible). Maybe there is a problem in my code?
#include <SD.h> // need to include the SD library
//#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10 //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 = 9; //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
}
tmrpcm.play("1"); //the sound file "music" will play each time the arduino powers up, or is reset
}
void loop(){
if(Serial.read() == 'p'){ //send the letter p over the serial monitor to start playback
tmrpcm.play("1");
Serial.println("Now playing");
}
}
I have set up the hardware, but this can't be the problem I don't think as I receive no error serial messages and when I play the sound file, it prints "playing" so it's unlikely the hardware (though not impossible). Maybe there is a problem in my code?
The software says playing, but the hardware does nothing, so it must be a software problem. I do not understand that logic at all.
PaulS:
The software says playing, but the hardware does nothing, so it must be a software problem. I do not understand that logic at all.
What I meant by that (maybe it wasn't clear) is that maybe the tmrpcm library wasn't working for some reason, that's why I let it up to you to decide what the problem might be. And actually I said "it's unlikely the hardware", I didn't say it must be a software problem. @aarg Well this is the demo sketch that comes with it I just thought maybe the library has a problem with the speakerpin or something, I know little about the chip's internal clocks and stuff.
If the software reads the serial port, sees the p, tells the hardware to get busy, and prints the message that you see, but you don't hear anything, then the problem really does appear to be because the hardware isn't working (or the volume is turned down too low or the speaker is not connected properly or is the wrong kind) or the required file doesn't exist on the SD card. It is not a software problem.