Switch-activated-sound

Ok so first of all, I know the board is a knockoff. When I bought It I really didn't know what I was doing.

So I have a big project I'm working on. This one creates the sound...or should... for the "Game Timer." (Technically its a 'Bomb Timer' but I don't like calling it that because people freak out. Its not a bomb, yes it imitates one, but it does not cause bodily harm. Psychological harm though... that's debatable.)

This module on the flip of a switch should play the sound of the K-19 klaxon (Remember when the reactor begins to melt down?), which I have on the SD card module. The sound file is a mono, 16000hz .wav file sitting on a fat32 formatted microSD card next to a zipped folder of the same audio file. (I wasn't sure if it would read the zip file or just the .wav by itself, turns out it reads both.)

I had to do research on the pinout for the Microsd card; turns out pins 50-53 are for cs, sck, mosi, and miso, a good thing to learn for me, I just put them anywhere at first.

As far as I know, everything is wired properly; hopefully the pictures are clear enough so that if anyone catches an error, please let me know... because it doesn't work.

The serial will print that the sd card works, is reading, notices the two files, and the system begins to play the file, printing which one it is playing, however, all that comes out of the speaker is mostly nothing, static, white noise... I don't know what to call it. I'm told that if the speaker was not working there would be nothing at all, so I am glad that there is at least static. I'll place the code in here so that you can review it and tell me if I did anything wrong; like I said the code works to my knowledge, the buttons pause / play and fast forward respectively, however I am getting no sound.

/*
 * Play wave audio files with Arduino.
 * This is a free software with NO WARRANTY.
 * https://simple-circuit.com/
 */
 
#include <SPI.h>     // include Arduino SPI library
#include <SD.h>      // include Arduino SD library
#include "TMRpcm.h"  // include TMRpcm library
 
#define next     2
#define _pause   3
 
TMRpcm audio;
 
File root;
 
void setup(void) {
  Serial.begin(9600);
  pinMode(next,   INPUT_PULLUP);
  pinMode(_pause, INPUT_PULLUP);
 
  Serial.print("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println("failed!");
    while(true);  // stay here.
  }
  Serial.println("OK!");
 
  audio.speakerPin = 10;  // set speaker output to pin 9
 
  root = SD.open("/");      // open SD card main root
  printDirectory(root, 0);  // print all files names and sizes
 
  audio.setVolume(7);    //   0 to 7. Set volume level
  audio.quality(0);      //  Set 1 for 2x oversampling Set 0 for normal
 
}
 
// main loop
void loop() {
 
  if ( !audio.isPlaying() ) {
    // no audio file is playing
    File entry =  root.openNextFile();  // open next file
    if (! entry) {
      // no more files
      root.rewindDirectory();  // go to start of the folder
      return;
    }
 
    uint8_t nameSize = String(entry.name()).length();  // get file name size
    String str1 = String(entry.name()).substring( nameSize - 4 );  // save the last 4 characters (file extension)
 
    if ( str1.equalsIgnoreCase(".wav") ) {
      // the opened file has '.wav' extension
      audio.play( entry.name() );      // play the audio file
      Serial.print("Playing file: ");
      Serial.println( entry.name() );
    }
 
    else {
      // not '.wav' format file
      entry.close();
      return;
    }
 
    while( debounce(next) ) ;  // wait until 'next' button is released
  }
 
  if ( !digitalRead(next) ) {
    // 'next' button is pressed
    audio.stopPlayback();  // stop playing
    return;
  }
 
  if ( !digitalRead(_pause) ) {
    // '_pause' button is pressed
    audio.pause();      // pauses/unpauses playback
    while( debounce(_pause) ) ;  // wait until '_pause' button is released
  }
 
}
 
// a small function for buttons debounce
bool debounce (int bt)
{
  byte count = 0;
  for(byte i = 0; i < 5; i++)
  {
    if ( !digitalRead(bt) )
      count++;
    delay(10);
  }
 
  if(count > 2)  return 1;
  else           return 0;
}
 
void printDirectory(File dir, int numTabs) {
  while (true) {
 
    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}
 
// end of code.

To start with I would suggest you try the example/basic.ino included with the TMPpcm library and ensure it plays the sound with that.
Once you get that working then ensure your audio file is encoded correctly as mentioned in the TMPpcm wiki and play that using the basic.ino sketch.
After this you can be confident the configuration is correct so ensue your sketch reflects this and try again.

I get the feeling your using an Arduino Mega and not the UNO so the speaker pin will be pin 11 and not pin 9 as used on the UNO

Why don’t you get a board that has DAC built in if playing of audio is important. Trying to use PWM for that would probably sound horrible

Thank you for your solution; I changed my audio output pin to pin 46, and lo, it works! but as Killzone_Kid noted it sounds terrible. I'm going to use the TMRpcm library tweeks to see if I can get it sounding better. Thanks again for having a look!

You're right, it sounds terrible. lol I don't have the money right now to purchase a solution like that, so I'll have to deal with what I have. Thanks for your response!

You can buy cheap modules that can play mp3 from an SD card.

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