Wave from SD Controller not playing on speaker (even though tone does!)

Hi all,

I'm doing a school project where touch paint sensors on a painting trigger a wave file to be played from a speaker. I'm using an Elegoo Mega with an SD card reader and small GIKFUN 3W speaker.

Grumpy_Mike if you are out there this is very similar to one you solved for someone else.

My code plays the tone through the speaker. The SD card read works, and It says it's playing the tes2.wav... and even the loop that checks if audio is playing comes back true. But I get no sound (aside from the test tone)

I feel like I am missing something obvious!

Below is my code, any suggestions are welcome! (Apologies for the bad photos!)

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

#define SD_ChipSelectPin 53
TMRpcm tmrpcm;

void setup() {
  Serial.begin(9600);
  
  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD card initialization failed");
    return;
  }

  tmrpcm.speakerPin = 9; // Use pin 9 for audio playback
  tmrpcm.setVolume(5); // Set the volume to a mid-high level


  // Now play a WAV file
  if (SD.exists("tes2.wav")) {
     // First, play a tone
  tone(9, 1000, 1000); // Play 1000 Hz tone for 1000 ms
  delay(1500); // Wait for the tone to finish plus a buffer

    noTone(9); // Stop any ongoing tones
    pinMode(9, OUTPUT); // Reinitialize pin mode
    tmrpcm.play("tes2.wav"); // Play the WAV file
   delay(3000); // Optional: delay while audio plays
  } else {
    Serial.println("File tes2.wav not found.");
  }
}

void loop() {


  // If you need to continuously check or react to the audio state, do it here.
  if (tmrpcm.isPlaying()) {
    Serial.println("Audio is still playing...");
    // Additional logic if needed while audio is playing
  } else {
    // Actions to perform once audio playback is complete
    Serial.println("Playback has completed or file was not found.");
    // You might want to loop playback, handle errors, etc.
  }
  delay(1000); // Adjust timing or actions according to your application needs
}



Hi @riverbcodes ,
Welcome to the forum..
Interesting project..
You know if you put the "@" in front of the user name then they get notified..
Not sure if @Grumpy_Mike would see it otherwise..

Never used this lib before, but quick look at their demos..
This same line is in all of them..

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

maybe you're just using the wrong pin, you have the mega correct??

good luck.. ~q

1 Like

@qubits-us

THANK YOU!

No idea why this worked. We had it on pin 9. I changed it to pin 46... and it works?!

Thank you

And thank you @Grumpy_Mike , your previous answers got us this far also

1 Like

Glad to be of help previously.

Now then, pin 46 implies you are using a Mega and they have different default outputs for the TMRpcm library than pin 9 which looks like an Arduino Uno output.

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