Playing audio with TMRpcm through SD card while receiving RF data

Hello!

I've been working on a project where an RF transmitter would send a codeword to an RF receiver -- if the codeword is correct, the Arduino Mega connected to the receiver would play a .wav audio file stored on a connected SD card module via the TMRpcm library.

So far, I have been able to get the Arduino Mega to play the audio one time (after the first instance of the correct keyword being sent), but the speaker falls quiet after this first playthrough, and the Mega no longer appears to receive/react to RF signals, nor does the audio play at all after that.

Here are the connections on the Mega:

  • 3V => connected to VCC LM386 Mono Audio Amplifier and Micro SD TF Card adapter
  • 5V => connected to VCC of 433M Transmitter
  • PWM 11 => connected to data pin of 433M Transmitter
  • PWM 5 => connected to IN pin of LM386 Mono Audio Amplifier
  • All other connections for the SD card have been checked and verified to be correct, and the SD card successfully loads every time

Here is the code itself:

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
#include <SD.h>                   
#define SD_ChipSelectPin 53
#include <TMRpcm.h>           
#include <SPI.h>

RH_ASK driver;
TMRpcm tmrpcm; 


void setup()
{
    tmrpcm.speakerPin = 5; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc

    Serial.begin(9600);	// Debugging only
    if (!driver.init())
         Serial.println("init failed");

    if (!SD.begin(SD_ChipSelectPin)) {  
      Serial.println("SD fail");  
      return;   
    }
    else
    {
      Serial.println("SD connected");
    }

    Serial.println("Connected!");  
    tmrpcm.setVolume(5);
}

bool playAudio = false;


void loop()
{
    uint8_t buf[12];
    uint8_t buflen = sizeof(buf);
    driver.waitAvailable();
    if (driver.recv((uint8_t *) &buf, &buflen))
        {
            Serial.println((char*)buf);
            if (strcmp((char*)buf, "hello there") == 0)
            {
                playAudio = true;
            }
            else
            {
                Serial.println("Did not receive correct keyword");
            }
        }
    if (!tmrpcm.isPlaying() && playAudio) 
    {
        tmrpcm.play("bell.wav") // Play audio file asynchronously
        playAudio = false; // Reset flag after starting playback
    }
    
    delay(100);
}

Any help would be greatly appreciated! I've been at this for a while and can't seem to find any solutions.

This is also my first time posting on the Arduino Forum, so any tips on how to make my post(s) more approachable/digestible would also be helpful.

Thank you!!

Welcome to the forum and thanks for posting the code, using code tags.

Both the RadioHead library and the TMRpcm library use timers, and you likely have a conflict with timer usage. Check both sets of library docs to see which timers are used on the Mega.

Since you did not post the transmitter code, it is impossible to predict whether the following line will work at the receiver. It will not, if the received string is not properly zero terminated. If not, that can be fixed on either end. Consider using strncmp() instead.

            if (strcmp((char*)buf, "hello there") == 0)

Hint: first get radio communications working without using the audio library.

Posting an annotated schematic (the language of electronics) would help us help you. Schematics work much better when troubleshooting then wiring diagrams (frizzies). Be sure to post links to technical information on the hardware items.

The timer was the thing to change -- thank you for the suggestion! I changed the RH_ASK.cpp file so that it would use timer2 rather than timer1 (there's a line you can uncomment in the file), and was able to get it working soon after.