user78323: I am working on a "personal assistant" with Arduino and I am having trouble playing sound from an SD card... I am using a shield called "Easy Shield UNO" by Simkeim, and it has an SD card reader on it. It does work because I tested it through the Serial monitor, but when I try to play sound, nothing happens. (The speaker does work) Can someone please help me check my code or something to see what is wrong?
Here is the code I found online, I only changed it a bit...
#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;

void setup(){
 tmrpcm.speakerPin = 7;
 Serial.begin(9600);

 tmrpcm.setVolume(255);
}
void loop() {
  if(!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD card not found");
    return;
}
  else{
    Serial.println("SD card reading sucess");
    tmrpcm.play("test1.wav");
  }
}


user42015: I see three problems with your code:

It doesn't make much sense to begin the SD card again and again in loop(). Please move that part to setup() or somewhere, where it isn't called on every loop iteration.

You shouldn't call tmrpcm.play() in every loop() iteration. It doesn't wait for the song to be finished. It just starts the playing (which is controlled by a timer interrupt) and exits. And every call of it will start the file again at its start. So by calling it again and again in fast succession you don't leave enough time for the file to actually be played, until you play again from start. To prevent that you can use an if statement to only start, if there isn't a file playing currently:
  if(!tmrpcm.isPlaying()){
      tmrpcm.play("test1.wav");
  }


You are choosing pin 7 as output pin, but you cannot choose the pin freely, because it has to be connected to the used Timer and thus being PWM capable. Thats not the case for pin 7. In the wiki of the TMRpcm library you can find the following line under "Functions":
  audio.speakerPin = 11;     set to 5,6,11 or 46 for Mega, 9 for Uno, Nano, etc.

So please connect your speaker to pin 9 and use tmrpcm.speakerPin = 9;.



78323: <p>I am working on a &quot;personal assistant&quot; with Arduino and I am having trouble playing sound from an SD card... I am using a shield called &quot;Easy Shield UNO&quot; by Simkeim, and it has an SD card reader on it. It does work because I tested it through the Serial monitor, but when I try to play sound, nothing happens. (The speaker does work) Can someone please help me check my code or something to see what is wrong?</p>
<p>Here is the code I found online, I only changed it a bit...</p>
<pre><code>#include &quot;SD.h&quot;
#define SD_ChipSelectPin 10
#include &quot;TMRpcm.h&quot;
#include &quot;SPI.h&quot;

TMRpcm tmrpcm;

void setup(){
 tmrpcm.speakerPin = 7;
 Serial.begin(9600);

 tmrpcm.setVolume(255);
}
void loop() {
  if(!SD.begin(SD_ChipSelectPin)) {
    Serial.println(&quot;SD card not found&quot;);
    return;
}
  else{
    Serial.println(&quot;SD card reading sucess&quot;);
    tmrpcm.play(&quot;test1.wav&quot;);
  }
}
</code></pre>
 42015: <p>I see three problems with your code:</p>
<ul>
<li><p>It doesn't make much sense to begin the SD card again and again in <code>loop()</code>. Please move that part to <code>setup()</code> or somewhere, where it isn't called on every loop iteration.</p>
</li>
<li><p>You shouldn't call <code>tmrpcm.play()</code> in every <code>loop()</code> iteration. It doesn't wait for the song to be finished. It just starts the playing (which is controlled by a timer interrupt) and exits. And every call of it will start the file again at its start. So by calling it again and again in fast succession you don't leave enough time for the file to actually be played, until you play again from start. To prevent that you can use an if statement to only start, if there isn't a file playing currently:</p>
<pre><code>  if(!tmrpcm.isPlaying()){
      tmrpcm.play(&quot;test1.wav&quot;);
  }
</code></pre>
</li>
<li><p>You are choosing pin 7 as output pin, but you cannot choose the pin freely, because it has to be connected to the used Timer and thus being PWM capable. Thats not the case for pin 7. In the <a href="https://github.com/TMRh20/TMRpcm/wiki" rel="nofollow noreferrer">wiki of the TMRpcm library</a> you can find the following line under &quot;Functions&quot;:</p>
<pre><code>  audio.speakerPin = 11;     set to 5,6,11 or 46 for Mega, 9 for Uno, Nano, etc.
</code></pre>
<p>So please connect your speaker to pin 9 and use <code>tmrpcm.speakerPin = 9;</code>.</p>
</li>
</ul>
