1

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");
  }
}
3
  • A) Please add a link to the shield. B) Have you tried using a sample sketch that comes with the library to ensure everything works as expected?
    – Nino
    Aug 7 at 11:08
  • I bought the shield while we were living in China, so it is a Chinese website. i-item.jd.com/59763201006.html. Yes I tried using a sample sketch, everything works, but the sound is just two long static sounds
    – Renier Jr.
    Aug 7 at 11:35
  • When I change the name of the file, then the speaker does nothing, but when I use the real name of the file, the speaker makes squeaky sounds
    – Renier Jr.
    Aug 7 at 11:50
2

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;.

3
  • Hi, thanks for the help! I did change it like you said, but it still makes that static sound... I also tried an example TMRpcm code, but my speaker still just makes a static sound? Do you think that could be related to the code or the file? Because I did try it with two other speakers and they did the same...
    – Renier Jr.
    Aug 7 at 16:01
  • @RenierJr. I've looked again at the library and found, that you cannot freely choose the used pin. It has to be PWM capable and belong to the used Timer (defaults to Timer1). I added a paragraph about that in my answer
    – chrisl
    Aug 7 at 18:32
  • Thanks chrisl!! I did try the example code and it used pin 9, now it works, but there is a lot of static playing with the music. I will try to change the sampling rate to see if that works... Thanks again for the time you spent to help me!
    – Renier Jr.
    Aug 8 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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